//This code works in unix environment using cc, or gcc commands.
//It works on windows with "Turbo C", with small alterations in code.
Perfect Number :
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
File Name : perfect.c
#include <stdio.h>
void main()
{
int n,i,s=0;
printf("Enter a Number :");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
printf("Given no is a Perfect Number");
else
printf("Given no is not a Perfect Number");
}
Command To Compile in Ubuntu Terminal:
cc perfect.c
Note : The above command generates a.out(executable) file which can be executed as follows.
Command To Execute a.out file:
./a.out
Output1:
Enter a Number :6
Given no is a Perfect Number
Output2:
Enter a Number :28
Given no is a Perfect Number
Output3:
Enter a Number :53
Given no is not a Perfect Number
//It works on windows with "Turbo C", with small alterations in code.
Perfect Number :
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.
File Name : perfect.c
#include <stdio.h>
void main()
{
int n,i,s=0;
printf("Enter a Number :");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
printf("Given no is a Perfect Number");
else
printf("Given no is not a Perfect Number");
}
Command To Compile in Ubuntu Terminal:
cc perfect.c
Note : The above command generates a.out(executable) file which can be executed as follows.
Command To Execute a.out file:
./a.out
Output1:
Enter a Number :6
Given no is a Perfect Number
Output2:
Enter a Number :28
Given no is a Perfect Number
Output3:
Enter a Number :53
Given no is not a Perfect Number
No comments:
Post a Comment