Perfect number program in java

Java code to check given number is perfect number or not


import java.io.*;

class Test {
     public static void main(String[] args) throws IOException {
          int n,i=1,sum=0;
          System.out.println("Enter a number:-");
          BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
          n=Integer.parseInt(br.readLine());

          while(i>0){
              if(n%i==0)
                   sum=sum+i;
              i++;
          }

          if(sum==n)
              System.out.println("The no   "+ i +"  is a perfect number");
          else
              System.out.println("The no  "+i+" is not a perfect number");
     }
Post a Comment