Thursday, 6 August 2015

Calculate square of a number without using *, / and pow()

#include<iostream>
using namespace std;
 
int square(int n)
{
   // handle negative input
   if (n<0) n = -n;
 
   // Initialize result
   int res = n;
 
   // Add n to res n-1 times
   for (int i=1; i<n; i++)
       res += n;
 
   return res;
}
 
// drive program
int main()
{
    for (int n = 1; n<=5; n++)
        cout << "n = " << n << ", n^2 = "
             << square(n) << endl;
    return 0;
}

Leave a Reply

 
 

Labels

Blog Archive