C++ Input and Output Practice Questions with Solutions

Input and Output (I/O) operations are among the most fundamental concepts in C++ programming. Every interactive program depends on taking input from the user, processing it, and displaying meaningful output. Whether you’re developing a calculator, banking system, student management software, or competitive programming solution, understanding input and output is essential.

In C++, the cin object is used to receive input from the keyboard, while cout is used to display output on the screen. These objects are part of the <iostream> library and are used in almost every C++ program. C++ Input and Output practice questions with solutions help to understand the concepts.

This chapter contains practical input and output programming questions designed to strengthen your understanding of user input, formatted output, multiple inputs, character input, and basic calculations.

Each practice question includes:

  • Problem Statement
  • Complete C++ Solution
  • Sample Input
  • Sample Output
  • Explanation
  • Concepts Covered

Let’s start solving real-world C++ Input and Output programming questions.


1. C++ Program to Input and Display an Integer

Problem Statement

Write a C++ program to accept an integer from the user and display it.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int number;

    cout << "Enter an integer: ";
    cin >> number;

    cout << "You entered: " << number;

    return 0;
}

Sample Input

Enter an integer: 45

Sample Output

You entered: 45

Explanation

The cin object receives an integer entered by the user and stores it in the variable number. The cout object then displays the stored value.

Concepts Covered

  • cin
  • cout
  • Integer Input
  • Variable Declaration

2. C++ Program to Input and Display a Floating-Point Number

Problem Statement

Write a C++ program to accept a decimal number from the user and display it.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float number;

    cout << "Enter a decimal number: ";
    cin >> number;

    cout << "Entered Number = " << number;

    return 0;
}

Sample Input

Enter a decimal number: 25.75

Sample Output

Entered Number = 25.75

Explanation

The program accepts a floating-point number using cin and prints it using cout.

Concepts Covered

  • Float Data Type
  • User Input
  • Output Statement

3. C++ Program to Input and Display a Character

Problem Statement

Write a C++ program to input a character and display it.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    char ch;

    cout << "Enter a character: ";
    cin >> ch;

    cout << "Character Entered = " << ch;

    return 0;
}

Sample Input

Enter a character: A

Sample Output

Character Entered = A

Explanation

The program stores a single character entered by the user and displays it on the screen.

Concepts Covered

  • Character Data Type
  • Character Input
  • User Interaction

4. C++ Program to Input and Display Your Name

Problem Statement

Write a C++ program to input your name and display a welcome message.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;

    cout << "Enter your name: ";
    getline(cin, name);

    cout << "Welcome, " << name;

    return 0;
}

Sample Input

Enter your name: Rahul Sharma

Sample Output

Welcome, Rahul Sharma

Explanation

The getline() function is used to read an entire line, including spaces, making it ideal for full names.

Concepts Covered

  • String Data Type
  • getline()
  • User Input
  • Formatted Output

5. C++ Program to Input Student Details

Problem Statement

Write a C++ program to accept a student’s name, age, and percentage, then display all the details.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    int age;
    float percentage;

    cout << "Enter Student Name: ";
    getline(cin, name);

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

    cout << "Enter Percentage: ";
    cin >> percentage;

    cout << "\n----- Student Details -----" << endl;
    cout << "Name       : " << name << endl;
    cout << "Age        : " << age << endl;
    cout << "Percentage : " << percentage << "%" << endl;

    return 0;
}

Sample Input

Enter Student Name: Amit Kumar
Enter Age: 20
Enter Percentage: 87.5

Sample Output

----- Student Details -----
Name       : Amit Kumar
Age        : 20
Percentage : 87.5%

Explanation

The program accepts different types of user input (string, int, and float) and displays them in a formatted manner.

Concepts Covered

  • Multiple User Inputs
  • String
  • Integer
  • Float
  • Formatted Output
  • getline()
  • cin
  • cout

6. C++ Program to Input Two Numbers and Display Their Sum

Problem Statement

Write a C++ program to accept two integers from the user and display their sum.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    int num1, num2;

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

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

    cout << "Sum = "
         << num1 + num2;

    return 0;
}

Sample Input

Enter first number: 45
Enter second number: 30

Sample Output

Sum = 75

Explanation

The program accepts two integer values from the user using cin, adds them together, and displays the result using cout.

Concepts Covered

  • Integer Input
  • Multiple Variables
  • Addition
  • Output Statement

7. C++ Program to Input Three Numbers and Find Their Average

Problem Statement

Write a C++ program to input three numbers and calculate their average.

C++ Solution

#include &lt;iostream>
using namespace std;

int main()
{
    float num1, num2, num3;
    float average;

    cout &lt;&lt; "Enter first number: ";
    cin >> num1;

    cout &lt;&lt; "Enter second number: ";
    cin >> num2;

    cout &lt;&lt; "Enter third number: ";
    cin >> num3;

    average = (num1 + num2 + num3) / 3;

    cout &lt;&lt; "Average = "
         &lt;&lt; average;

    return 0;
}

