-->





Translate

FIND THE PERFECT NUMBER IN C++

25 comments
What is a perfect number?
"Perfect number is a positive number which sum of all positive divisors excluding that number.
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and  28 is also a Perfect Number
 since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128




  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()                 //Start of main
  4. {
  5.   clrscr();
  6.    int i=1, u=1, sum=0;
  7.    while(i<=500)
  8.  {                                  // start of first loop.

  9.    while(u<=500)
  10.    {                               //start of second loop.
  11.      if(u<i)
  12.      {
  13.       if(i%u==0 )
  14.       sum=sum+u;
  15.      }                          //End of if statement
  16.     
  17.      u++;
  18.    }                           //End of second loop

  19.    if(sum==i)
  20.    {
  21.     cout<<i<<" is a perfect number."<<"\n";
  22.    }

  23.    i++;
  24.    u=1;  sum=0;
  25.  }                             //End of First loop
  26.    getch();
  27.  }                            //End of main

Sample output

is a perfect number.
28 is a perfect number.
496 is a perfect number.

Note:
If you want to calculate perfect number within your desire limit simply take a variable and replaced 500 with it.

25 comments:

  1. In APL: isPerfect←{⍵=+/(⍳⍵-1){(0=⍺|⍵)/⍺}⍵}

    isPerfect 28
    1

    ReplyDelete
  2. Also...
    {⍵ if isPerfect ¨⍵}⍳500
    6 28 496

    ReplyDelete
  3. very nice.was a great support for me!!!!!

    ReplyDelete
  4. AM NEW DUDE. SO WHERE DO I START?

    ReplyDelete
    Replies
    1. You Need To Understand The Basics Of C++ Programming.
      Try To Practice As Much As You Can.
      Practice is Very Important For Your Solid Base to Learn Programming.. Good Luck

      Delete
  5. Fahad bhai: i am new where do i start ?

    ReplyDelete
    Replies
    1. Waqas Bhai.
      You Need To Understand The Basics Of C++ Programming.
      Try To Practice As Much As You Can.
      Practice is Very Important For Your Solid Base to Learn Programming.. Good Luck

      Delete
  6. #include

    using namespace std;



    int main()
    {
    int num,perfect;
    int per=0;
    cout<<"enter number \n";
    cin>>num;
    int newnum=num;
    for(int i=1;i<=num;i++){
    if(num%i == 0){
    perfect=i;
    cout<<perfect<<endl;
    per=per+perfect;
    }
    }

    if(newnum == per/2){
    cout<<"perfect";
    }else{
    cout<<"not perfect";
    }


    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. A.o.A Fahad ..
    Your website is really really helping. I am a beginner in C++.I have made a more simplest logic for perfect numbers and it works.


    #include
    #include
    using namespace std;
    int main()
    {
    int a,b,c;
    c=0;
    cout<<"Plz enter a number :";
    cin>>b;
    for(a=1;a<b;a++)
    {
    if(b%a==0)

    c=c+a;

    }
    if(c==b)
    cout<<"Yes it is a Perfect number";
    else
    cout<<"No it is not a perfect number";

    getch();

    }

    ReplyDelete
  9. what is if and if else statement

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  11. How To Use C++ In Windows7? Becoz in my system windows7 there are not operating c Language & C++ Plz Help...

    ReplyDelete
  12. All the programs are really helpful... but i need more guidance bcuz every time i try to write program, i literally get confused! PLZ HELP ME GET OUT OF IT BCUZ I AM DOING SOFTWARE ENGG! I REALLY NEED HELP

    ReplyDelete


  13. Thanks for sharing the very useful info about clanguage and please keep updating........

    ReplyDelete
  14. hi
    I am a student of class 7and cant understand this.
    please can anyone help me with correcting the following:
    @include<
    #include
    void Main()
    {
    Clrscr();
    Int a=7;
    char section = F;
    cout>>”Hello class”<<a;
    cout<<”\nSection: “<<section<<endline;
    getch()
    }

    ReplyDelete