Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 38: Input and Output in C++ – Basics, Functions, and Examples | Class 11

Learn about input and output in C++ in Day 38 of Class 11 Computer Science. Covers cin, cout, and examples for easy understanding.

Day 38 – Chapter 5: Input and Output in C++

Class 11 Computer Science

Introduction to Input and Output in C++ (C++ में इनपुट और आउटपुट का परिचय)

Input refers to providing data to a program, while output refers to displaying the result or information to the user. In C++, the cin and cout objects are used for input and output operations, respectively.
इनपुट का मतलब प्रोग्राम को डेटा प्रदान करना है, जबकि आउटपुट का मतलब उपयोगकर्ता को परिणाम या जानकारी दिखाना है। C++ में, इनपुट और आउटपुट संचालन के लिए क्रमशः cin और cout ऑब्जेक्ट का उपयोग किया जाता है।

Basic Syntax for Input and Output (इनपुट और आउटपुट का बेसिक सिंटैक्स)

  • Input using cin: The cin object reads data from the user via the keyboard.
    Example:
    int age;
    cin >> age;
    
  • Output using cout: The cout object displays data to the console.
    Example:
    int age = 18;
    cout << "Age: " << age;
    

Using Manipulators for Output Formatting (आउटपुट फॉर्मेटिंग के लिए मैनिपुलेटर्स का उपयोग)

Manipulators are used to format the output. Common manipulators include endl, setw, setprecision, and fixed.
मैनिपुलेटर्स का उपयोग आउटपुट को फॉर्मेट करने के लिए किया जाता है। सामान्य मैनिपुलेटर्स में endl, setw, setprecision, और fixed शामिल हैं।

  • endl: Moves the output cursor to the next line.
    Example:
    cout << "Hello" << endl << "World";
    
  • setw: Sets the width of the output field.
    Example:
    #include <iomanip>
    cout << setw(10) << "Hello";
    
  • setprecision: Sets the number of decimal places for floating-point numbers.
    Example:
    #include <iomanip>
    cout << setprecision(3) << 3.14159;
    

Examples of Input and Output Operations (इनपुट और आउटपुट संचालन के उदाहरण)

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

int main() {
    int age;
    float pi = 3.14159;

    // Input
    cout << "Enter your age: ";
    cin >> age;

    // Output
    cout << "Your age is: " << age << endl;

    // Formatted Output
    cout << "Pi to 3 decimal places: " << fixed << setprecision(3) << pi << endl;

    return 0;
}

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which object is used for input in C++?
    (a) cout | (b) cin | (c) printf | (d) scanf
  2. What is the output of the following code?
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello" << endl << "World";
        return 0;
    }
            
    (a) HelloWorld | (b) Hello World | (c) Hello\nWorld | (d) None
  3. Which manipulator is used to move the output to a new line?
    (a) setw | (b) endl | (c) setprecision | (d) fixed
  4. What does cin do in C++?
    (a) Reads input | (b) Displays output | (c) Formats output | (d) None
  5. What header file is required for manipulators like setw?
    (a) iostream | (b) iomanip | (c) cmath | (d) cstdlib
  6. Which of the following outputs the value of a variable?
    (a) cout << variable; | (b) cin >> variable; | (c) print(variable); | (d) None
  7. What will setprecision(3) do?
    (a) Format integers | (b) Set 3 decimal places | (c) Align text | (d) None
  8. Which manipulator sets the width of an output field?
    (a) setw | (b) endl | (c) setprecision | (d) fixed
  9. What is the output of this code?
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main() {
        cout << setprecision(2) << 3.14159;
        return 0;
    }
            
    (a) 3.14159 | (b) 3.14 | (c) 3.1 | (d) None
  10. What is the purpose of fixed?
    (a) Sets a fixed value | (b) Fixes alignment | (c) Ensures decimal formatting | (d) None

Answers to MCQs:

1: (b), 2: (b), 3: (b), 4: (a), 5: (b), 6: (a), 7: (b), 8: (a), 9: (b), 10: (c)

Short Answer Questions

  1. What is the purpose of cin and cout in C++?
    Answer: cin is used for input, and cout is used for output in C++.
  2. Write the syntax for using setw in C++.
    Answer:
    #include <iomanip>
    cout << setw(10) << "Hello";
    
  3. What does setprecision do in C++?
    Answer: It sets the number of decimal places for floating-point numbers.
  4. Explain the use of the endl manipulator in C++.
    Answer: The endl manipulator moves the cursor to the next line in the output.
  5. Write a program to take input of a name and display it.
    Answer:
    #include <iostream>
    using namespace std;
    
    int main() {
        string name;
        cout << "Enter your name: ";
        cin >> name;
        cout << "Your name is: " << name;
        return 0;
    }
    

Long Answer Questions

  1. Explain the difference between cin and cout with examples.
  2. Write a program to demonstrate the use of manipulators like setw, setprecision, and endl.
  3. Discuss the importance of input and output in programming with practical examples.

Post a Comment