Check whether a given number is prime or not

Write a C program to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not in C.

Program in C

Code:

/*Write a C program to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not Using C*/

#include<stdio.h>
#include<math.h>
int main()
{
    int num,i;
    printf("Enter a number:");
    scanf("%d",&num);
    int count=0;
    for(i=2;i<=sqrt(num);i++)
    {
       if(num%i==0)
        count++;
    }
   if(count==0)
     printf("It is Prime ");
   else
        printf("It is not Prime ");
}

Input/Output:
Enter a number:37
It is Prime

Write a C++ program to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not in C++.

Program in C++

Code:

/*Write a C++ program to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not using C++*/

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;
    int count=0;
    for(int i=2;i<=sqrt(num);i++)
    {
       if(num%i==0)
        count++;
    }
   if(count==0)
     cout<<"It is Prime ";
   else
        cout<<"It is not Prime ";
}

Input/Output:
Enter a number:42
It is not Prime

Write a JAVA program to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not in Java.

Program in Java

Code:

/*Write a JAVA program to check whether a given number is prime or not. or Write a program to  check whether a given number is prime or not using Java*/

import java.util.Scanner;
public class PrimeOrNot {

public static void main(String[] args) {

Scanner cs=new Scanner(System.in);
int num,i;
    System.out.println("Enter a number:");
    num=cs.nextInt();
    int count=0;
    for(i=2;i<=Math.sqrt(num);i++)
    {
       if(num%i==0)
        count++;
    }
   if(count==0)
     System.out.println("It is Prime ");
   else
        System.out.println("It is not Prime ");
cs.close();
}
}

Input/Output:
Enter a number:
333
It is not Prime 

Write a PYTHON to check whether a given number is prime or not. or Write a program to check whether a given number is prime or not in Python.

Program in Python

Code:

'''Write a Python program to check whether a given number is a prime or not. or 
   Write a program to check whether a given number is a prime or not using Python '''

import math
num=int(input("Enter a number:"))
count=0
for i in range(2,int(math.sqrt(num))+1):
   if num%i==0:
      count+=1
if count==0:
        print("It is Prime")
else:
      print("It is not Prime")

Input/Output:
Enter a number:557
It is Prime





Post a Comment

0 Comments