- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C program to display all prime numbers between 1 to N using for loop

## Problem

Write a C program to display all the prime numbers between 1 and n is a value given by the user at run time.

## Solution

C program to display all the prime numbers between 1 and n is a value given by the user at run time is explained below −

## Algorithm

Given below is an algorithm to display all the prime numbers between 1 and n is a value given by the user at run time.

**Step 1 **− Read n value.

**Step 2** − Initialize count = 0

**Step 3** − for i = 2 to n

a. for j = 1 to i b. if i % j = 0 c. then increment count d. if count is equal to 2 e. then print i value

## Flowchart

A flowchart is given below to explain the algorithm for the C program to display all the prime numbers between 1 and n is a value given by the user at run time.

## Example

Following is the C program to **display all the prime numbers between 1 and n is a value given by the user at run time** −

#include<stdio.h> void main(){ int i, num, n, count; printf("Enter the range: \n"); scanf("%d", &n); printf("The prime numbers in between the range 1 to %d:",n); for(num = 1;num<=n;num++){ count = 0; for(i=2;i<=num/2;i++){ if(num%i==0){ count++; break; } } if(count==0 && num!= 1) printf("%d ",num); } }

## Output

When the above program is executed, it produces the following result −

Enter the range:50 The prime numbers in between the range 1 to 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

- Related Questions & Answers
- Java Program to Display All Prime Numbers from 1 to N
- Program to find sum of prime numbers between 1 to n in C++
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- Find product of prime numbers between 1 to n in C++
- C++ Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Prime Numbers Between Intervals Using Function
- Generate an even squares between 1 to n using for loop in C Programming
- Java Program to Display Prime Numbers Between Two Intervals
- C program to display the prime numbers in between two intervals
- C program to print four powers of numbers 1 to 9 using nested for loop
- Program to print numbers from 1 to 100 without using loop
- Find count of Almost Prime numbers from 1 to N in C++
- C Program to print numbers from 1 to N without using semicolon
- Print prime numbers from 1 to N in reverse order
- C program to write all digits into words using for loop