Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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’.
#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.