Question #47874

a simple c++ program that accepts positive integers and then displays a result whether the number is prime, odd, even or a perfect square

Expert's answer

//Connecting library
#include<iostream>
#include<math.h>

usingnamespace std;

boolisPrime(int a)
{
for(int i=1; i<a; i++)
{
if(a%i==0)
{
return false;
}
}

return true;
}

boolisOdd(int a)
{
if(a%2!=0)
{
return true;
}
else
{
return false;
}
}

boolisEven(int a)
{
if(a%2==0)
{
return true;
}
else
{
return false;
}
}

boolisSquare(int a)
{
double b =sqrt((double)a);

if(floor(b)==b)
{
return true;
}
else
{
return false;
}
}


//The main function
int main()
{

int a=0;
int sum=0;
while(1)
{
cout<<endl<<"Enter the number: ";
cin>>a;

if(a>0)
{
if(isPrime(a))
{
cout<<"This is Prime number!";
}

if(isOdd(a))
{
cout<<"This is Odd number!";
}

if(isEven(a))
{
cout<<"This is Even number!";
}

if(isSquare(a))
{
cout<<"This is Square number!";
}

}
else
{
cout<<"A negative number!!";
}

}

//system("pause");
return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS