Find Out The Given Number Is Perfect Number Or Not by using Java Program

Code:

import java.util.*;

public class PerfectNumber
{
     public static void main(String[] args)
     {
int c = 0;

Scanner sc = new Scanner(System.in);

            System.out.print("\nEnter The Number : ");
            int n = sc.nextInt();

            for(int i=1;i<n;i++)
            {
if(n%i==0)
{
c = c+i;
                 }
                 else
                 {
c = c;
}
            }

            if(c==n)
            {
System.out.println("\n"+n+" Is A Perfect Number\n");
}
else
{
System.out.println("\n"+n+" Is Not A Perfect Number\n");
}
      }
}

Output of program:


No comments

Powered by Blogger.