#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int st, n, x, flag;
clrscr();
printf("Enter the value of 'n': ");
scanf("%d", &n);
for(st=1; st<=n; st++) {
x=2; flag=0;
while(x<=st/2)
{
if(st%x==0) { flag=1; break; }
x++;
}
if( flag==0)
printf("%5d", st);
}
getch();
return 0;
}
Explaination:-Here, my first loop we will stops when 'st' equals to 'n' value.
And Now the Logic part.
In the second loop ie.,(while), the loop will ends when 'x' equals to half of the given number.
Important point:-
*******************
Here 'flag' variable is used to check whether 'st' is divisible by 'x' or not. If it is divisible then 'flag' becomes 'one' and it will not be executed becoz 'st' value will be displayed when 'flag' is zero.
No comments:
Post a Comment