The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
A perfect number is defined to be one which is equal to the sum of all its divisible number. For example let us consider a number 6, the numbers which are all divisible by 6 are 1, 2, 3. Then 1 + 2 + 3 = 6 . Thus 6 is a perfect number.
Some of the following perfect numbers are
Number | Explanation |
---|---|
6 | 1 + 2 + 3 |
28 | 1 + 2 + 4 + 7 + 14 |
496. | 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 |
8128 | 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 |
Let us write a c program to find whether a user entered number is a perfect number or not.
#include <stdio.h> int main() { int a, b, num; printf("Enter a number : "); scanf("%d",&num); a = 1; b = 0; while(num > a) { if(num % a == 0) b = b + a; a++; } if(b == num) printf("\n%d is a perfect number", a); else printf("\n%d is not a perfect number", a); return 0; }
Enter a number : 28 28 is a perfect number
while loop is used for iterating a value from 1 to one number less than user entered number. If the numbers from 1 is divisible by user entered number, then the numbers are added in a dummy variable b. finally we check whether b == user entered number. If so we will declare as a perfect number else it will be declared as not a perfect number.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019