Program that checks whether a number is prime or not by using if else structure and while loop

Program that checks whether a number is prime or not.

Program that checks whether a number is prime or not by using if else structure and while loop

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();
}

Output:

Program that checks whether a number is prime or not by using if else structure and while loop

Program that checks whether a number is prime or not by using if else structure and while loop

2 thoughts on “Program that checks whether a number is prime or not by using if else structure and while loop”

  1. Pingback: Top 40 projects with C++ for beginners in 2021

Leave a Comment

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