C++ → Basics → Arithmetical Operations → Input in C++
How C++ take values from the user?
Input using ‘cin’
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please insert a number:" << endl;
cin >> number;
cout << "You insert the number: " << number << endl;
return 0;
}
If we want to insert the number on the same line (and not on the next line, then??)
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please insert a number: "; cin >> number;
cout << "You insert the number: " << number << endl;
return 0;
}
NOTE: endl changes the line. So we have deleted the endl. As, we have deleted the endl, we also have to delete <<.
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Please insert a number: "; cin >> number;
cout << "The double of the number you inserted is: " << 2*number << endl;
return 0;
}
Convert kilometers into meters
#include <iostream>
using namespace std;
int main()
{
int kilometer;
cout << "Please insert value in kilometer: "; cin >> kilometer;
cout << "The number of meters are: " << 1000*kilometer << endl;
return 0;
}
Since there are 1000 meters in one kilometer.
The above program can also be written as
#include <iostream>
using namespace std;
int main()
{
int kilometer;
cout << "Please insert value in kilometer: "; cin >> kilometer;
cout << "The number of meters in " << kilometer << " kilometers are " << 1000*kilometer << endl;
return 0;
}
Convert temperature from Fahrenheit to Celsius
Q. Write a program in C++ that take temperature in Fahrenheit and show the equivalent temperature in Celsius.
#include <iostream>
using namespace std;
int main()
{
int fahrenheit;
cout << "Insert temperature in Fahrenheit: "; cin >> fahrenheit;
int celsius = (fahrenheit - 32)*5/9;
cout << "The equivalent temperature in Celsius is: " << celsius << endl;
return 0;
}
Physics: Check the two scales give the same temperature at -40.
Convert meters into feet
There are 3.28 feets in 1 meter. And integers do not take decimal values. So, we use ‘float’ here.
#include <iostream>
using namespace std;
int main()
{
float meter;
cout << "Insert length in meters: "; cin >> meter;
float feet = 3.28*meter;
cout << "The equivalent length in feet is: " << feet << endl;
return 0;
}
Convert British Pound into Pakistani Rupee
#include <iostream>
using namespace std;
int main()
{
float pound;
cout << "Insert amount in British Pounds: "; cin >> pound;
float pkr = 168.3*pound;
cout << "The equivalent amount in Pakistani Rupee is: " << pkr << endl;
return 0;
}
Q. Write a program in C++ that takes radius of a circle and shows its area.
Remember: The formula for area of a circle is πr2
#include <iostream>
using namespace std;
int main()
{
float radius;
const float pi = 3.14159;
cout << "Insert radius of the circle: "; cin >> radius;
float area = pi*radius*radius;
cout << "The area of the circle is: " << area << endl;
return 0;
}
We define the constant value of pi as, const float pi = 3.14159;. Now this value will not change throught the program.
Q. Write a program in C++ that takes radius and height of cylinder, and shows its area.
Remember: The formula for area of a cylinder is πr2h
#include <iostream>
using namespace std;
int main()
{
float radius;
float height;
const float pi = 3.14159;
cout << "Insert radius of the cylinder: "; cin >> radius;
cout << "Insert height of the cylinder: "; cin >> height;
float area = pi*radius*radius*height;
cout << "The area of the cylinder is: " << area << endl;
return 0;
}