Program that checks whether a number is prime or not.
Object: Write a program that checks whether a number is prime or not by using if else structure and while loop. C++ projects for beginners with source code
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int num, i=2, a=0;
cout<<“Enter any Number: “;
cin>>num;
while(i <= num/2)
{
if(num%i == 0)
{
a=1;
break;
}
i++;
}
if(a == 0)
cout<<num<<” is a Prime Number.\n”;
else
cout<<num<<” is Not a Prime Number.\n”;
getch();
}
#include<iostream> #include<conio.h> using namespace std; void main() { int num, i=2, a=0; cout<<"Enter any Number: "; cin>>num; while(i <= num/2) { if(num%i == 0) { a=1; break; } i++; } if(a == 0) cout<<num<<" is a Prime Number.\n"; else cout<<num<<" is Not a Prime Number.\n"; getch(); }
Muchas gracias. ?Como puedo iniciar sesion?
Pingback: Top 40 projects with C++ for beginners in 2021