Search

Perfect Number Program in C || C Programs

In mathematics, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.

For example, 6 is a positive number that is completely divisible by 1, 2, and 3. We know that the number is also divisible by itself but we will include it in the addition of divisors. When we add these divisors (1 + 2 + 3 = 6), it produces 6, which is equal to the number that we have considered. So, we can say that 6 is a perfect number.


Code -

/*C program to check whether the given number is the Perfect number*/ 
#include<stdio.h> 
#include<conio.h> 
void main()  
{  
// declare and initialize the variables 
int num, rem, sum = 0, i;  
// take an input from the user. 
printf("Enter a number\n");  
scanf("%d", &num);      
// find all divisors and add them 
for(i = 1; i < num; i++)  
                     {  
                              rem = num % i;  
 if (rem == 0)  
                                        {  
                                               sum = sum + i;  
                                         }  
                        }  
if (sum == num)  
                      printf(" %d is a Perfect Number");  
 else 
                      printf("\n %d is not a Perfect Number");  
getch();  
}  

Output -

Enter a Number
29
Entered Number is a Perfect Number

0 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube