Perfect number Program (ISC 2018)- recursive technique


Design a class Perfect to check if a given number is a perfect number or not. (A number is said to be perfect if sum of the factors of the number excluding itself is equal to the original number.)
Example : 6= 1+2+3 ( where 1, 2 and 3 are the factors of 6, excluding itself)

Class name    : Perfect
Data members:
num         : to store the number
Member  methods:
Perfect(int nn)     :  initialize the data member num=nn
int sumofFact(int i) : return the sum of the factors of the number, excluding itself, using recursive technique.
void check()       : checks whether the given number is perfect by invoking the function sumofFact()   and  displays the result with an appropriate message.
Specify the class  and define main() to create an object and call the functions accordingly to enable the task.

Program:
import java.util.*;
class Perfect
{
int num;
Perfect(int nn)
{
num=nn;
}
public int sumofFact(int i)
{
int n=num;
if(i>n/2)
return 1;
else
{
if(n %i==0)
return i +sumofFact(i+1);
else
return sumofFact(i+1);
}
}
public void check()
{
int s=sumofFact(2);
if(s==num)
System.out.println(num+”is a perfect number”);
else
System.out.println(num+”is not a perfect number”);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter a number”);
int n= sc.nextInt();
Perfect obj=new Perfect(n);
obj.check();
}
}

Output:

Enter a number
28
28 is a perfect number
Enter a number
8128
8128 is a perfect number
Enter a number
46
46 is not a perfect number

Recursive method


Find the output of the following fragment:

public class st
{

static int strange(int x,int y)
{
if(x>=y)
{
x=x-y;
return strange(x,y);
}
else
return x;
}

}

 

dry run

1    x=20,y=5               strange(20,5)

2.  x=15,y=5                strange (15,5)

3. x=10,y=5                strange (10,5)

4. x=5,y=5                   strange(5,5)

5. x=0,y=5                   strange(0,5)

returns  0;

 

Factorial using loop and using recursive method


Factorial of a number is the product of all descending numbers from the given number n. 

Example 5!  Means 5*4*3*2*1

The product of the descending numbers from 5 is 120.

Let’s look, can write a program using loop and recursion.

Looping statements 3

1. for loop

2. while loop

3. do while loop

I. Using for  loop

class FactorialLoop1
{  
 public static void main(String args[])
{  
  int i,fact=1;  
  int number=5; // to find the factorial of value that assigned to the variable number
  for(i=1;i<=number;i++)
{    
      fact=fact*i;    
  }    
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
}  

Output

Factorial of the given number 5 is : 120.

2. Using while loop

class FactorialLoop2
{  
 public static void main(String args[])
{  
  int i=1, fact=1;
  int number=5; // to find the factorial of value that assigned to the variable number
while( i<=number)
{    
      fact=fact*i;    
      i++;
  }    
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
} 

Output

Factorial of the given number 5 is:  120.

III. Using do while loop

class FactorialLoop3
{  
 public static void main(String args[])
{  
  int i=1, fact=1;
  int number=5; // to find the factorial of value that assigned to the variable number
do
{    
      fact=fact*i;    
      i++;
  }   while( i<=number);
  System.out.println("Factorial of the given number "+number+" is: "+fact);    
 }  
} 

Output

Factorial of the given number 5 is : 120.

Using Recursion

class FactorialUsingRecursion 
 static int factorial(int n)
{
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int  fact=1;  
  int number=5; //It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of the given number"+number+" is: "+fact);    
 }  
}  

Output

Factorial of the given number is: 120