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:
- Python Program to Check Armstrong Number
- WAP to find the greatest number among the three numbers
- Python Program to Display the Multiplication Table
- WAP in C to Check Whether the Number is a Prime or not
- Python Program to Print all Prime Numbers in an Interval
- WAP in C++ & Python to calculate the size of each data types
- Python Program to Find the Size of Image
- WAP in C to check whether the number is a Palindrome number or not
- Python Program to Sort Words in Alphabetic Order
- WAP in C++ and python to check whether the number is even or odd
Leave a Comment on this Post