
- 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
Print prime numbers from 1 to N in reverse order
Input number n till which prime numbers are calculated and displayed in reverse order
Input : number 30 Output : 29 23 19 17 13 11 7 5 3 2
Algorithm
START Step 1 -> declare variables as n, I, j, flag to 0 as int Step 2 -> input number in n Step 3 -> Loop For from i to n and i>1 and i— Step 3.1 ->. Inner loop for from j to i/2 and j>=1 and j— Statement If %j==0 && j!=1 Set flag=0 Break End IF Else Flag=1 End Else Step 3.2 -> end inner Loop For Step 4 -> statement IF to check flag=1 Print i End IF Step 5 -> End outer For STOP
Example
#include <stdio.h> int main(int argc, char const *argv[]) { int n, i, j, flag=0; printf("Enter a number
"); scanf("%d", &n); for(i=n; i>1; i--) { for (j = i/2; j >= 1; j--) { if(i%j==0 && j!=1) { flag = 0; break; } else flag = 1; } if(flag == 1) { printf("%d ", i); } } return 0; }
Output
If we run the above program then it will generate the following output
Enter a number 30 29 23 19 17 13 11 7 5 3 2
- Related Articles
- How to Display all Prime Numbers from 1 to N in Golang?
- 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
- Find count of Almost Prime numbers from 1 to N in C++
- Print the nearest prime number formed by adding prime numbers to N
- C Program to print numbers from 1 to N without using semicolon
- Print all prime numbers less than or equal to N in C++
- How to print one dimensional array in reverse order?
- How to print the elements in a reverse order from an array in C?
- Print all odd numbers and their sum from 1 to n in PL/SQL
- Print all Semi-Prime Numbers less than or equal to N in C++
- Find product of prime numbers between 1 to n in C++
- Program to find sum of prime numbers between 1 to n in C++

Advertisements