C++ program to check whether a number is complete square or not

This program inputs a positive integer from user, checks whether it is a complete square or not and outputs the result to the user.

Explanation:

We use three variables in this program. n is used to store the number under check which is input by the user. i is initialized from 0 and then incremented by one during the execusion of a while loop. m is used to store the square of i during each loop. The loop executes as long as square of i i. e m is less than the number n. After this loop, an if selection statement is used to check whether this square which we have obtained as a result of execution of this loop is equal to the number n or not? if, it is, then we tell the user that the number he entered is a complete square. Otherwise we display a message on the screen stating that this number is not a complete square.

If you have any suggestions to improve this program then please let me know them via comments. I shall be very thankful to you!

Source Code:

// complete squre checker.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;

int _tmain()
{
	int i, n, m;
	i = 0;
	m = i * i;
	cout << "Please enter the number that you want to check:n";
	cin >> n;
	while (m < n)
	{
		i = i + 1;
		m = i * i;
	}
	if (m == n)
	{
		cout << n << " is a complete sqaure.n";
	}
	else
	{
		cout << n << " is not a complete square.n";
	}

	cout << "Press any key to close this program.";
	_getch();
	return 0;
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.