• Post author:
• Post category:C Programs

If you are searching for Prime number program in c | c++ and java then you are at the right article.

## What is prime number?

Prime number is a number which can only be divided completely by 1 and that number.

Examples : 1, 2, 3, 5, 7, 9, 11,13, 15, 17, 19.

## Logic to check prime number

Input the number. Iterate from 2 to 9. For each iteration check whether the number divisible completely or not. If that number is divides completely then that is not a prime number. We can make use of modulus operator in an if else condition check to confirm that number is divisible completely or not.

## Prerequisite programming concepts for prime number program in c c++ and java

• Datatype in c | c++ and java.
• For loop in c | c++ and java.
• If else in c | c++ and java.
• Input output operation in c | c++ and java.
• Modulus operator in c | c++ and java.
• Comparisn operator c | c++ and java.

## Prime number program in c

``````#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("\nPrime number program in c\nEnter a number to check for prime number in c \n");
int number;
scanf("%d",&number);
int is_prime=1;
for( int i=2 ; i<10 ; i++ )
{
if( i == number )
continue;
if( ( number % i ) == 0 )
{
is_prime=0;
break;
}
}
if( is_prime )
{
printf( "\n%d is a prime number.\n",number );
}
else
{
printf( "\n%d is not a prime number.\n",number );
}
return 0;
}``````

## Prime number program in c++

``````#include<iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"\nPrime number program in c\nEnter a number to check for prime number in c++ \n";
int number;
cin>>number;
int is_prime=1;
for( int i=2 ; i<10 ; i++ )
{
if( i == number )
continue;
if( ( number % i ) == 0 )
{
is_prime=0;
break;
}
}
if( is_prime )
{
cout<<"\n"<<number<<" is a prime number.\n";
}
else
{
cout<<"\n"<<number<<" is not prime number.\n";
}
return 0;
}``````

## Prime number program in java

``````import java.util.Scanner;

public static void main(String[] args) {
int number=0;
Scanner stdin = new Scanner( System.in );
System.out.println( "Prime number program in java.");
System.out.println( "Enter a number.");
number = stdin.nextInt();
boolean isPrime=true;
for( int i=2 ; i<=9 ; i++  ) {
if( i == number )
continue;
if( ( number % i ) == 0 ) {
isPrime=false;
continue;
}
}
if( isPrime )
{
System.out.println( number+" is a prime number." );
}
else
{
System.out.println( number+" is not prime number." );
}
}

}
``````