C++ program to determine whether an Input number is perfect square or not

Explanation

In this C++ program, we will check whether our input number is a perfect square or not in C++ programming language without any use of built-in function. First of all, you must have knowledge about perfect square.

A perfect square is a number that can be written as the product of two integers or
as the integer’s second exponent.
25 is a perfect square because it is the product of the integer 5 multiplied by itself, 5*5 = 25.
For your information, we use * sign (asterisk) in computers for multiplication. In simple words,
a number is a perfect square if it is the square of an integer.

User Input: 36
Output: 36 is a perfect square.

Explanation: 36 is a perfect square. Because, 6 * 6 = 36

User Input: 29
Output: 29 is not a perfect square.

Code:

#include <iostream> //header file in C++

using namespace std;

int main()  {
  int number;  // number of integer datatype
  cout<<"Enter a number: ";  //displaying message to user in order to enter a number
  cin>>number; //input a number using cin statement
  bool check=false;

    for(int i=0;i<number;i++){  //for loop
        if (i*i==number){
            check=true;  //it will change check variable value if condition evaluates to true
        }
    }
    if(check){                     //if statement
        cout<<number<<" is a perfect square";
    }
    else {                         //else statement
        cout<<number<<" is not a perfect square";
    }

    return 0; // it states that our program executed successfully

}  // ending bracket of main function

OUTPUT:

Enter a number: 36
36 is a perfect square



Enter a number: 20
20 is not a perfect square

Code Working:

Step 1: In step 1, we will input a number through user.

Step 2: In step 2, we will use variable check of boolean datatype and storing ‘true’ value in it.

Step3: In step 3, we will apply for loop on our entered number with condition i<number and increment i++.

Let user enters a number 6. Loop will iterate from initial value of i variable that is 0 till 6 according to condition. At 0 value: 0<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 0*0==6 condition evaluates to false as 0*0==0 and 0!=6. Then, our check variable will store ‘false’ value in it.

At 1 value: 1<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 1*1==6 condition evaluates to false as 1*1==1 and 1!=6. Then, our check variable will store ‘false’ value in it.

At 2 value: 2<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 2*2==6 condition evaluates to false as 2*2==4 and 4!=6. Then, our check variable will store ‘false’ value in it.

At 3 value: 3<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 3*3==6 condition evaluates to false as 3*3==9 and 9!=6. Then, our check variable will store ‘false’ value in it.

At 4 value: 4<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 4*4==6 condition evaluates to false as 4*4==16 and 16!=6. Then, our check variable will store ‘false’ value in it.

At 5 value: 5<6 condition evaluates to true then our flow of control enters in loop body. In if statement, 5*5==6 condition evaluates to false as 5*5==25 and 25!=6. Then, our check variable will store ‘false’ value in it.

At 6 value: 6<6 condition evaluates to false because 6 is equal to 6 (6==6)

Step 4: In step 4, our flow of control will enter in if statement after for loop. If our check variable value is ‘true’ then our statement under if statement will execute otherwise, else part will execute. At last, our program will terminate.

For complete details of C++: Visit http://cppreference.com/

Guidance about Perfect Squares

Related Post

1 Comment

  • Anonymous April 24, 2022

    Thanks alot for sharing.

Leave a Reply

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