In this tutorial, we are going to write a program in C to print the first N Prime Number. Basically, on the basis of the given input, our program will print the Prime Number.
For example, if we want to print the first 5 prime numbers, Our program will take 5 as an input and it will print 2,3,5,7,11 as an output.
What is Prime number?
A prime number is a number that can only divide by 1 and the number itself.
for example, 2, 3, 5 etc are the prime number. Here 2 and 3 can only be divided by 1 and number itself.
But 4 or 6 is not a prime number because 4 can be divided by 2 and 6 can be divided by 2 and 3.
Let’s see the program in C
C Program to print first n Prime Number
#include<stdio.h> void main(){ int i=0,n,temp,temp1=1; printf("Please give input a number: "); scanf("%d",&n); while(temp1<=n){ temp=0; for(i=2;i<=(temp1/2);i++){ if(temp1%i==0) { temp=1; break; } } if(temp==0) printf("%d is a prime number \n",temp1); temp1++; } }
Output

Share on
Also Prepare Below Important Question
- Java Program to Perform Left Rotation on Array Elements by Two
- Java Program to Perform Right Rotation on Array Elements by Two
- Java Program to Print Odd Numbers from Array
- Java Program to Print All Even Numbers in Array
- Java Program to Find the Sum of Array Elements
- Java Program to Delete Element of Array At Given Location
- Java Program to Delete a given Element of Array
- Java Program to Delete Element at End of Array
- Java Program to Insert Element in Array at given Location
- Java Program to Insert Element At the End of Array
- Java Program to Print Length of an Array
- Reverse Array without using Second Array or inplace Reversal Java Program
- Java Program to Print Array in Reverse Order
- Java Program to Sort String Character in Descending order
- Java Program to Sort String in Ascending Order
- Java Program to Print non Repeating Characters in String
- Java Program to Find Sum of Integers in the String
- Java Program to Remove Duplicates From String
- Java Program to Concatenate two Strings
- Java Program to Check if two Strings are Same
Interview Questions Categories
C Programming Interview Preparation
Core Java Programming Interview Preparation
- Core Java Programming Coding Questions
- Core Java Pattern Programming Questions
- Core Java Programming Interview Questions