Skip to main content

Taking input from users

Taking input from users is important in programming because it allows your programs to interact with people and respond to their specific needs. Users can provide data or make choices that influence how your program works. In C++, you use the cin object to take input from the user.

Importance: Taking input from users allows your programs to be more versatile and user-friendly. It enables customization and interaction, making your software more useful and engaging.

Object to Use: In C++, you use the cin object to take input from the user. You can use it with the >> operator to read various types of data, such as numbers, text, and more, from the keyboard. For example, cin >> variableName; is used to store user input in a variable named variableName.

Example :

#include <iostream>
using namespace std;

int main() {

int age;

cout<<"Enter Age:";
cin>>age;

cout<<age;

return 0;
}

Output:

Enter Age: 8
8

Explanation :

Line 6 : declare variable name age
Line 8 : Enter Age from user using cout object.
Line 9 : store value of age using cin object.
Line 11 : print age variable.

WAP to print sum of numbers by taking values from user​

Example:

#include <iostream>
using namespace std;

int main() {

int num1;
int num2;

cout<<"Enter num1:";
cin>>num1;

cout<<"Enter num2:";
cin>>num2;

int sum = num1 + num2;

cout<<"Sum ="<<sum;

return 0;
}

Output :

Enter num1: 5
Enter num2: 7
Sum = 12

Explanation :

Line 6 : declare variable name num1
Line 7 : declare variable name num2
Line 9 : Enter num1 from user using cout object.
Line 10 : store value of num1 using cin object.
Line 12 : Enter num2 from user using cout object.
Line 13 : store value of num2 using cin object.
Line 15 : declare sum variable and assign value of num1 + num2
Line 17 : print sum variable .

Area of Reactangle​

Example :

#include <iostream>
using namespace std;

int main() {

int l;
int b;

cout<<"Enter length:";
cin>>l;

cout<<"Enter breadth:";
cin>>b;

int area = l * b;

cout<<"Area = "<<area;
return 0;
}

Output:

Enter length: 8
Enter breadth: 4
Area = 32

Explanation :

Line 6 : declare variable name l
Line 7 : declare variable name b
Line 9 : Enter length from user using cout object.
Line 10 : store value of l using cin object.
Line 12 : Enter breadth from user using cout object.
Line 13 : store value of b using cin object.
Line 15 : declare area variable and assign value of l * b
Line 17 : print area variable .