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

By | October 4, 2021

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:

Vamware
Enter a Number: 17
It is a prime number

People are also reading:

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published. Required fields are marked *