19 March, 2012

C++ Program To Find A Perfect Number.

This Post Contains A C++ Program To Find A Perfect Number With Correct Source Code & Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of 'While-loop' & 'If...else' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.


C++ Program To Find A Perfect Number.

# include <iostream.h>
# include <conio.h>

void main()
{
   int Num  =  0,  k,  i;
   clrscr();
   cout << " Enter Any Number : ";
   cin >> Num;
   i  =  1,  k  =  0;
   while  (  i  <  Num  )
   {
      if  (  Num  %  i  ==  0  )
         k  =  k  +  i;
         i++;
   }
   if ( k  ==  Num )
   {
      cout << Num << " Is A Perfect Number. ";
   }
   else
   {
      cout << Num << " Is Not A Perfect Number. ";
   }
   getch();
}

Output :

Enter Any Number : 6
6 Is A Perfect Number.

Enter Any Number : 13
13 Is Not A Perfect Number.




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.