Vous êtes sur la page 1sur 19

1.To print the magic numbers within a range using recursive technique.

import java.util.*;//Import java packages

public class magic//Declare the class

int a,b;//declare variables

magic()//start constructor

a=0;//assign value

b=0;//assign value

void accept()//start function

Scanner ob=new Scanner(System.in);//create object

System.out.print("Enter the first number of the range ");//display message

a=ob.nextInt();//take input

System.out.print("Enter the second number of the range ");//display message

b=ob.nextInt();//take input

int sod(int b)//start function

if(b==0)//check condition

return 0;//return value

else
return (b%10+sod(b/10));//call the function within its body

boolean check(int n)//start function

while(n>9)//check condition

n=sod(n);//store value

if(n==1)//check condition

return true;//return true

else

return false;//return false

void show()//start function

System.out.println("Magic numbers within the range are ");//display message

for(int i=a;i<=b;i++)//start loop

if(check(i))//check condition

System.out.println(i);//print numbers

public static void main(String args[])//main method


{

magic S=new magic();//create object

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 2:Import java packages and declare the class.

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));

Step 6:Start a function to check whether a number is a magic number or not.

boolean check(int n)//start function

while(n>9)

nsod(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()

System.out.println("Magic numbers within the range are ");

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[])

magic S=new magic();

S.accept();

S.show();

Step 9:End of main

Step 10:End of class.

INPUT/OUTPUT

2.To print the armstong numbers within a range using recursive technique.

import java.util.*;//Import java packages

public class armstrong//Declare the class

int a,b,s;//declare variables

armstrong()//start constructor
{

a=0;//assign value

b=0;//assign value

s=0;//assign value

void accept()//start function

Scanner ob=new Scanner(System.in);//create object

System.out.print("Enter the first number of the range ");//display message

a=ob.nextInt();//take input

System.out.print("Enter the second number of the range ");//display message

b=ob.nextInt();//take input

int power(int b, int e)//start function

if(e==0)//Check conditon

return 1;//return value

else

return (b*power(b,e-1));//call the function in the body

boolean check(int n)//start function

{
int d=0;//assign value

int y=n;//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

return true;//return true

else

return false;//return false

void show()//start function

System.out.println("The armstrong numbers within the range are ");//display message

for(int i=a;i<=b;i++)//start loop

if(check(i))//check condition

System.out.println(i);//display numbers

}
public static void main(String args[])//main method

armstrong S=new armstrong();//create object

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 2:Import java packages and declare the class.

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.

int power(int b, int e)

if(e==0)

return 1;

else

return (b*power(b,e-1));

Step 6:Start a function to check whether a number is a armstrong number or not.

boolean check(int n)

int d0;

int yn;

s0;

while(n>0)

dn%10;

ss+power(d,3);

nn/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()

System.out.println("The armstrong numbers within the range are ");

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[])

armstrong S=new armstrong();

S.accept();

S.show();

Step 9:End of main

Step 10:End of class.


INPUT/OUTPUT

3.To check whether a number is a special number or not using recursive technique.

import java.util.*;//Import java packages

public class special//Declare the class

int n,s;//Declare variables

special()//constructor

n=0;//assign value

s=0;//assign value

void accept()//start function


{

Scanner ob=new Scanner(System.in);//create object

System.out.print("Enter the number ");//display message

n=ob.nextInt();//take input

int factorial(int n1)//start function

if(n1==0)//check condition

return 1;//return value

else

return (n1*factorial(n1-1));//call the function in the body

boolean check()//start function

int d=0;//declare variable

int y=n;//assign value

while(n>0)//check condition

d=n%10;//extract digit

s=s+factorial(d);//store the sum

n=n/10;//divide by 10

if(s==y)//check condition
return true;//return true

else

return false;//return false

public static void main(String args[])//main method

special S=new special();//create object

S.accept();//call method

if(S.check())//check condition

System.out.println("Special number");//display message

else

System.out.println("Not a special number");//display message

}//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.

Step 4:Create a recursive function to find the factorial of the number.

int factorial(int n1)

if(n1==0)

return 1;

else

return (n1*factorial(n1-1));

Step 5:Start a function to check whether a number is a special number or not.

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.

public static void main(String args[])


{

special S=new special();

S.accept();

if(S.check())

System.out.println("Special number");

else

System.out.println("Not a special number");

Step 7:End of main.

Step 8:End of class.

INPUT/OUTPUT

4.To check whether a word is palindrome or not using recursive technique.

import java.util.*;//Import java packages

public class reverse//Declare the class

{
String w;//declare variable

reverse()//constructor

w="";//assign

void accept()//start function

Scanner ob=new Scanner(System.in);//create object

System.out.print("Enter a word:- ");//display message

w=ob.nextLine();//take input

String reverse(String s, int l)//start function

if(l==-1)//check condition

return "";//return space

else

return (s.charAt(l)+reverse(s,l-1));//call the function in the body

void check()//start function

if(reverse(w,w.length()-1).equals(w)==true)//check condition

System.out.println("Palindrome Word");//display message


else

System.out.println("not a Palindrome Word");//display message

public static void main(String args[])//main method

reverse S=new reverse();//create object

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.

Step 3:Start a function to accept a word from the user.

Step 4:Create a recursive function to reverse a word.

String reverse(String s, int l)

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

System.out.println("not a Palindrome Word");

Step 6:Create main method and create an object of the class.Call the functions accordingly.

public static void main(String args[])

reverse S=new reverse();

S.accept();

S.check();

Step 7:End of main.

Step 8:End of class.


INPUT/OUTPUT

Vous aimerez peut-être aussi