write a program to swap Two numbers without using temp variable.
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Write a C program that computes the value ex by using the formula ex =1+x/1!+x2/2!+x3+3!+………….
What the different types of arrays in c?
Please provide question papers of NATIONAL INFORMATICS CENTRE for Scientific officer
What is const keyword in c?
What is pointers in c?
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
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...
Why enum is used in c?
what is the difference between 123 and 0123 in c?
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)
explain what is an endless loop?
Why is sprintf unsafe?