Write a program to display prime nos between 1 to 10 [ using for, while and do-while loop ]
Program:
Using for loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,flag=0;
clrscr();
printf("Prime no between 1 and 10 are:\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
flag=0;
}
getch();
}
Using while loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int count,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
while(i<=10)
{
count=0;
a=1;
while(a<=i)
{
if(i%a==0)
count++;
a++;
}
if(count==2)
printf("%d\n",i);
i++;
}
getch();
}
Using do-while loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int flag,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
do
{
flag=0;
a=1;
do
{
if(i%a==0)
flag++;
a++;
}
while(a<=i);
if(flag==2)
printf("%d\n",i);
i++;
}
while(i<=10);
getch();
}
Output:
Program:
Using for loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,flag=0;
clrscr();
printf("Prime no between 1 and 10 are:\n");
for(i=1;i<=10;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
flag++;
}
if(flag==2)
printf("%d\n",i);
flag=0;
}
getch();
}
Using while loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int count,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
while(i<=10)
{
count=0;
a=1;
while(a<=i)
{
if(i%a==0)
count++;
a++;
}
if(count==2)
printf("%d\n",i);
i++;
}
getch();
}
Using do-while loop.....
#include<stdio.h>
#include<conio.h>
void main()
{
int flag,i=1,a;
clrscr();
printf("Prime no between 1 and 10 are:\n");
do
{
flag=0;
a=1;
do
{
if(i%a==0)
flag++;
a++;
}
while(a<=i);
if(flag==2)
printf("%d\n",i);
i++;
}
while(i<=10);
getch();
}
Output:
3 comments:
Prime number program in C
It's simple and easy code
This code containe Error...
this code contain error
Post a Comment