In this tutorial you will learn about the C program to print Prime Numbers from 1 to 100 and its application with practical example.
C program to print Prime Numbers from 1 to 100
In this tutorial, we will learn to create a C program that will print Prime Numbers from 1 to 100 using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Conditional Statements in C programming.
- Arithmetic operations in C Programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the required variables for the program. 2. Defining the range for numbers. 3. Using the child for loop to find the prime numbers. 4. Finding the prime numbers in between the range. 5. Printing the resultant prime numbers. 6. End the Program. |
Prime Numbers:-
The prime numbers are those numbers that are only divisible by themselves. These numbers can’t be complete, divide by the 2 & 3.
Display Prime Numbers Between 1 and 100:-
In today’s program, we will define the input range to print the prime numbers between them. Then we will find the prime numbers between the range using the for loops and the conditional statements. Now we will print the prime numbers.
With the help of this program, we can Display Prime Numbers Between Two 1 to 100 Using For Loop and conditional statements.
Program Code to print prime numbers:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* C Program to Print Prime number between 1 and 100 using For Loop */ #include <stdio.h> int main() { /* Declaring the required variable for the program */ int i, no, flag; /* no = it will hold the integer value for the parent loop i = it will hold the integer value for the child loop flag = it will hold the temporary value */ /* Declaring the parent for loop */ printf(" Prime number from 1 to 100 are: \n"); for(no = 1; no <= 100; no++) { flag = 0; /* Declaring the child for loop */ for (i = 2; i <= no/2; i++) { if(no%i == 0) { flag++; break; } } if(flag == 0 && no != 1 ) { /* Printing the prime numbers in the range */ printf(" %d ", no); } } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value of the loop.
- no = it will hold the integer value of the loop.
- flag = it will hold the temporary value.
The parent for loop to define the range.
Defining the child loop for the program.
Printing output prime numbers between the range of 1 to 100.