Skip to main content

Perfect number in java, program for perfect number in java

A perfect number is one where the sum of factors of a number (excluding the number itself) is equal to the number itself.

EXAMPLE-
We take 6. Factors of 6 are: 1,2,3
6 = 1 + 2 + 3
6 is a perfect number.

Now we take 28 .Factors of 28 are: 1,2,4,7,14
28 = 1 + 2 + 4 + 7 + 14
28 is a perfect number.

Perfect number in java language can be made by first applying a for loop starting from 1 to 1 less than that number and then with the help of if statement, checking if the number is divisible by that particular number which is currently stored in loop control variable. 

Then after the loop terminates we will check whether the sum of factors of the number is equal to the number itself or not. If the statement returns true then appropriate statement prints.
Here is the code for perfect number in two different formats firstly with the help of methods and secondly a very simple program using main method.
JAVA CODE ( with the help of methods)-
import java.util.*;
public class perfect
{
int num;
int sum;
perfect()
{
}
perfect(int nn)
{
num=nn;
}
int fact(int x)
{
int s=0;
for(int i=1;i<=x/2;i++)
{
 if(x%i==0)
{
s=s+i;
}
}
return s;
}
void isperfect()
 {
int n=num;
sum=0;
perfect obj=new perfect();
sum=sum+obj.fact(n);
if(sum==num)
System.out.println(num+" is a perfect number");
else
System.out.println(num+" is not a perfect number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int e=sc.nextInt();
perfect obj=new perfect(e);
obj.isperfect();
}
}

Output-
enter a number
6
6 is a perfect number
enter a number
12
12 is not a perfect number


ALGORITHM-
 STEP 1: start
STEP 2: print"enter a number"
STEP 3: taking input of integer in e
STEP 4: sum = sum + STEP 8
STEP 5: repeat STEP 6 to STEP 7 until i<x/2
STEP 6: if x%i==0 then goto STEP 7
STEP 7: s=s+i
STEP 8: return s
STEP 9: if sum==num then goto STEP 10 else goto STEP 11
STEP 10: print num +"is a perfect number"
STEP 11: print num +"is not a perfect number" 
STEP 12: END

JAVA CODE ( WITHOUT USING METHOD)-
import java.util.*;
public class perfection
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
int num=sc.nextInt();
int sum=0;
for(int i=1;i<x;i++)
{
if(num%i==0)
sum+=i;
}
if(sum==num)
System.out.println(num+" is a perfect number);
else
System.out.println(num+" is not a perfect number");
}
}

Output-

enter a number
28
28 is a perfect number
enter a number
16
16 is not a perfect number

ALGORITHM-
 STEP 1: start
STEP 2: print "enter a number"
STEP 3: taking input in num
STEP 4: int i=1 ,repeat STEP 5 to STEP 6 until i    <num
STEP 5: if num%i==0 then goto STEP 6
STEP 6: sum=sum + i
STEP 7: if sum ==num then goto STEP 8 else goto STEP 9
STEP 8: print num+"is a perfect number"
STEP 9: print num+"is not a perfect number"
STEP 10:end

Related programs

 1-Prime number
If you have find this helpful please share .

 If you have any query related to this program then you can comment below.


Comments

Popular posts from this blog

How to Learn whole syllabus of subject in 1 day? + some cool last minute tips

Hi everyone,  In  this article I am going to tell you how to complete the whole syllabus for exam in one day. I will tell you a full one day plan to complete all your syllabus without having any doubt and also prepare for that subject. To follow this, firstly you have to divide the syllabus of your subjects into 3 parts-  The first part is for easy chapters second part is for moderate chapters and third parties for difficult chapters in easy chapters you have to put it at topics for which you can answer all the question.  In moderate you have to put that chapters in which you have very less doubt. In the difficult you have to put that topic whose questions you are not able to answer. HERE IS THE TEMPLATE FOR YOUR PLANNING- PLAN starting in the morning  Prepare a chart of the whole day time table. Begin your day at 8:00 a.m.  In the first three hours revise or learn all the chapters which are easy for you remember after every 50 minutes you have to take 10 minute break .once all the cha

What is magic number in java ?

A number is said to be magic number if the repetitive sum of digits of number is equal to 1 or a single digit number . If the repetitive sum ends up  as 1 then the number is a magic number, else not. For example- n= 289;.   2+8+9 = 19      19;.      1+9 = 10      10;.      1+0= 0       1 Since the number ends in 1, hence 289 is a magic number. Magic number in java is the java program to check whether a number is magic number or not . For this we do repetitive addition of the digits of the number till the number is a two or more digit number. If after repetitive adding digits of the number ends up to 1 then the loop stops and a message is displayed. For this program we need to extract digits of the number for this we use while loop and add this digit . Code snippet to get digits of the number - while(n!=0) {        int d = n%10;         s=s+d;        n=n/10; } ALGORITHM - STEP 1: start STEP 2: print "enter a number" STEP 3: taking input of integer in n STEP 4: rep

disarium number in java

A number is said to be disarium if the sum of its digit powered with their positions is equal to the number itself. EXAMPLE- 135 = (1^1) + (3^2) + (5^3)                             = 1 + 9 + 125                             = 135 A simple java program to check disarium number can be made by firstly calculating the length of the number and then adding digits of the number raised to the power of its position in the number. ALGORITHM - STEP 1 : Start STEP 2 : print "enter a number" STEP 3 : taking input of integer in n STEP 4 : int l= Integer.toString().length() STEP 5 : int no=n STEP 6 : int s=0 STEP 7 : repeat STEP 8 to STEP 11 until (no!=0) STEP 8 : int d=no%10 STEP 9 : s=s+(int)(Math.pow(d,l) STEP 10 :no=no/10 STEP 11 : l-- STEP 12 : if  (s==n) then goto STEP 13 else goto STEP 14 STEP 13 : print "the number is disarium" STEP 14 : print "the number is not disarium" STEP 15 : end JAVA CODE- import java.util.*; public class disarium {     public static void m