- 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

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

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 −

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

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.

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); } }

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
- 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
- Generate an even squares between 1 to n using for loop in C Programming
- 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
- C Program to print numbers from 1 to N without using semicolon
- Find count of Almost Prime numbers from 1 to N in C++
- Print prime numbers from 1 to N in reverse order
- Program to find all missing numbers from 1 to N in Python
- Python program to print all Disarium numbers between 1 to 100
- Print all prime numbers less than or equal to N in C++
- Python program to print all Happy numbers between 1 and 100

Advertisements