Detailed Print – CSKC
Sign In

Search

siteicon
Solved Problem#CPP#2049 Back

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

		 

Click to open popup

Run Output

10x10 = 100 sqrt(10) = 3.16228
11x11 = 121 sqrt(11) = 3.31662
12x12 = 144 sqrt(12) = 3.4641
13x13 = 169 sqrt(13) = 3.60555
14x14 = 196 sqrt(14) = 3.74166
15x15 = 225 sqrt(15) = 3.87298
16x16 = 256 sqrt(16) = 4
17x17 = 289 sqrt(17) = 4.12311
18x18 = 324 sqrt(18) = 4.24264
19x19 = 361 sqrt(19) = 4.3589
20x20 = 400 sqrt(20) = 4.47214

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.