Q. Write a C program to accept a specific last number and print all the less then or equal prime number.
for example: 1 to 100
Ans.
Note:-
1. 1 is not the prime number.
2. All even number is not the prime number except the number 2.
/*C program to print 1 to 100 prime numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,div,p;
printf("Enter any number: ");
scanf("%d", &num);
for(n=2; n<=num; n++)
{
for(div=2; div<n; div++)
{
if(n%div==0)
{
p=0;
break;
}
p=1;
}
if(p)
printf(" %d",n);
}
getch();
return 0;
}
/************Output************/
Related program:
or
Q. Write a C program to print the prime number between 1 to n number.for example: 1 to 100
Ans.
Note:-
1. 1 is not the prime number.
2. All even number is not the prime number except the number 2.
/*C program to print 1 to 100 prime numbers*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num,n,div,p;
printf("Enter any number: ");
scanf("%d", &num);
for(n=2; n<=num; n++)
{
for(div=2; div<n; div++)
{
if(n%div==0)
{
p=0;
break;
}
p=1;
}
if(p)
printf(" %d",n);
}
getch();
return 0;
}
/************Output************/
Screen shot for print the prime number between 1 to 100 C program |
Related program:
0 comments:
Post a Comment