Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Others » C program for perfect number
Suryateja Pericherla Categories: Others. No Comments on C program for perfect number
0
(0)

In this article we will learn to implement a C program for perfect number. A C program is provided below to check whether the given number is a perfect number or not.

 

Examples of perfect numbers are: 6, 28, 496, etc.

 

Program is as follows:

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

 

Input and output for the above program is as follows:

Enter a number: 28
28 is a perfect number

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory