Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Description:
Prime number is a number which is exactly divisible by one and itself only Ex: 2, 3, 5, 7,………
Algorithm:
Step 1: start Step 2: read n Step 3: initialize i = 1, c = 0 Step 4:if i <= n goto step 5 If not goto step 10 Step 5: initialize j = 1 Step 6: if j <= 1 do as the follow. If no goto step 7 i)if i%j == 0 increment c ii) increment j iii) goto Step 6 Step 7: if c == 2 print i Step 8: increment i Step 9: goto step 4 Step 10: stop
Flowchart:
Program:
#include<stdio.h> #include<conio.h> void main() { int n, i, j, count; clrscr(); printf("Prime no.series\n"); printf("Enter any number\n"); scanf("%d", &n); printf("The prime numbers between 1 to %d\n",n); for(i = 1; i <= n; i++) { count = 0; for(j = 1; j <=i; j++) if(i % j == 0) { count++; } if(count == 2) { printf("%d\t", i); } } getch(); }
Input & Output:
Prime no. series Enter any number 10 The prime numbers between 1 to 10 2 3 5 7
Viva Questions:
Q: What is prime number ?
Ans: Prime number is a number which is exactly divisible by one and itself only.
Q: What is an algorithm?
Ans: A step by step procedure is called algorithm.
Q: What is flow chart?
Ans: A pictorial representation an algorithm is called a flow chart.
Q: What is program?
Ans: A collection of statements is called.
-
UpdatedNov 23, 2014
-
Views63,828
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.