C program to print prime numbers up to n
Program Description:
Write a C program to print prime numbers up to n (user-provided number). The program should accept a positive number from the user let’s say it is as n and Program should print all prime numbers up to n. We are going to use the loops to get the prime numbers. Here is an example Inputs and outputs of the program
Example Inputs and Outputs
Input:
Enter a Positive Number : 10
Output:
2 3 5 7
Here we have printed the prime numbers up to the number 10, which are 2 3 5 7.
Related Programs:
This program is part of a series of prime number generation programs. Here are other articles.
- What is Prime Number and C program to Check given number is Prime or Not
- Check given Number is Prime or not Using Square Root(sqrt) Function.(Efficient way)
- C Program to generate prime numbers between two numbers
- C Program to generate first N prime numbers.
- C program to print prime numbers up to n
- C Program to Print nth Prime Number in C
Program Pre-Requisites:
It is recommended to have knowledge of the C loops and goto statement to understand the program better. Please go through the following articles to learn more.
C program to print prime numbers up to n Algorithm:
- Take the input from the user and store it in variable n
- Check if the user entered a positive number, If not display an error message and allow the user to enter the input number again ( We are using the Goto statement to jump from one statement in the function to another)
- If the number is a positive number, Then we proceed further
- We will use two for loops (Nested for loops), Outer for loop and Inner for loop. The Outer for loop will go from the 2 to the n (user input) and Inner for loop will start from 2 to i/2 ( here i is outer for loop control variables – please look at the program below)
- The outer loop will go from 2 to n, with the help of the loop variable i. And at each iteration,
- We are going to check if the i is a prime number or not using another loop (Inner loop).
- We will use the simple prime number check logic – check if i is divisible by any number from 2 to i/2 ( We can also use the square root method )
- If i is prime, Print the number, If not proceed to the next number.
- The Outer loop will terminate once the i value reaches n ( user input). This way we will get all the prime numbers from the 2 to the user-provided number n.
Let’s convert the above pseudo-code into the C code.
C program to print prime numbers up to n using for loops:
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 42 |
/* C Program to print prime numbers upto n */ #include<stdio.h> int main() { int i,j,cnt,n; LABEL: printf("Enter a Positive Number : "); scanf("%d", &n); /* * let’s test given number is above 1 * and number must non Negative number */ if(n < 2) { printf("Invalid Input, Please enter valid Number \n "); goto LABEL; } for(i=2; i<= n ; i++) { // this loop for iterating from 2 to given number cnt = 0; for(j=2; j<=i/2; j++) { // testing i is prime or not.. if(i%j == 0) { cnt++; break; } } if(cnt == 0) { // i is prime.. printf("%d ",i); } } printf("\n"); return 0; } |
C program to print prime numbers up to n Output:
Compile and run the program using IDE.
Test 1: When the user enters a positive number
1 2 3 4 5 |
$ gcc prime-upto-n.c $ ./a.out Enter a Positive Number : 10 2 3 5 7 $ |
As you can see from the above output, The program is printed all prime numbers up to 10
Test 2: Prime numbers up to 100
1 2 3 4 |
$ ./a.out Enter a Positive Number : 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 $ ./a.out |
Test 3: Negative Number:
1 2 3 4 5 6 |
$ ./a.out Enter a Positive Number : -3 Invalid Input, Please enter valid Number Enter a Positive Number : 45 2 3 5 7 11 13 17 19 23 29 31 37 41 43 $ |
In the event that the user entered a negative value, the application presented an error notice – <strong>Invalid</strong> Input, <strong>Please</strong> <strong>enter</strong> <strong>valid</strong> Number and Prompted to provide the number again.
Exercise:
- Rewrite above program using the while loop
Related programs:
- C program to Calculate percentage of student.
- C Program to check given year is the leap or not.
- C Program to convert Temparatuere.
- C program to understand type conversation.
- Finding Largest of two numbers using the conditional operator in C.
- C program to calculate the simple Interest,
- C program to understand Size of Operator
3 Responses
[…] C program to print prime numbers up to n […]
[…] C program to print prime numbers up to n […]
[…] C Program to generate Prime numbers up to N […]