C++ Algorithm
Date: Aug 2014
Level: Basic


INTERVIEW QUESTIONS

Company: Apple
Question Title: C++ Algorithm
Language: C++
QUESTION DETAILS:

How can we get square of a number without using * or carrot sign?


Here's the code:
#include <iostream> 
using namespace std; 
int square(int n) 
{ 
int sq_n=0,odd=1; 
for(int i=1;i<=n;i++) 
{ 
sq_n+=odd; 
odd=odd+2; 
} 
return sq_n; 
} 
int main() 
{ 
cout<<(square(5))<<endl;
return 0; 
}
All Questions