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: Thecinobject reads data from the user via the keyboard.
Example:int age; cin >> age;
- Output using
cout: Thecoutobject 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)
- Which object is used for input in C++?
(a) cout | (b) cin | (c) printf | (d) scanf - 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 - Which manipulator is used to move the output to a new line?
(a) setw | (b) endl | (c) setprecision | (d) fixed - What does
cindo in C++?
(a) Reads input | (b) Displays output | (c) Formats output | (d) None - What header file is required for manipulators like
setw?
(a) iostream | (b) iomanip | (c) cmath | (d) cstdlib - Which of the following outputs the value of a variable?
(a) cout << variable; | (b) cin >> variable; | (c) print(variable); | (d) None - What will
setprecision(3)do?
(a) Format integers | (b) Set 3 decimal places | (c) Align text | (d) None - Which manipulator sets the width of an output field?
(a) setw | (b) endl | (c) setprecision | (d) fixed - 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 - 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
- What is the purpose of
cinandcoutin C++?
Answer:cinis used for input, andcoutis used for output in C++. - Write the syntax for using
setwin C++.
Answer:#include <iomanip> cout << setw(10) << "Hello";
- What does
setprecisiondo in C++?
Answer: It sets the number of decimal places for floating-point numbers. - Explain the use of the
endlmanipulator in C++.
Answer: Theendlmanipulator moves the cursor to the next line in the output. - 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
- Explain the difference between
cinandcoutwith examples. - Write a program to demonstrate the use of manipulators like
setw,setprecision, andendl. - Discuss the importance of input and output in programming with practical examples.
Post a Comment