[C++] 완전수 찾기소스

비주얼C++/예제모음 2014. 7. 24. 21:36


[C++] 완전수 찾기소스


완전수란?

"완전수란 자신을 제외한 모든 양의 약수들의 합을 가진 양수이다. 예를들어 6은 완전수이다. 왜냐하면 

약수 1,2,3의 합이 6이기 때문이다. 또한 28도 완전수이다. 

또다른 완전수 : 496,8128


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


코드 :


#include<iostream.h>


void main()                 //Start of main

{

    char ch;

int i=1, u=1, sum=0;

   while(i<=500)

 {                                  // start of first loop.


   while(u<=500)

   {                               //start of second loop.

     if(u<i)

     {

      if(i%u==0 ) // 

      sum=sum+u;

     }                          //End of if statement

    

     u++;

   }                           //End of second loop


   if(sum==i)

   {

     cout << i << " 는 완전수입니다." << "\n";

   }


   i++;

   u=1;  sum=0;

 }                             //End of First loop

 cin.get(ch);

 }                            //End of main



'비주얼C++ > 예제모음' 카테고리의 다른 글

삽입정렬  (0) 2012.11.24
선택정렬  (0) 2012.11.24
버블정렬  (0) 2012.11.24


설정

트랙백

댓글