- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion

# 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:

"); 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 Articles
- Java Program to Display All Prime Numbers from 1 to N
- Swift Program to Display All Prime Numbers from 1 to N
- Haskell program to display all prime numbers from 1 to n
- Kotlin Program to Display All Prime Numbers from 1 to N
- How to Display all Prime Numbers from 1 to N in Golang?
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C++ Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Prime Numbers Between Intervals Using Function
- Swift program to display prime numbers between intervals using function
- Haskell Program to Display Prime Numbers Between Intervals Using Function
- Program to find sum of prime numbers between 1 to n in C++
- C program to display the prime numbers in between two intervals
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- Java Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals