Write a program in C++ & python to check whether the entered number is prime or not

Posted in

Vinay Khatri
Last updated on June 8, 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. 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

    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