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 (2262)
 C++ interview questions  C++ Interview Questions (1118)
 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 ?    1378 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 ?    583 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 ?    346 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 ?    239 Yes 55 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 ?    197 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 ?    180 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 ?    116 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 ?    72 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
 
void main() { char c; while(c=getchar()!='\n') printf("%d",c); } o/p=11 why? Wipro 8
which one low Priority in c? a)=,b)++,c)==,d)+   10
how many argument we can pas in in a function CTS 25
int main() { int x = (2,3,4); int y = 9,10,11; printf("%d %d",x,y); } what would be the output? Wipro 7
Implement a function that returns the 5th element from the end in a singly linked list of integers in one pass. Microsoft 11
What are bit fields? What is their use? Adobe 2
How to print India by nested loop? I IN IND INDI INDIA NIIT 3
sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x? Subex 4
main() { clrscr(); } clrscr(); ME 6
What's the best way to declare and define global variables?   7
Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10 CMS 1
whitch value return void main?   10
 
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