• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

How to square a number in c++ ?

May 29, 2021 by Admin Leave a Comment

In c++ logic when we require square of a number then there are multiple solutions are available for it.

  1. You can use Power function of c++ pow(Base,Power)
  2. Simple multiplication of number two time
  3. If we want to make square without multiplication then we can achieve it using for loop or while loop.
Create square of number in c++
Square of a number solution in c++

How to square a number in c++ ?

#include<iostream>
#include<cmath>
using namespace std;

int main(){
    float B = 10.0;
    float P = 2.0;
    float Square_of_Number = pow(B,P);
    //B ^P = 10^2 = 100
    
    cout<<"Square of Number = "<<Square_of_Number<<endl;
}

Here we can square number using Power function. You will need to include #include<cmath> for it. Inside function we need to pass Base value which we want to square and Power value (for square it will be 2). So example will be Pow(Power,Base). You can consider it as c++ square operator.

square function c++

#include<iostream>
using namespace std;

float Square(float value){
    // Multiply value two times
    return value*value;
}

int main(){
    cout<<"Square of Number = "<<Square(10)<<endl;
}

We can create separate function for getting square of number. Here we just have to do multiplication of number two time.

Find square of a number without using multiplication or division operators

If there is requirement to create square of number without using multiplication/division then we have to create different logic using for loop or while loop.

  1. Using for loop, make addition of number
  2. Using while loop, we have to add Odd number so it will generate square at the end.

c++ program to find square of a number using for loop

#include<iostream>
using namespace std;

float Square(float value){
    
    float ans=0;
    // 10+10...10 time addition
    for(int i=0;i<value;i++){
     ans= ans + value;  
    }
    
    return ans;
}

int main(){
    cout<<"Square of Number = "<<Square(10)<<endl;
}

Here we are adding 10+10…. up to 10 times. So after addition it will create square of 10 which is 100.

c++ program to find a square of a number using while loop

#include <iostream>
using namespace std;
 
int Square(int Number)
{
    int OddNumber = 1;
    int SquareValue = 0;
 
    // If Value is negative : Convert to Positive
    Number = abs(Number);
 
    // add odd numbers num times to result
    // 3^2 = (1 + 3 + 5) = 9
    while (Number--)
    {
        SquareValue = SquareValue + OddNumber;
        OddNumber = OddNumber + 2; // Adding 2 to make it odd
    }
 
    return SquareValue;
}
 
int main()
{
    cout<<"Square of Number = "<<Square(3)<<endl;
    return 0;
}

Here we are adding odd number using while loop and final result will create square of a number.

SEE MORE:

What is c++ used for? | Top Uses of C++ programming language

Filed Under: All C++, C++

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter