Page 200 - Computer science 868 Class 12
P. 200

Program 3     Input a number and print the sum of all the perfect numbers up to it.


                1       import java.util.*;
                2       class Prime_Perfect

                3       {
                4           public static void main()
                5           {

                6               Scanner sc = new Scanner(System.in);

                7               int i, j, num, Sum, sum_of_perfect=0;
                8               System.out.print("Please Enter any num : ");
                9               num = sc.nextInt();

                10              for(i = 1; i < num; i++)
                11              {

                12                    Sum=0;
                13                  for(j=1; j<i; j++)

                14                  {
                15                      if(i % j == 0)

                16                      {
                17                          Sum = Sum + j;

                18                      }
                19                  }

                20
                21                  if(Sum == i)

                22                  {
                23                      sum_of_perfect = sum_of_perfect + i;

                24                      System.out.println(i + " is a Perfect number");
                25                  }

                26              }

                27               System.out.println("Sum of all the perfect numbers upto "+ num + " is :
                                 "+ sum_of_perfect);
                28          }

                29      }

              The output of the preceding program is as follows:
              OUTPUT 1
              Please Enter any num : 75
              6 is a Perfect number



                198198  Touchpad Computer Science-XII
   195   196   197   198   199   200   201   202   203   204   205