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 |
"C" language developed by "Dennis Ritchie" at AT & T. his remarks are a) too general, too abstract b) could deal with only specific problems c) lost generality of BCPL and B restored d) no remarks
Is c is a high level language?
what is difference between procedural language and functional language ?
How can I allocate arrays or structures bigger than 64K?
Explain how can I right-justify a string?
C program execution always begins with a) #include b) comment (/*-------*/) c) main() d) declaration instructions
What kind of sorting is this? SORT (k,n) 1.[Loop on I Index] repeat thru step2 for i=1,2,........n-1 2.[For each pass,get small value] min=i; repeat for j=i+1 to N do { if K[j]<k[min] min=j; } temp=K[i];K[i]=K[min];K[min]=temp; 3.[Sorted Values will be returned] A)Bubble Sort B)Quick Sort C)Selection Sort D)Merge Sort
struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeof" operator??
what do you mean by defining a variable in our c code?
Why do we use null pointer?
What are the advantages of external class?
while running a program, i got the msg that press return key to exit.what that mean in C as there are no such options as far i know.