Write a program in C++ & Python Program to Check Whether a Number is Prime or Not

Posted in

Write a program in C++ & Python Program to Check Whether a Number is Prime or Not

Vinay Khatri
Last updated on July 22, 2022

    In this program, we will code to find the number is prime or not. A number is said to be a prime number if it has only 2 divisors 1 and the number itself.

    Prime Number Program in C++

    #include<iostream.h>
    #include< conio.h>
    #include<stdio.h>
    #include<math.h>
    void main()
    {
    clrscr();
    int num,k=0;
    cout<<"Enter a number: ";
    cin>>num;
    for(int i=2;i<=num/2;i++)
    {
    if(num%i==0)
    {cout<<"It is not a prime number";
    break;
    }
    else
    k++;
    }
    if(k>1)
    cout<<"It is a prime number";
    getch();
    }

    Output:

    Enter a Number: 17
    It is a prime number

    Prime Number Program in Python

    num =int(input("Enter a Number: "))
    k=0
    for i in range(2,num//2):
        if num%i==0:
            print("It is not a prime number")
            break
        else:
            k+=1
    if k>0:
        print("It is a prime number")

    Output:

    Enter a Number: 17
    It is a prime number

    People are also reading:

    Leave a Comment on this Post

    0 Comments