Sample Input

Enter first number: 25
Enter second number: 35
Enter third number: 40

Sample Output

Average = 33.3333

Explanation

The program stores three floating-point values, calculates their average, and displays the result.

Concepts Covered

  • Float Variables
  • Arithmetic Operations
  • Average Calculation
  • User Input

8. C++ Program to Input Employee Details

Problem Statement

Write a C++ program to accept an employee’s name, employee ID, designation, and salary, then display all the details.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    string designation;
    int employeeId;
    float salary;

    cout << "Enter Employee Name: ";
    getline(cin, name);

    cout << "Enter Employee ID: ";
    cin >> employeeId;

    cin.ignore();

    cout << "Enter Designation: ";
    getline(cin, designation);

    cout << "Enter Salary: ";
    cin >> salary;

    cout << "\n----- Employee Details -----" << endl;
    cout << "Name        : " << name << endl;
    cout << "Employee ID : " << employeeId << endl;
    cout << "Designation : " << designation << endl;
    cout << "Salary      : Rs. " << salary;

    return 0;
}

Sample Input

Enter Employee Name: Rahul Sharma
Enter Employee ID: 101
Enter Designation: Software Developer
Enter Salary: 65000

Sample Output

----- Employee Details -----
Name        : Rahul Sharma
Employee ID : 101
Designation : Software Developer
Salary      : Rs. 65000

Explanation

This program demonstrates how to take different types of user input, including integers, floating-point values, and strings containing spaces.

Concepts Covered

  • getline()
  • cin
  • cin.ignore()
  • String Input
  • Formatted Output

9. C++ Program to Input Full Address

Problem Statement

Write a C++ program to input a complete address from the user and display it.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string address;

    cout << "Enter your address: ";
    getline(cin, address);

    cout << "\nYour Address is:\n";
    cout << address;

    return 0;
}

Sample Input

Enter your address:
A-66, Sector-7, Dwarka, New Delhi

Sample Output

Your Address is:
A-66, Sector-7, Dwarka, New Delhi

Explanation

Since an address usually contains spaces and commas, getline() is used instead of cin.

Concepts Covered

  • String Handling
  • getline()
  • Multi-word Input
  • Output Formatting

10. C++ Program to Input Student Marks and Calculate Total Marks

Problem Statement

Write a C++ program to input marks of five subjects and calculate the total marks obtained.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    float math, science, english, computer, hindi;
    float total;

    cout << "Enter marks of Mathematics: ";
    cin >> math;

    cout << "Enter marks of Science: ";
    cin >> science;

    cout << "Enter marks of English: ";
    cin >> english;

    cout << "Enter marks of Computer: ";
    cin >> computer;

    cout << "Enter marks of Hindi: ";
    cin >> hindi;

    total = math + science + english + computer + hindi;

    cout << "\nTotal Marks = "
         << total;

    return 0;
}

Sample Input

Mathematics : 85
Science     : 90
English     : 80
Computer    : 95
Hindi       : 75

Sample Output

Total Marks = 425

Explanation

The program accepts marks for five subjects individually and calculates the total by adding all the values together.

Concepts Covered

  • Multiple User Inputs
  • Float Variables
  • Arithmetic Operations
  • Total Calculation
  • Input and Output

11. C++ Program to Input Two Characters and Display Them

Problem Statement

Write a C++ program to accept two characters from the user and display them.

C++ Solution

#include <iostream>
using namespace std;

int main()
{
    char firstChar, secondChar;

    cout << "Enter first character: ";
    cin >> firstChar;

    cout << "Enter second character: ";
    cin >> secondChar;

    cout << "\nFirst Character  = "
         << firstChar << endl;

    cout << "Second Character = "
         << secondChar;

    return 0;
}

Sample Input

Enter first character: A
Enter second character: Z

Sample Output

First Character  = A
Second Character = Z

Explanation

This program demonstrates how to take multiple character inputs using cin.

Concepts Covered

  • Character Data Type
  • Multiple Inputs
  • User Interaction

12. C++ Program to Input Roll Number, Name, and Section

Problem Statement

Write a C++ program to input a student’s roll number, name, and section, then display the details.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int rollNumber;
    string name;
    char section;

    cout << "Enter Roll Number: ";
    cin >> rollNumber;

    cin.ignore();

    cout << "Enter Student Name: ";
    getline(cin, name);

    cout << "Enter Section: ";
    cin >> section;

    cout << "\n----- Student Information -----" << endl;
    cout << "Roll Number : " << rollNumber << endl;
    cout << "Name        : " << name << endl;
    cout << "Section     : " << section << endl;

    return 0;
}

Sample Input

Enter Roll Number: 102
Enter Student Name: Aman Gupta
Enter Section: B

Sample Output

----- Student Information -----
Roll Number : 102
Name        : Aman Gupta
Section     : B

Explanation

The program accepts different data types from the user and displays them in a structured format.

Concepts Covered

  • Integer Input
  • Character Input
  • String Input
  • getline()
  • cin.ignore()

