Page 222 - CA_Blue( J )_Class9
P. 222

Write a program to input a number and print the number is a perfect number or not. A perfect
                   Program 11
                                  number is a number whose sum of the factors that is equal to the number. (6 = 1+2+3 =6).

                     1     import java.util.*;
                     2     class prog_loop3

                     3     {
                     4         public static void main()

                     5         {
                     6             Scanner sc = new Scanner(System.in);
                     7             int n, i=1, sum=0 ;

                     8             System.out.println("Enter a number : ");

                     9             n=sc.nextInt();
                    10             while (i<n)
                    11             {

                    12                 if(n%i==0)
                    13                 {

                    14                  sum=sum+i;
                    15                 }

                    16                 i++;
                    17             }

                    18             if (sum == n)
                    19                 System.out.println(n +" is perfect number.");

                    20             else
                    21                 System.out.println(n +" is not a perfect number.");

                    22         }
                    23     }

                  You will get the following output:
























                   220    Touchpad Computer Applications-IX
   217   218   219   220   221   222   223   224   225   226   227