Wednesday, 25 May 2016



Write a program that inputs a number form the user and display all perfect number upto the number entered.

          #include<iostream.h>
          #include <conio.h>
          void main()
          {
                 int  num , mid, sum, limit;
                 clrscr();
                 cout<<"Enter the number upto which perfect numbers are required. ";
                 cin>>limit;
                 if(limit < 6)
                    cout<<"No perfect number upto " <<limit;
                 else
                 {
                     cout<<"Perfect numbers upto "<<limit<< "  are "<<endl;
                     for(num = 6; num <= limit; num++)
                     {
                         sum = 0;
                          mid = num / 2;
                          for(int i=1; i<= mid; i++)
                          {
                               if((num % i)== 0)
                                   sum += i;
                          }
                          if(sum == num)
                             cout<<num << " ";
                     }
                 }
                  getch();
          }


No comments:

Post a Comment