20_10

Write a c program to check whether a given number is a perfect number or not.

Source Code:

 /* Write a c program to check whether a given number is a perfect number or not. (Perfect number is a positive number which sum of all positive divisors excluding that number is equal to 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) */

#include <stdio.h>
int main()
{
int n,sum; /*n to store the number, sum - to store the sum of factors*/
printf("Enter a number: ");
scanf("%d",&n); /*Input the  number from the test case 6, 28 & 45 respectively */
int i;
sum=0;
for(i=1;i<n;i++)
{
  if(n%i==0)
    sum=sum+i;
}
if(sum==n)
    printf("YES");
    else
    printf("NO");
    return 0;
}

Output:
Write a c program to check whether a given number is a perfect number or not


No comments:

Post a Comment

Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...