In this tutorial we will learn how to write a c language program that print all perfect numbers between 1 to 500 or till n(given numbers).
C program code
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num,a,b,sum;
printf("\n Enter a number to know all perfect numbers till n:\n ");
scanf("%d",&num);
printf("\n The perfect numbers between 1 and %d are:\n",num);
for(a=1;a<=num;a++)
{
sum=0;
for(b=1;b<a;b++)
{
if(a%b==0)
{
sum+=b;
}
}
if(sum==a)
{
printf(" %d\n",a);
}
}
getch();
}
OUTPUT:
In this output the range of numbers to check perfect numbers is 500
And the perfect numbers between 1 and 500 is
6, 28, 496
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.
Created by-- HARSH CHAUHAN
No comments:
Post a Comment