In: Computer Science
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
Here is the solution:
CPP code:
#include <iostream>
#include <cmath>
#include <iomanip> // to set the decimal places
using namespace std;
int main(){
int N;
cout<<"Enter the table size:\n";
cin>>N; // read input
cout<<"N\tN^2\tSquare root of N\n";
for(int i=1;i<=N;i++){
cout<<i; // N
cout<<"\t"<<i*i;
//N^2
cout<<"\t"<<setprecision(3)<<sqrt(i); //Square
root of N
cout<<"\n";
}
}
code and output:
If you have any doubts please leave a comment!!