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 (2253)
 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 ?    1374 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 ?    580 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 a program which counts a product of array elements lower than 10.   1
A B C D E F G F E D C B A A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A   2
C program to find frequency of each character in a text file?   4
Discuss similarities and differences of Multiprogramming OS and multiprocessing OS? TCS 4
Reverse a string word by word??   9
what are the advantage and disadvantage of recursion   4
how to find your architecture is LittleEndian or BigEndian?   1
what is the output for this question: main() { int i=1; printf("%d%d%d",i,i++,++i); }   9
explain about storage of union elements. Bosch 2
Difference between C and Embedded C?   1
How to write a C program to determine the smallest among three nos using conditional operator?   2
18)struct base {int a,b; base(); int virtual function1(); } struct derv1:base{ int b,c,d; derv1() int virtual function1(); } struct derv2 : base {int a,e; } base::base() { a=2;b=3; } derv1::derv1(){ b=5; c=10;d=11;} base::function1() {return(100); } derv1::function1() { return(200); } main() base ba; derv1 d1,d2; printf("%d %d",d1.a,d1.b) o/p is a)a=2;b=3; b)a=3; b=2; c)a=5; b=10; d)none 19) for the above program answer the following q's main() base da; derv1 d1; derv2 d2; printf("%d %d %d",da.function1(),d1.function1(),d2.function1 ()); o/p is a)100,200,200; b)200,100,200; c)200,200,100; d)none 20)struct { int x; int y; }abc; you can not access x by the following 1)abc-->x; 2)abc[0]-->x; abc.x; (abc)-->x; a)1,2,3 b)2&3 c)1&2 d)1,3,4   1
 
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