logo

Programming  -  C Programming



Topics

1.Find out the perfect number using C program

C program to check perfect number:

Program:
#include
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i 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:
Enter a number: 6
6 is a perfect number

2.C program to print perfect numbers from 1 to 100

C program to print perfect numbers from 1 to 100:

Program:
#include
int main(){
int n,i,sum;
printf("Perfect numbers are: ");
for(n=1;n<=100;n++){
i=1;
sum = 0;
while(i if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d ",n);
}
return 0;
}

Output:
Perfect numbers are: 6 28

3.C program to check whether a number is Armstrong or not

C program to check whether a number is Armstrong or not:

Program:
#include
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}

Output:
Enter a number: 153
153 is an Armstrong number

4.C program to find given number is prime or not

C program to find given number is prime or not:

Program:
#include
int main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}

Output:
Enter a number: 5
5 is a prime number

5.C program to find sum of prime numbers

C program to find sum of prime numbers:

Program:
#include
int main(){
int num,i,count,min,max,sum=0;
printf("Enter min range: ");
scanf("%d",&min);
printf("Enter max range: ");
scanf("%d",&max);
for(num = min;num<=max;num++){
count = 0;
for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
sum = sum + num;
}
printf("Sum of prime numbers is: %d ",sum);
return 0;
}

Output:
Enter min range: 50
Enter max range: 100

12345Next >>

Freshbuddies.com. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Contact us: info@freshbuddies.com