Friday 10 December 2010

To print a prime numbers up to 1 to n


#include<stdio.h>
#include<conio.h> void main()

{
int  n,i,fact,j; clrscr();

printf("enter the number:"); scanf("%d",&n); for(i=1;i<=n;i++)

{
fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT. for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2) printf("\n %d",i);

}
getch( );
}

Output:

Enter the number : 5
1     3  5
Enter the number : 10
 2 3 5 7
Enter the number : 12
2     3  5  7 11
Conclusion : The program is error free

VIVA QUESTIONS:

1) What is prime number ?
Ans: Prime number is a number which is exactly divisible by one and itself only

2)What is an algorithm?
Ans : A step by step procedure is called algorithm

3)What is flow chart?
Ans: A pictorial representation an algorithm is called a flow chart

4)What is program?
Ans : A collection of statements is called program


No comments:

Post a Comment