C++ Program for Swapping Two Numbers Without Using a Third Variable (तीसरे Variable का उपयोग किए बिना दो संख्याओं की अदला-बदली करने का C++ प्रोग्राम)

Swapping means exchanging the values of two variables. In this C++ program, we swap two numbers without using a third variable. We use arithmetic operators (+ and -) to exchange their values.

स्वैपिंग (Swapping) का अर्थ दो Variables के मानों को आपस में बदलना है। इस C++ प्रोग्राम में तीसरे Variable का उपयोग किए बिना Arithmetic Operators (+ और -) की सहायता से दो संख्याओं के मान बदले जाते हैं।

C++ Program

#include <iostream>
using namespace std;
int main()
{
float a, b;
cout << "Enter Two Numbers: ";
cin >> a >> b;
cout << "\nBefore Swapping:" << endl;
cout << "a = " << a << " and b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
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 (प्रोग्राम की व्याख्या)

Two variables a and b are declared using the float data type. The user enters two numbers, which are stored in these variables.

इस प्रोग्राम में float Data Type के दो Variables a और b घोषित किए गए हैं। उपयोगकर्ता द्वारा दर्ज की गई दो संख्याएँ इन Variables में संग्रहित होती हैं।

The swapping process is performed using three arithmetic statements:

a = a + b;
b = a - b;
a = a - b;

Step-by-step explanation:

  1. a = a + b; – The sum of both numbers is stored in a.

  2. b = a - b; – The original value of a is stored in b.

  3. a = a - b; – The original value of b is stored in a.

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

  1. a = a + b; – दोनों संख्याओं का योग Variable a में संग्रहित किया जाता है।

  2. b = a - b; – अब योग में से b का पुराना मान घटाने पर a का पुराना मान प्राप्त होता है और उसे b में रख दिया जाता है।

  3. a = a - b; – अब योग में से b का नया मान घटाने पर b का पुराना मान प्राप्त होता है और उसे a में रख दिया जाता है।

इस प्रकार बिना तीसरे Variable के दोनों संख्याओं के मान आपस में बदल जाते हैं।

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

Suppose a = 10 and b = 20.

StepStatementab
Initial—1020
1a = a + b;3020
2b = a - b;3010
3a = a - b;2010

This program demonstrates how two numbers can be swapped without using a third variable by applying arithmetic operators. It helps students understand arithmetic operations and variable assignment in C++.

यह प्रोग्राम Arithmetic Operators की सहायता से तीसरे Variable के बिना दो संख्याओं के मान बदलने की विधि समझाता है। इससे विद्यार्थियों को C++ में गणितीय संक्रियाओं और Variable Assignment की जानकारी मिलती है।

Note: This addition-and-subtraction method can cause numeric overflow for sufficiently large values. In modern C++, std::swap(a, b) is generally preferred for practical programming.

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)