Day 39 – Chapter 5: Operators in C++
Class 11 Computer Science
What are Operators in C++? (C++ में ऑपरेटर क्या हैं?)
Operators in C++ are symbols or keywords used to perform specific operations on variables and values. They allow the manipulation and comparison of data.
C++ में ऑपरेटर प्रतीक या कीवर्ड होते हैं जिनका उपयोग वेरिएबल और मानों पर विशेष संचालन करने के लिए किया जाता है। ये डेटा को हेरफेर और तुलना करने की अनुमति देते हैं।
Types of Operators in C++ (C++ में ऑपरेटर के प्रकार)
Common operators in C++ include arithmetic, relational, and logical operators.
C++ में सामान्य ऑपरेटर में अंकगणितीय, संबंधात्मक, और तार्किक ऑपरेटर शामिल हैं।
Arithmetic Operators (अंकगणितीय ऑपरेटर)
These operators are used to perform mathematical calculations.
इन ऑपरेटर का उपयोग गणितीय गणनाएँ करने के लिए किया जाता है।
| Operator | Description (विवरण) | Example |
|---|---|---|
| + | Addition (योग) | c = a + b; |
| - | Subtraction (घटाव) | c = a - b; |
| * | Multiplication (गुणा) | c = a * b; |
| / | Division (भाग) | c = a / b; |
| % | Modulus (शेषफल) | c = a % b; |
Relational Operators (संबंधात्मक ऑपरेटर)
These operators are used to compare two values. The result is either true or false.
इन ऑपरेटर का उपयोग दो मानों की तुलना करने के लिए किया जाता है। परिणाम सही या गलत होता है।
| Operator | Description (विवरण) | Example |
|---|---|---|
| == | Equal to (समान) | a == b; |
| != | Not equal to (असमान) | a != b; |
| < | Less than (छोटा) | a < b; |
| > | Greater than (बड़ा) | a > b; |
| <= | Less than or equal to (छोटा या बराबर) | a <= b; |
| >= | Greater than or equal to (बड़ा या बराबर) | a >= b; |
Logical Operators (तार्किक ऑपरेटर)
These operators are used to perform logical operations, typically on boolean values.
इन ऑपरेटर का उपयोग तार्किक संचालन करने के लिए किया जाता है, आमतौर पर बूलियन मानों पर।
| Operator | Description (विवरण) | Example |
|---|---|---|
| && | Logical AND (तार्किक AND) | (a > 0) && (b > 0) |
| || | Logical OR (तार्किक OR) | (a > 0) || (b < 0) |
| ! | Logical NOT (तार्किक NOT) | !(a > b) |
Examples (उदाहरण)
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 5;
// Arithmetic Operators
cout << "Addition: " << (a + b) << endl;
cout << "Multiplication: " << (a * b) << endl;
// Relational Operators
cout << "a is greater than b: " << (a > b) << endl;
// Logical Operators
cout << "Both a and b are positive: " << ((a > 0) && (b > 0)) << endl;
return 0;
}
Practice Questions
Multiple Choice Questions (MCQs)
- Which operator is used for addition?
(a) - | (b) + | (c) * | (d) / - What is the result of
10 % 3?
(a) 1 | (b) 3 | (c) 10 | (d) None - What does the == operator do?
(a) Assigns a value | (b) Compares values for equality | (c) Checks for inequality | (d) None - Which operator checks if both conditions are true?
(a) || | (b) && | (c) ! | (d) None - What is the output of
!(10 < 5)?
(a) true | (b) false | (c) 10 | (d) None - What is the precedence of arithmetic operators?
(a) *, /, +, - | (b) +, -, *, / | (c) *, +, -, / | (d) None - Which operator is used for multiplication?
(a) * | (b) + | (c) / | (d) % - What is the logical NOT operator?
(a) && | (b) || | (c) ! | (d) None - What will
cout << (5 > 3)output?
(a) true | (b) 1 | (c) 0 | (d) None - What does the modulus operator (%) return?
(a) Quotient | (b) Remainder | (c) Product | (d) None
Answers to MCQs:
1: (b), 2: (a), 3: (b), 4: (b), 5: (a), 6: (a), 7: (a), 8: (c), 9: (b), 10: (b)
Short Answer Questions
- What is the difference between arithmetic and relational operators?
Answer: Arithmetic operators perform mathematical operations like addition and subtraction, while relational operators compare two values and return a boolean result. - Explain the use of logical operators in C++.
Answer: Logical operators are used to combine or invert boolean expressions. For example,&&checks if both conditions are true,||checks if at least one condition is true, and!inverts a condition. - Write the syntax for using arithmetic operators in C++.
Answer:int a = 10, b = 5; int sum = a + b; int difference = a - b; int product = a * b; int quotient = a / b; int remainder = a % b; - What does the modulus operator (%) do in C++?
Answer: The modulus operator returns the remainder when one number is divided by another. For example,10 % 3returns1. - Write a program to demonstrate the use of relational operators.
Answer:#include <iostream> using namespace std; int main() { int a = 10, b = 5; cout << "a > b: " << (a > b) << endl; cout << "a == b: " << (a == b) << endl; cout << "a <= b: " << (a <= b) << endl; return 0; }
Long Answer Questions
- Explain the types of operators in C++ with examples.
- Write a program to demonstrate the use of arithmetic, relational, and logical operators.
- Discuss the precedence and associativity of operators in C++.
Post a Comment