C++ Program for Swapping Two Numbers Using a Third Variable (तीसरे Variable की सहायता से दो संख्याओं की अदला-बदली करने का C++ प्रोग्राम)
Swapping means exchanging the values of two variables. In this C++ program, we use a third variable to swap the values of two numbers.
स्वैपिंग (Swapping) का अर्थ दो Variables के मानों को आपस में बदलना है। इस C++ प्रोग्राम में दो संख्याओं के मानों को बदलने के लिए तीसरे Variable का उपयोग किया गया है।
C++ Program
#include <iostream>using namespace std;int main(){float a, b, c;cout << "Enter Two Numbers: ";cin >> a >> b;cout << "\nBefore Swapping:" << endl;cout << "a = " << a << " and b = " << b << endl;c = a;a = b;b = c;cout << "\nAfter Swapping:" << endl;cout << "a = " << a << " and b = " << b << endl;return 0;}
Sample Output (उदाहरण आउटपुट)
Enter Two Numbers: 10 20Before Swapping:a = 10 and b = 20After Swapping:a = 20 and b = 10
Explanation of the Program (प्रोग्राम की व्याख्या)
Three variables a, b and c are declared as float data types. The user enters two numbers, which are stored in variables a and b.
इस प्रोग्राम में a, b और c नाम के तीन Variables को float Data Type के रूप में घोषित किया गया है। उपयोगकर्ता द्वारा दी गई दो संख्याएँ a और b में संग्रहित होती हैं।
The swapping process is performed using the following three statements:
c = a;a = b;b = c;
Step-by-step explanation:
c = a;– The value ofais copied into the third variablec.a = b;– The value ofbis assigned toa.b = c;– The original value ofa, stored inc, is assigned tob.
हिंदी में व्याख्या:
c = a;– Variableaका मान तीसरे Variablecमें सुरक्षित किया जाता है।a = b;– Variablebका मान Variableaमें रख दिया जाता है।b = c;– Variablecमें सुरक्षित पुरानेaके मान को Variablebमें रख दिया जाता है।
इस प्रकार दोनों Variables के मान आपस में बदल जाते हैं।
Swapping Process (स्वैपिंग प्रक्रिया)
| Step | Statement | a | b | c |
|---|---|---|---|---|
| Initial | — | 10 | 20 | — |
| 1 | c = a; | 10 | 20 | 10 |
| 2 | a = b; | 20 | 20 | 10 |
| 3 | b = c; | 20 | 10 | 10 |
This program demonstrates how two numbers can be swapped using a third temporary variable in C++. This method is simple and commonly used to understand variable assignment and swapping logic.
यह प्रोग्राम C++ में तीसरे अस्थायी Variable की सहायता से दो संख्याओं के मान बदलने की सरल विधि समझाता है। इससे Variable Assignment और Swapping की अवधारणा स्पष्ट होती है।
Comments
Post a Comment