Q. Write a c program to check given number is perfect number or not. (C lan)

				
					#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i=1,sum=0;
 clrscr();
 printf("Enter a number:");
 scanf("%d",&n);
 while(i<n)
 {
  if(n%i==0)
  {
   sum=sum+i;
  }
  i++;
 }
 if(sum==n)
  printf("%d is a perfect number",i);
 else
  printf("%d is not a perfect number",i);
 getch();
}

				
			

Out put

				
					Enter number :6
6 is a perfect number
				
			

Another example

				
					#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i=1,sum=0;
 clrscr();
 printf("Enter a number:");
 scanf("%d",&n);
 while(i<n)
 {
  if(n%i==0)
  {
   sum=sum+i;
  }
  i++;
 }
 if(sum==n)
  printf("%d is a perfect number",i);
 else
  printf("%d is not a perfect number",i);
 getch();
}

				
			

This C program checks whether an input number is a perfect number. A perfect number is a number that is equal to the sum of its proper divisors (excluding the number itself). For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6.

Breakdown of Code and Functions

1.#include<stdio.h>

  • This is a preprocessor directive that includes the Standard Input Output header file. This file is required for using input/output functions like ‘printf‘ and ‘scanf‘.

2. “int main()

  • This is the main function from where the execution of the program starts.
  • The function returns an integer value (‘0‘ by default) which indicates that the program has terminated successfully.

Variable Declarations:

				
					int n, i = 1, sum = 0;

				
			
  • n‘:  Stores the number entered by the user.
  • i‘:  Acts as a loop counter, initialized to 1. It will be used to check for divisors.
  • sum‘:  Stores the sum of the divisors of the number. It is initialized to 0.

User Input:

				
					printf("Enter a number: ");
scanf("%d", &n);

				
			
  • printf("Enter a number: ")‘:  Displays a message asking the user to enter a number.
  • scanf("%d", &n)':  Takes an integer input from the user and stores it in the variable ‘n‘.

" while Loop "(Finding Divisors):

				
					while(i <= n / 2) {
    if(n % i == 0) {
        sum = sum + i;
    }
    i++;
}

				
			
  • while(i <= n / 2): This loop runs until ‘i‘is less than or equal to ‘n / 2‘. This is because divisors of a number will always be less than or equal to half of that number.
  • if(n % i == 0): This checks if ‘i‘ is a divisor of ‘n‘ by checking if the remainder of ‘n‘ divided by ‘i‘ is zero (i.e., ‘n % i == 0‘).
  • sum = sum + i: If ‘i‘ is a divisor, its value is added to the ‘sum‘ variable.
  • i++: Increment ‘i‘ to check the next number.

Check if the Number is Perfect:

				
					if(sum == n) {
    printf("%d is a perfect number\n", n);
} else {
    printf("%d is not a perfect number\n", n);
}

				
			
  • if(sum == n): After the loop, the program checks if the sum of the divisors is equal to the number itself.
  • If true, it means the number is a perfect number and the program prints ‘"n is a perfect number"‘.
  • Otherwise, it prints ‘"n is not a perfect number"‘.

return 0;

  • The ‘return 0;‘ statement indicates that the program executed successfully and returns control back to the operating system.
prime number

Example of Program Execution:

				
					Enter a number: 6
6 is a perfect number

Enter a number: 10
10 is not a perfect number

				
			

In this program:

  • For the input ‘6‘, the divisors of ‘6‘ are ‘1, 2, 3‘. The sum of these divisors is ‘1 + 2 + 3 = 6‘, so it correctly identifies ‘6‘ as a perfect number.
  • For the input ‘10‘, the divisors are ‘1, 2, 5‘. The sum is ‘1 + 2 + 5 = 8‘, which is not equal to ‘10‘, so it identifies ‘10‘ as not a perfect number.
Scroll to Top