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 20
Before Swapping:
a = 10 and b = 20
After 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:

  1. c = a; – The value of a is copied into the third variable c.

  2. a = b; – The value of b is assigned to a.

  3. b = c; – The original value of a, stored in c, is assigned to b.

हिंदी में व्याख्या:

  1. c = a; – Variable a का मान तीसरे Variable c में सुरक्षित किया जाता है।

  2. a = b; – Variable b का मान Variable a में रख दिया जाता है।

  3. b = c; – Variable c में सुरक्षित पुराने a के मान को Variable b में रख दिया जाता है।

इस प्रकार दोनों Variables के मान आपस में बदल जाते हैं।

Swapping Process (स्वैपिंग प्रक्रिया)

StepStatementabc
Initial—1020—
1c = a;102010
2a = b;202010
3b = c;201010

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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Programming Methodologies and Data structures using C++ Language with OOP's Concepts

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)