Algorithm
#include<stdio.h>
#include<conio.h> //only for windows users
void main()
{
clrscr(); //only for windows users
int lim,i,j,count;
printf("\n enter the limit:\t");
scanf("%d",&lim);
if(lim==1)
{
printf("\n one is neither prime nor composite");
}
else
for(i=1;i<=lim;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
}
getch(); \\only for windows users
}
Output
enter the limit: 10
2 3 5 7
- start
- set i=1,j=1
- read limit
- if limit =1
- display one is neither prime nor composite
- else
- repeat steps 8 to 14 if i<=limit,count=0
- repeat steps 9 to 11 if j<=i
- if i%j==0
- count++
- j+1 go to step 8
- if count==2
- display i
- i+1 go to step 4
- else
- stop
#include<stdio.h>
#include<conio.h> //only for windows users
void main()
{
clrscr(); //only for windows users
int lim,i,j,count;
printf("\n enter the limit:\t");
scanf("%d",&lim);
if(lim==1)
{
printf("\n one is neither prime nor composite");
}
else
for(i=1;i<=lim;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
}
getch(); \\only for windows users
}
Output
enter the limit: 10
2 3 5 7
useful
ReplyDelete