write a program to swap Two numbers without using temp variable.

Answers were Sorted based on User's Feedback



write a program to swap Two numbers without using temp variable...

Answer / chandan

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

Is This Answer Correct ?    1400 Yes 158 No

write a program to swap Two numbers without using temp variable...

Answer / harisharumalla

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter the 2 numbers");
scanf("%d%d",&a,&b);
//swaping of 2 numbers without using temp variable
a=a+b;
b=a-b;
a=a-b;
/* or
a=a*b;
b=a/b;
a=a/b;
*/
printf("\n A = %d \n B = %d\n");
getch();
}

Is This Answer Correct ?    604 Yes 119 No

write a program to swap Two numbers without using temp variable...

Answer / abhi

void swap(int *a,int *b)
{
if(*a == *b)
return;
*a^=*b;
*b^=*a;
*a^=*b;
}

Is This Answer Correct ?    461 Yes 195 No

write a program to swap Two numbers without using temp variable...

Answer / prasanna

Swapping two variables in a single line with no temporary
variable..

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

so.. simple..

Prasanna. (prasanna7287@yahoo.co.in)

Is This Answer Correct ?    351 Yes 140 No

write a program to swap Two numbers without using temp variable...

Answer / guest

# include "stdio.h"
main()
{
int a,b;
printf("enter two numbers for swaping");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("a is %d",a);
printf("b is %d",b);
} output:- takea,b value is 2,3 and give answers is3,2

Is This Answer Correct ?    243 Yes 58 No

write a program to swap Two numbers without using temp variable...

Answer / anantha

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

if a=3 and b=5
then now b=a+b=8
and a=8-a=8-3=5,now a=5
b=8-a=8-5=3,now b=3

so, a=5 and b=3

Is This Answer Correct ?    198 Yes 59 No

write a program to swap Two numbers without using temp variable...

Answer / nagakishorebabu

a=a+b;
b=a-b;
a=a-b;
printf("a %dand b %dis :",a,b);

Is This Answer Correct ?    181 Yes 43 No

write a program to swap Two numbers without using temp variable...

Answer / ravi saini

void main()
{
int a,b;
printf("enter the two numbers");
scanf("%d%d",&a,&b);
a^=b^=a^=b;
printf("%d%d",a,b);//swapped no
}

Is This Answer Correct ?    119 Yes 66 No

write a program to swap Two numbers without using temp variable...

Answer / sree

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a &b);
a=a+b;
b=a-b;
a=a-b;
printf("The swapped values are:");
printf("a:%d",a);
printf("b:%d",b);
getch();
}

Is This Answer Correct ?    73 Yes 24 No

write a program to swap Two numbers without using temp variable...

Answer / sweety

main()
{
int a=2,b=3;
a=a+b;
b=a-b;
a=a-b;
printf("%d",&a);
printf("%d",&b);
getch();
}

Is This Answer Correct ?    72 Yes 27 No

Post New Answer

More C Interview Questions

Write a C program that computes the value ex by using the formula ex =1+x/1!+x2/2!+x3+3!+………….

1 Answers  


What the different types of arrays in c?

0 Answers  


Please provide question papers of NATIONAL INFORMATICS CENTRE for Scientific officer

0 Answers  


What is const keyword in c?

0 Answers  


What is pointers in c?

0 Answers  






cin.ignore(80, _ _);This statement a) ignores all input b) ignores the first 80 characters in the input c) ignores all input till end-of-line d) iteration

0 Answers  


I have written a pro*C program to fetch data from the cursor. where in i have used the concept of BULK FETCH.... each FETCH statement is taking lots of time to fetch specified number of rows at...

0 Answers  


Why enum is used in c?

0 Answers  


what is the difference between 123 and 0123 in c?

0 Answers  


Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6)

0 Answers   InterGraph,


explain what is an endless loop?

0 Answers  


Why is sprintf unsafe?

0 Answers  


Categories