Program to check whether the number is prime or not.

Here, we have a basic program example to check if a number is prime or not using different languages. This program is created in c language, c++, Java, and Python.

Program to check if a number is prime in C language

#include <stdio.h>
int main() {
  int n, i, flag = 0;
  printf("Enter a positive integer: ");
  scanf("%d", &n);
  if (n == 0 || n == 1)
    flag = 1;
  for (i = 2; i <= n / 2; ++i) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (flag == 0)
    printf("%d is a prime number.", n);
  else
    printf("%d is not a prime number.", n);

  return 0;
}

Program to check if a number is prime in C++ language

#include<iostream>
using namespace std;
int main()
{
  int n, i, flag = 0;
  cout<<"Enter a positive integer: ";
  cin>>n;
  if (n == 0 || n == 1)
    flag = 1;
  for (i = 2; i <= n / 2; ++i) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }

  if (flag == 0)
    cout<<n<<" is a prime number.";
  else
    cout<<n<<" is not a prime number.";

  return 0;
}

Program to check if a number is prime in Python language

num=int(input("Enter a positive Integer: "))
flag = False

if num == 1:
    print(num, "is not a prime number")
elif num > 1:
    for i in range(2, num):
        if (num % i) == 0:
            flag = True
            break
    if flag:
        print(num, "is not a prime number")
    else:
        print(num, "is a prime number")

Program to check if a number is prime in Java language

import java.util.*;
public class prime {
    public static void main(String[] args) {     
        int n, i, flag = 0;
	System.out.println("Enter a positive integer:  ");
        Scanner s=new Scanner(System.in);
        n = s.nextInt();
	if (n == 0 || n == 1)
	    flag = 1;
	  for (i = 2; i <= n/2; ++i) {
	    if (n % i == 0) {
	      flag = 1;
	      break;
	    }
	  }
	  if (flag == 0)
	    System.out.println(n + " is a prime number.");
	  else
	    System.out.println(n +" is not a prime number.");        
    }
}