Code 1:C program to determine prime number
#include<stdio.h>
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;
}
Sample output:
Enter a number:7
7 is a prime number
____________________________________________________________
Code 2: c program to generate n prime
numbers
#include<stdio.h>
int main(){
int number,i,count,n;
printf("Enter the max range of prime numbers you want ");
scanf("%d",&n);
for(number=1; number<=n; number++){
count
= 0;
for(i=2;i<=number/2;i++){
if(number%i==0){
count++;
break;
}
}
if(count==0 && number!= 1)
printf("%d
",number);
}
return 0;
}
Sample output:
Enter the max range of prime numbers you want: 20
2 3 5 7 11 13 17 19
____________________________________________________________
Code 3:How to print prime numbers from 1 to 100
in c
#include<stdio.h>
int main(){
int number,i,count;
for(number = 1;number<=100;number++){
count
= 0;
for(i=2;i<=number/2;i++){
if(number%i==0){
count++;
break;
}
}
if(count==0 && number!= 1)
printf("%d
",number);
}
return 0;
}
Output:
2 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79 83 89 97
____________________________________________________________
Code 4:Sum of prime numbers from 1 to 100 in c
#include<stdio.h>
int main(){
int
num,i,count,sum=0;
for(num = 1;num<=100;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:
Sum of prime numbers is: 1060
____________________________________________________________
Code 5:C program to find prime numbers using while loop
#include<stdio.h>
int main(){
int
num,i,count,min,max;
printf("Enter
minimum range: ");
scanf("%d",&min);
printf("Enter maximum range: ");
scanf("%d",&max);
num
= min;
while(num<=max){
count
= 0;
i=2;
while(i<=num/2){
if(num%i==0){
count++;
break;
}
i++;
}
if(count==0 && num!= 1)
printf("%d
",num);
num++;
}
return 0;
}
output:
Enter minimum range: 20
Enter maximum range: 30
23,29
____________________________________________________________
Code 6:C program to find sum of prime numbers
#include<stdio.h>
int main(){
int
num,i,count,min,max,sum=0;
printf("Enter minimum range: ");
scanf("%d",&min);
printf("Enter maximum 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;
}
Enter min range: 20
Enter max range: 30
Sum of
prime numbers is:52
____________________________________________________________
Code 7:C program to find prime numbers using for loop
#include<stdio.h>
int main(){
int
num,i,count,min,max;
printf("Enter minimum range: ");
scanf("%d",&min);
printf("Enter maximum 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)
printf("%d
",num);
}
return 0;
}
output:
Enter minimum range: 20
Enter maximum range: 30
23,29
No comments:
Post a Comment