What is a perfect number?
If the sum of the divisor(excluding itself) of a number is equals to the same number, then it is a perfect number.
Example: 6 => 1, 2, 3 are the positive divisors of 6.
1 + 2 + 3 = 6. Sum of positive divisor gives the same number. So, 6 is a perfect number.
Write a C program to check whether the given number is perfect or not.
If the sum of the divisor(excluding itself) of a number is equals to the same number, then it is a perfect number.
Example: 6 => 1, 2, 3 are the positive divisors of 6.
1 + 2 + 3 = 6. Sum of positive divisor gives the same number. So, 6 is a perfect number.
Write a C program to check whether the given number is perfect or not.
int main () {
int data, i, res = 0;
/* get the input from the user */
printf("Enter your input value:");
scanf("%d", &data);
/* check whether the given input is perfect no or not */
for (i = 1; i <= data/2; i++) {
if (data % i == 0) {
res = res + i;
}
}
/* print the result*/
if (res == data)
printf("%d is a perfect number\n", data);
else
printf("%d is not a perfect number\n", data);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input value:6
6 is a perfect number
jp@jp-VirtualBox:~/$ ./a.out
Enter your input value:7
7 is not a perfect number
Enter your input value:6
6 is a perfect number
jp@jp-VirtualBox:~/$ ./a.out
Enter your input value:7
7 is not a perfect number
No comments:
Post a Comment