1.to Print The Magic Numbers Within A Range Using Recursive Technique
1.to Print The Magic Numbers Within A Range Using Recursive Technique
magic()//start constructor
a=0;//assign value
b=0;//assign value
a=ob.nextInt();//take input
b=ob.nextInt();//take input
if(b==0)//check condition
else
return (b%10+sod(b/10));//call the function within its body
while(n>9)//check condition
n=sod(n);//store value
if(n==1)//check condition
else
if(check(i))//check condition
System.out.println(i);//print numbers
S.accept();//call method
S.show();//call method
}//end of main
}//end of class
VARIABLE LISTING
VARIABLE NAME DATA TYPE PURPOSE
a Integer To store the smaller number of
the range.
b Integer To store the larger number of
the range,as a function
argument.
n Integer As a function argument.
i Integer As a loop variable.
ALGORITHM
Step 1:Start.
Step 3:Declare the variables and start the constructor to assign values to the variables.
magic()
a=0;
b=0;
Step 4:Start a function to accept the upper and lower limits of the range.
void accept()
Step 5:Create a recursive function to calculate the sum of digits of each number.
int sod(int b)
if(b==0)
return 0;
else
return (b%10+sod(b/10));
while(n>9)
nsod(n);
if(n==1)
return true;
else
return false;
Step 7:Start display function and display the magic numbers within the entered range.
void show()
for(int i=a;i<=b;i++)
if(check(i))
System.out.println(i);
Step 8:Define the main function and create the object of the class to call the required functions.
public static void main(String args[])
S.accept();
S.show();
INPUT/OUTPUT
2.To print the armstong numbers within a range using recursive technique.
armstrong()//start constructor
{
a=0;//assign value
b=0;//assign value
s=0;//assign value
a=ob.nextInt();//take input
b=ob.nextInt();//take input
if(e==0)//Check conditon
else
{
int d=0;//assign value
s=0;//assign value
while(n>0)//check condition
d=n%10;//extract digit
s=s+power(d,3);//find summation
n=n/10;//divide by 10
if(s==y)//check condition
else
if(check(i))//check condition
System.out.println(i);//display numbers
}
public static void main(String args[])//main method
S.accept();//call method
S.show();//call method
}//end of main
}//end of class
VARIABLE LISTING
VARIABLE NAME DATA TYPE PURPOSE
a Integer To store the smaller number of
the range.
b Integer To store the larger number of
the range,as a function
argument.
n Integer As a function argument.
i Integer As a loop variable.
s Integer To store the sum.
e Integer As a function argument.
y Integer To store the number separately.
ALGORITHM
Step 1:Start.
Step 3:Declare the variables and start the constructor to assign values to the variables.
armstrong()
a=0;
b=0;
s=0;
Step 4:Start a function to accept the upper and lower limits of the range.
void accept()
Step 5:Create a recursive function to find the cube of the digits of each number.
if(e==0)
return 1;
else
return (b*power(b,e-1));
boolean check(int n)
int d0;
int yn;
s0;
while(n>0)
dn%10;
ss+power(d,3);
nn/10;
if(s==y)
return true;
else
return false;
Step 7:Start display function and display the armstrong numbers within the entered range.
void show()
for(int i=a;i<=b;i++)
if(check(i))
System.out.println(i);
Step 8:Define the main function and create the object of the class to call the required functions.
S.accept();
S.show();
3.To check whether a number is a special number or not using recursive technique.
special()//constructor
n=0;//assign value
s=0;//assign value
n=ob.nextInt();//take input
if(n1==0)//check condition
else
while(n>0)//check condition
d=n%10;//extract digit
n=n/10;//divide by 10
if(s==y)//check condition
return true;//return true
else
S.accept();//call method
if(S.check())//check condition
else
}//end of main
}//end of class
VARIABLE LISTING
VARIABLE NAME DATA TYPE PURPOSE
n Integer To store the entered number .
s Integer To store the sum.
n1 Integer As a function argument.
d Integer To store extracted digit.
y Integer To store the entered number
seperately.
ALGORITHM
Step 1:Start.
Step 2:Import java packages,declare the class and declare the variables.
Step 3:Start a function and accept a number from the user.
if(n1==0)
return 1;
else
return (n1*factorial(n1-1));
boolean check()
int d=0;
int y=n;
while(n>0)
d=n%10;
s=s+factorial(d);
n=n/10;
if(s==y)
return true;
else
return false;
Step 6:Create main method and create an object of the class.Display whether the entered number is a
special number or not.
S.accept();
if(S.check())
System.out.println("Special number");
else
INPUT/OUTPUT
{
String w;//declare variable
reverse()//constructor
w="";//assign
w=ob.nextLine();//take input
if(l==-1)//check condition
else
if(reverse(w,w.length()-1).equals(w)==true)//check condition
S.accept();//call method
S.check();//call method
}//end of main
}//end of class
VARIABLE LISTING
VARIABLE NAME DATA TYPE PURPOSE
w Integer To store the entered word .
s Integer As a function argument
l Integer As a function argument.
ALGORITHM
Step 1:Start.
Step 2:Import java packages,declare the class and declare the variable.
if(l==-1)
return "";
else
return (s.charAt(l)+reverse(s,l-1));
Step 5:Start a function to check whether a word is palindrome or not.Mwssages are displayed
accordingly.
void check()
if(reverse(w,w.length()-1).equals(w)==true)
System.out.println("Palindrome Word");
else
Step 6:Create main method and create an object of the class.Call the functions accordingly.
S.accept();
S.check();