Check number is prefect number or not - C Programming



/***************************************************************
 Statement - Check number is prefect number or not.
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 Compiler - gcc
 ***************************************************************/


#include <stdio.h>

int main() 
{

	//The first perfect number is 6, because 1, 2, and 3
	//are its proper positive divisors, and 1 + 2 + 3 = 6.

	int n, i = 1, sum = 0;

	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);
    }
	return 0;

}

/*
Output

Run1- 
Enter a number: 6
6 is a perfect number

Run2- 
Enter a number: 36
36 is not a perfect number
*/

Compilers -

  1. Compile C program with gcc compiler on Bash on Ubuntu on Windows 10
  2. Compile C++ program with g++ compiler on Bash on Ubuntu on Windows 10
  3. Turbo C++
  4. C/C++ Compiler (gcc) for Android - Run C/C++ programs on Android
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.