Check The Given Number Is A Perfect Number Or Not
Source code :-
#include<stdio.h>
int main()
{
int n,s,i;
printf("Enter the number: ");
scanf("%d",&n);
s=0;
i=1;
while(i<=n/2)
{
if(n%i==0)
s=s+i;
i++;
}
if(s==n)
printf("\nThe entered number is a perfect number.\n");
else
printf("\nThe entered number is not a perfect number.\n");
return 1;
}
Input-Output :-
abhi@hp-15q-laptop:~$ gcc perfect.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 28
The entered number is a perfect number.
abhi@hp-15q-laptop:~$ gcc perfect.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the number: 12
The entered number is not a perfect number.
Print All The Perfect Number Of The Given Range
Source code :
#include<stdio.h>
int main()
{
int n,s,i,a,b;
printf("Enter the lower limit of the range : ");
scanf("%d",&a);
printf("\nEnter the upper limit of the range : ");
scanf("%d",&b);
printf("\nPerfect numbers are : ");
for(n=a;n<=b;n++)
{
s=0;
i=1;
while(i<=n/2)
{
if(n%i==0)
s=s+i;
i=i+1;
}
if(s==n)
printf("%d ",s);
}
return 1;
}
Input-Output :
abhi@hp-15q-laptop:~$ gcc perfect_number.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the upper limit of the range : 1
Enter the lower limit of the range : 10000
Perfect numbers are : 6 28 496 8128
Post a Comment