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

+2 votes
answered Oct 11, 2019 by rohan ag (1,150 points)
#include<stdio.h>

main()

{

int a=10,b=20;

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

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

}
commented Oct 13, 2019 by KOTRAGOUDA
20 10  a value will be assign to b then the value of the b will be updated to 10,after that the a value will be calculated
+1 vote
answered Oct 11, 2019 by Harsha (360 points)
without third variable we can swap but without any variables we cannt swap in c
0 votes
answered Oct 11, 2019 by Eria Mutasa (140 points)
#include<stdio.h>

int main()

{

   int first variable ;

int second variable;

printf("change the position of the first variable and second variable with each other");

return 0;

}

;
commented Jun 13, 2020 by xDELLx (6,200 points)
Bro you are awesome
0 votes
answered Oct 12, 2019 by Rohan Ghobade (520 points)
0 votes
answered Oct 12, 2019 by anonymous
#include<stdio.h>

#include<conio.h>

int main()

{

int a=1;

int b=2;

a+b

a=(a+b)-a;

b=(a+b)-a;
0 votes
answered Oct 12, 2019 by saiteja (270 points)
#include <stdio.h>
int main()
{
    int a,b,temp, *p, *q;
    p=&a;
    q=&b;
    printf("Enter a and b:\n");
    scanf("%d%d", &a,&b);
    temp=*p;
    *p=*q;
    *q=temp;
    printf("a=%d b=%d",*p,*q);
}
0 votes
answered Oct 13, 2019 by Anand Sharma (150 points)
#include <stdio.h>

int main()
{
    int a[2]={1,3};
    printf("\n::No.s before swap::\n");
    for(int i=0;i<2;i++)
    {
        printf("%d ",a[i]);
        printf("and ");
    }
    printf("\n::No.s after swap::\n");
    for(int i=1;i>=0;i--)
    {
        printf("%d ",a[i]);
        printf("and ");
    }

    return 0;
}
0 votes
answered Oct 13, 2019 by munna (140 points)
#include<stdio.h>

main()

{

int x,y;

x=10,y=20;

x=x^y;;

y=x^y;

x=x^y;

printf("%d  %d",x,y);

}
commented Oct 23, 2019 by Subhadeep Deb Roy
check the 6th line and remove one semicolon ( ; ), else it is perfect
0 votes
answered Oct 13, 2019 by prince
#include<stdio.h>

void main()

{

int a,b;

printf("enter numbers a b:\n");

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

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

a=a+b;

b=a-b;

a=a-b;

printf("after swaping:\n");

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

}
0 votes
answered Oct 13, 2019 by Abhishek Kumar
#include<stdio.h>

 int

{
  
int a, b;
   

printf ("enter numbers a,b:\n");
   

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

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

a = a + b;
   

b = a - b;
   

a = a - b;
   

printf ("after swaping:\n");
 

printf ("%d %d\n", a, b)
 
}
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.
...