Notice: Undefined offset: 1886682 in /var/www/html/qa-external/qa-external-users.php on line 744
write a program to swap two numbers without using variables - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed write a program to swap two numbers without using variables

+13 votes
asked Oct 10, 2019 by Goldwyn Gedela (190 points)
closed Jun 9, 2024 by Admin
closed with the note: answered

19 Answers

0 votes
answered Oct 14, 2019 by karthik srinivas (190 points)
#include<stdio.h>

void main()

{

int a,b;

a=5;b=10;

a=a+b-(b=a);

printf("%d\n%d",a,b);

getch();

}
0 votes
answered Oct 16, 2019 by Vishnu Prashitha (140 points)
#include<stdio.h>

int main()

{

int a,b;

printf("\n Input integers to swap two integers (a,b)");

scanf("%d%d",&a,&b);

a=a+b;

a=a-b;

printf("\n After swapping ",a,b);

}
commented Oct 17, 2019 by anonymous
#include<stdio.h>

int main()

{

int x,y;

printf("\n Input integers to swap two integers (x,y)");

scanf("%d%d",&x,&y);

x=x*y;

y=x/y;

x=x/y;
printf("\n After swapping ",x,y);

}
0 votes
answered Dec 22, 2019 by anonymous
x = 5

y = 6

(x,y) = (y,x)

print ("X value is " + x + "Y value is " + y )
0 votes
answered Dec 28, 2019 by somasekhar (140 points)
//program without using third variable

#include<stdio.h>

main()

{

int a=10,b=20;

a=a+b;     // here a becomes a=30

b=a-b;      // here b=30-20=10        b=10

a=a-b;      //here  a=30-10=20        a=20   (since b became b=10 in before condition)

printf("after swaping values are %d and %d",a,b);

}
0 votes
answered Dec 30, 2019 by maheshbabu bezawada (140 points)
#include<stdio.h>

int main()

{

int a=5,b=10;

printf("before sorting %d %d\n",a,b);

printf("after sort   ");

a=a+b;

b=a-b;

a=a-b;

printf("%d %d ",a,b);

}
0 votes
answered Jun 13, 2020 by mahesh (470 points)
#include <stdio.h>
main()
{
    int a,b;
    printf("enter the value of a\n");
    scanf("%d",&a);
    printf("enter the value of b\n");
    scanf("%d",&b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("after swapping is %d\n%d\n",a,b);
   
   
}
0 votes
answered Jun 20, 2020 by JennyAnthony25 (140 points)
#include<stdio.h>

void main()

{ int a, b

printf("enter the first number:");

scanf("%d"; &a);

printf("enter the second number:")

scanf("%d",&b);

a = a+b;

b= a-b;

a=a-b;

printf(" the swapped numbers are :", a,b);

}
0 votes
answered Jun 23, 2020 by Edward Ibble (140 points)
#include <stdio.h>
#include <iostream>
#include <utility>

std::pair <int, int> SwapTheseNumbersForNoReason(int a, int b) {
    return {b, a};
}

int main()
{
    std::pair <int, int> swapped = SwapTheseNumbersForNoReason(69, 42);
    std::cout << "New first: " << swapped.first << " - New second : " << swapped.second << std::end;
    return 0;
}
–1 vote
answered Jun 8, 2024 by Tran Henry (120 points)

#include <bits/stdc++.h>
using namespace
std;

int main() {
    int a, b;
    cin >> a >> b;
    a = (a + b) - a;
    b = (a + b) - b;
    cout << a << " " << b;
    return 0;
}

commented Jun 9, 2024 by Peter Minarik (101,360 points)
This does not work. You should test your answer before posting it.

a = (a + b) - a; will set a to be the same as b
then b = (a + b) - b; will set b to be a, but at this point a is already b, so b will be set to b.

In the end, two b will be printed.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...