ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Programming Languages >> C
 
 


 

 
 C interview questions  C Interview Questions (2254)
 C++ interview questions  C++ Interview Questions (1111)
 VC++ interview questions  VC++ Interview Questions (342)
 Delphi interview questions  Delphi Interview Questions (510)
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions (646)
Question
write a program to swap Two numbers without using temp variable.
 Question Submitted By :: C
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
a=a+b;
b=a-b;
a=a-b;
 
Is This Answer Correct ?    1376 Yes 156 No
Chandan
 
Answer
# 2
#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 ?    582 Yes 119 No
Harisharumalla
 
 
 
Answer
# 3
void swap(int *a,int *b)
{
if(*a == *b)
return;
*a^=*b;
*b^=*a;
*a^=*b;
}
 
Is This Answer Correct ?    456 Yes 195 No
Abhi
 
Answer
# 4
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 ?    345 Yes 137 No
Prasanna
 
Answer
# 5
# 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 ?    238 Yes 54 No
Guest
 
Answer
# 6
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 ?    195 Yes 57 No
Anantha
 
Answer
# 7
a=a+b;
b=a-b;
a=a-b;
printf("a %dand b %dis :",a,b);
 
Is This Answer Correct ?    179 Yes 42 No
Nagakishorebabu
 
Answer
# 8
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 ?    115 Yes 66 No
Ravi Saini
 
Answer
# 9
#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 ?    71 Yes 23 No
Sree
 
Answer
# 10
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 ?    71 Yes 26 No
Sweety
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
write the output of following code .. main() { static int a[]={10,20,30,40,50}; int *ptr=a; static int arr[2][2]={1,2,3,4}; char str[]="ABCD * 4#"; char *s=str+2; int i,j; for(i=0;i<5,i++) printf("%d",*ptr++); for(i=0;i<2;i++) for(j=0;j<2;j++) printf("%d\n",*(*(n+i)+j)); printf("%c\n%c\n%c\n",*(str+2),*s++,*--s); }   1
Finding first/last occurrence of a character in a string without using strchr( ) /strrchr( ) function.   2
how to find sum of 5 digits in C?   4
What is the most efficient way to count the number of bits which are set in a value?   4
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. Tcs 1
triangle number finding program... HCL 1
what does ‘segmentation violation’ mean?   1
which do you prefer C or Pascal?   1
int array[]={1,2,3,4,5,6,7,8}; #define SIZE (sizeof(array)/sizeof(int)) main() { if(-1<=SIZE) printf("1"); else printf("2"); }   1
Print the foll in C...eg when n=5 the o/p must b + + + + + + + + + + + + + + + + +   1
What is information technology.   1
Look at the Code: #include<string.h> void main() { char s1[]="abcd"; char s2[10]; char s3[]="efgh"; int i; clrscr(); i=strcmp(strcat(s3,ctrcpy(s2,s1))strcat(s3,"abcd")); printf("%d",i); } What will be the output? A)No output B) A Non Integer C)0 D) Garbage Accenture 6
 
For more C Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright © 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com