A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).
For example 28. Its divisor are 1, 2, 4, 7, 14, and 28. And the sum of its divisors i.e, 1+2+4+7+14 is also 28. So 28 is a perfect number
SOURCE CODE:
For example 28. Its divisor are 1, 2, 4, 7, 14, and 28. And the sum of its divisors i.e, 1+2+4+7+14 is also 28. So 28 is a perfect number
SOURCE CODE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include |
OUTPUT
Explanation
Here within the main() function first we declared two integer variable num and sum. We initialized sum as 0 .Then we display a message for the user for taking input and store it in the num variable. Then we are running a for loop from i=0 to iwithin which there is a conditional for loop. We are finding perfect divisor by checking if the remainder is zero i.e, num % i==0 then we are adding the value of i in sum where i is divisor.Next again we are running a if-else loop to declared the as perfect or not .If the sum is equals to the number then we are printing the number as perfect else it is not perfect.
Related Topics
- C program to display Fibonacci series up to n terms
- C Program to display Fibonacci series up to a certain number
- C Program to display the Factorial of a number
- C Program to find prime numbers between intervals using function.t
- C program to identify a number as Even or Odd
- C program to print "Hello World"
No comments:
Post a Comment