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.

write a program to swap two numbers without using variables

+6 votes
asked Oct 10, 2019 by Goldwyn Gedela (190 points)

18 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;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...