Problem Statement - Square and square root of numbers in a range
Write a c++ program to output the square and square roots of a number in a range where range begin variable is initialised in the variable ‘from’ and range end variable is initialised in the variable ‘to’.
Solution
Run Output
Solved Problem Understanding
#include <math.h>
This library header will support use of sqrt function.
int from=10,to=20;
Here the range variable names declared and initialised as desired.
for(int i=from;i<=to;i++)
Here the range variable are used at appropriate places. The from variable for loop begin condition and to variable to check the loop closure condition.
cout<<i<<“x”<<i<<” = “<<i*i<<” “;
This line outputs the square by using the proper chaining of variables and string literals.
cout<<“sqrt(“<<i<<“)”<<” = “<<sqrt(i)<<endl;
This line print the square root with required suffix.
Common Errors
- Care must be taken to chain the variables and string literals properly in the cout streams.
