#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace std;
inlinevoid keep_window_open() {char ch; cin>>ch;}
int main()
{
cout<< "Please enter a floating-point value:";
double n;
cin>>n;
cout<< "n =="<<n
<< "\nn+1 =="<<n+1
<< "\nthree times n=="<<3*n
<< "\ntwice n=="<<n+n
<< "\nn squared=="<<n*n
<< "\nhalf of n=="<<n/2
<< "\nsquare root of n=="<<sqrt(n)
<< "\n"; // another name for newline ("end of line") in output
}
Now There is exercise:
Get this little program to run. Then, modify it to read an int rather than a double. Note that sqrt() is not defined for an int so assign n to a double and take sqrt() of that.
How should I do this? I get error message when trying do change the code.
#include<iostream>
int main()
{
std::cout<< "Please enter a integer value:";
int n;
std::cin>>n;
double nAsDouble = static_cast<double>(n);
std::cout<< "n =="<<n
<< "\nn+1 =="<<n+1
<< "\nthree times n=="<<3*n
<< "\ntwice n=="<<n+n
<< "\nn squared=="<<n*n
<< "\nhalf of n=="<<n/2
<< "\nsquare root of n=="<<sqrt(nAsDouble)
<< "\n"; // another name for newline ("end of line") in output
return 0;
}