Q: Create a function “square”, which calculate and displays square of any number entered through the keyboard.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void square()
{
int x, x2;
string str;
istringstream iss;
while (true) {
cout << "Enter a number (empty line to exit) ";
getline(cin, str);
if (str.size() == 0) {
break;
}
iss.str(str);
iss.clear();
iss >> x;
x2 = x*x;
cout << "The squere of " << x << " is " << x2
<< endl << endl;
}
}
int main() {
square();
}
Comments
Leave a comment