write a program in C to swap two variables
Answers were Sorted based on User's Feedback
Answer / muzammil
#include <stdio.h>
#include <conio.h>
main()
{
int a,b;
printf("Enter the values: ");
scanf("%d%d",&a,&b);
printf("The values before swapping: %d %d",a,b);
a=a-(b=(-b+(a=a+b)));
printf("The values after swapping are: %d %d",a,b);
getch();
}
Is This Answer Correct ? | 14 Yes | 3 No |
Answer / r.aruna
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the a value");
scanf("%d",&a);
printf("Enter the b value");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a,b value",a,b);
getch();
}
Is This Answer Correct ? | 11 Yes | 3 No |
Answer / muzammil
Here are some LOGICS to swap two nmbrs
1) a+=b; b=a-b; a-=b;
2) a*=b; b=a/b; a/=b;
3) a=a^b; b=a^b; a=a^b;
Now Single line statements
4) a=a-(b=(-b+(a=a+b)));
5) a=a^(b=(b^(a=a^b)));
6) b=a+b-(a=b); This one was posted BY Senthil Thanks to him
7) b=a*b/(a=b); Dis is same as above one except for da
operators.
Is This Answer Correct ? | 5 Yes | 0 No |
Answer / priyamurugan
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf("\n BEFORE SWAPPING THE NOS");
printf("\n enter the a, b values");
scanf("%d %d",&a,&b);
swap(&a,&b);
printf("\n AFTER SWAPPING THE NOS");
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
return();
}
Is This Answer Correct ? | 5 Yes | 2 No |
Answer / srinivas
#include <stdio.h>
int main(void)
{
int a = 2, b = 5;
a = a + b;
b = a - b;
a = a - b;
printf("%d\t%d\n", a, b);
return 0;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / senthil mca sns
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the a value:");
scanf("%d",&a);
printf("Enter the b value:");
scanf("%d",&b);
b=a+b-(a=b);
printf("After Swapping a=%d,b=%d",a,b);
getch();
}
Is This Answer Correct ? | 3 Yes | 2 No |
Answer / anju
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
scanf("%d%d",&a,&b);
printf("before swapping value of a=%d and b=%d",a,b);
temp=x;
x=y;
y=temp;
printf("after swapping value of a=%d and b=%d",a,b);
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
24.what is a void pointer? 25.why arithmetic operation can’t be performed on a void pointer? 26.differentiate between const char *a; char *const a; and char const *a; 27.compare array with pointer? 28.what is a NULL pointer? 29.what does ‘segmentation violation’ mean? 30.what does ‘Bus Error’ mean? 31.Define function pointers? 32.How do you initialize function pointers? Give an example? 33.where can function pointers be used?
What is the diffrent between while and do while statement ?
void main(){ int a; a=1; while(a-->=1) while(a-->=0); printf("%d",a); }
x=2,y=6,z=6 x=y==z; printf(%d",x)
13 Answers Bharat, Cisco, HCL, TCS,
A stack can be implemented only using array?if not what is used?
Find Index of least significant bit set in an Integer. ex. int value is say 10001000 results should be 4.
What is difference between %d and %i in c?
write a program to swap bits in a character and return the value prototype of function char fun (char a, charb flag c) where fun returns a char, char a is a the value char b is the bit to be changed and flag c is the bit value for eg: x=fun(45,7,0) since 45 is 0010 0101 and ow x should contain the value 65 (0110 0101)
1 Answers Bosch, College School Exams Tests,
What is the output for the following program #include<stdio.h> main() { char a[5][5],flag; a[0][0]='A'; flag=((a==*a)&&(*a==a[0])); printf("%d\n",flag); }
Why does everyone say not to use scanf? What should I use instead?
Why clrscr is used in c?
Why calloc is better than malloc?