13. C++ Program to Input Product Details

Problem Statement

Write a C++ program to input product name, product ID, quantity, and price, then display the complete product details.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string productName;
    int productId;
    int quantity;
    float price;

    cout << "Enter Product Name: ";
    getline(cin, productName);

    cout << "Enter Product ID: ";
    cin >> productId;

    cout << "Enter Quantity: ";
    cin >> quantity;

    cout << "Enter Price: ";
    cin >> price;

    cout << "\n----- Product Details -----" << endl;
    cout << "Product Name : " << productName << endl;
    cout << "Product ID   : " << productId << endl;
    cout << "Quantity     : " << quantity << endl;
    cout << "Price        : Rs. " << price << endl;

    return 0;
}

Sample Input

Enter Product Name: Wireless Mouse
Enter Product ID: 5001
Enter Quantity: 10
Enter Price: 799

Sample Output

----- Product Details -----
Product Name : Wireless Mouse
Product ID   : 5001
Quantity     : 10
Price        : Rs. 799

Explanation

This program demonstrates how multiple values of different data types can be collected from the user.

Concepts Covered

  • Multiple Inputs
  • String Handling
  • Integer Variables
  • Float Variables
  • Formatted Output

14. C++ Program to Input City and State Name

Problem Statement

Write a C++ program to input a city name and state name, then display both.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string city;
    string state;

    cout << "Enter City: ";
    getline(cin, city);

    cout << "Enter State: ";
    getline(cin, state);

    cout << "\nLocation Details" << endl;
    cout << "City  : " << city << endl;
    cout << "State : " << state << endl;

    return 0;
}

Sample Input

Enter City: New Delhi
Enter State: Delhi

Sample Output

Location Details
City  : New Delhi
State : Delhi

Explanation

The getline() function allows the user to enter complete names containing spaces.

Concepts Covered

  • String Input
  • Multi-word Input
  • Output Formatting

15. C++ Program to Input User Details and Display a Profile Card

Problem Statement

Write a C++ program to accept user details and display them as a formatted profile card.

C++ Solution

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    int age;
    string city;
    string profession;

    cout << "Enter Name: ";
    getline(cin, name);

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

    cin.ignore();

    cout << "Enter City: ";
    getline(cin, city);

    cout << "Enter Profession: ";
    getline(cin, profession);

    cout << "\n========== USER PROFILE ==========" << endl;
    cout << "Name       : " << name << endl;
    cout << "Age        : " << age << endl;
    cout << "City       : " << city << endl;
    cout << "Profession : " << profession << endl;
    cout << "==================================" << endl;

    return 0;
}

Sample Input

Enter Name: Rohan Sharma
Enter Age: 24
Enter City: Jaipur
Enter Profession: Software Engineer

Sample Output

========== USER PROFILE ==========
Name       : Rohan Sharma
Age        : 24
City       : Jaipur
Profession : Software Engineer
==================================

Explanation

This program combines different types of user input to create a clean and formatted profile card, demonstrating practical usage of cin, getline(), and output formatting.

Concepts Covered

  • User Input
  • cin
  • getline()
  • cin.ignore()
  • String Handling
  • Formatted Output

Chapter Summary

In this chapter, you learned how to use C++ input and output statements to build interactive programs. You practiced accepting integers, floating-point numbers, characters, strings, and multiple values from the user. You also learned when to use cin, cout, getline(), and cin.ignore() for handling different types of input correctly. These concepts are essential for creating user-friendly applications and form the basis for solving real-world programming problems.


Key Takeaways

  • cin is used to receive input from the user.
  • cout is used to display output on the screen.
  • getline() reads an entire line, including spaces.
  • cin.ignore() clears the input buffer before using getline().
  • Different data types require different input methods.
  • Formatted output improves program readability.
  • Combining multiple inputs helps create real-world applications.
  • Input and output operations are fundamental to all C++ programs.
  • Proper use of strings and characters is important for handling textual data.
  • Mastering I/O operations prepares you for conditional statements, loops, and functions.

Frequently Asked Questions (FAQs)

1. What is the difference between cin and cout?

cin is used to receive input from the keyboard, while cout is used to display output on the screen.


2. Why do we use getline() instead of cin?

getline() reads an entire line, including spaces, whereas cin stops reading at the first whitespace.


3. What is the purpose of cin.ignore()?

cin.ignore() clears the leftover newline character from the input buffer before calling getline().


4. Can cin read multiple values at once?

Yes. You can use:

cin >> a >> b >> c;

to read multiple values in a single statement.


5. Which header file is required for input and output in C++?

The <iostream> header file provides cin and cout.


6. Why is formatted output important?

Formatted output makes programs easier to read and improves the user experience.


7. Can getline() read numbers?

getline() reads text as a string. Numeric values should typically be read using cin unless you plan to convert the string later.


8. Why should beginners practice input and output programs?

Input and output operations are used in almost every C++ application. Mastering them is essential before learning conditionals, loops, functions, arrays, and object-oriented programming.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top