C++ Program to Check Number is Perfect Square, Armstrong or Prime using For Loop

Write C++ Program to Check Number is Perfect Square, Armstrong or Prime using For Loop

// CPP Program to Check Number is Perfect Square, Armstrong or Prime using For Loop

#include <iostream>
#include <math.h>

using namespace std;

int isPerfectSquare(int n)
{
	if (pow((int)sqrt(n), 2) == n)

		return 1;
	return 0;
}

int isArmstrongNumber(int n)
{
	int digits = ceil(log10(n));
	int temp = 0, temp2 = n, rem;
	for(; n > 0; )
	{
		rem = n % 10;
		temp += pow(rem, digits);
		n /= 10;
	}
	if (temp2 == temp)
		return 1;
	return 0;
}

int isPrime(int n)
{
	int i;
	for ( i = 2; i < n; i++)
	{
		if (n % i == 0)
		{
			return 0;
		}
	}
	return 1;
}

int main(int argc, char *argv[])
{
	int n, flag = 0;
	cout << "Enter the number: ";
	cin >> n;

	if (isPerfectSquare(n))
	{
		flag = 1;
		cout << "It is a Perfect Square\n";
	}
	if (isArmstrongNumber(n))
	{
		flag=1;
		cout << "it is an Armstrong number\n";
	}
	if (isPrime(n))
	{
		flag=1;
		printf("it is a Prime number\n");
	}

	if(flag == 0)
	{
		cout << "It's  not prime , Armstrong or Perfect Square";
	}

	return 0;
}

Output:

Run 1:
Enter the number: 36
It is a perfect square

Run 2:
Enter the number: 89
it is a prime number

≪ Previous | Next ≫