write a program to swap Two numbers without using temp variable.

Answers were Sorted based on User's Feedback



write a program to swap Two numbers without using temp variable...

Answer / sneha chorghade

#include<stdio.h>
void main()
{
int a=2,b=3;
printf("before swap the value is:::");
printf("a=%d\tb=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swap the value is:::");
printf("a=%d\tb=%d",a,b);
}

Is This Answer Correct ?    4 Yes 1 No

write a program to swap Two numbers without using temp variable...

Answer / anil joshi

b=(a*a)/a+(a=b)-a;

Is This Answer Correct ?    13 Yes 11 No

write a program to swap Two numbers without using temp variable...

Answer / ankit

#include<stdio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("enter two numbers");
scanf("%d%d",&a,&b);
swap(&a,&b);
/* b=(a+b)-(a=b); 1st method */
/* 2nd method
a=a+b;
b=a-b;
a=a-b; */
/* 3rd Method
a=a*b;
b=b/a;
a=a/b; */
/*4th Method
a=a^b;
b=b^a;
a=a^b; */
/* 5th Method
using pointer*/
printf("a=%d\nb=%d",a,b);
getch();
}
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}

Is This Answer Correct ?    2 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / ram thilak.p

#include<stdio.h>
#include<conio.h>

void main()
{
int a,b,*p1,*p2;
clrscr();
printf("\n\n\t Enter The Values Of A and B:");
scanf("%d %d",&a,&b);
*p1=a;
*p2=b;
b=*p1;
a=*p2;
printf("\n\n\t The Values Of Elements After Swapping Is:%d %d",a,b);
getch();
}

Is This Answer Correct ?    2 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / susa

printf("number present in a",&b);
printf("number present in b",&a);

Is This Answer Correct ?    3 Yes 1 No

write a program to swap Two numbers without using temp variable...

Answer / sneha

two ways to swap a number....
1st method
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=%d,b=%d",a,b)
getch()
}

2nd method
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=%d,b=%d",a,b)
getch()
}

Is This Answer Correct ?    2 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / ankit tiwari

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("enter the two number");
scanf("%d%d",&a,&b);
a=b-a;
b=b-a;
a=b+a;
printf("ais=%d",a);
printf("b is=%d",b);
getch();
}

Is This Answer Correct ?    2 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / gaurav sharma

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,b;
printf("enter the value of a : ");
scanf("%d",&a);
printf("enter the value of b : ");
scanf("%d",&b);
printf("Before swapping a is %d and b is %d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping of a and b:\na=%d\nb=%d",a,b);
getch();
}

Is This Answer Correct ?    2 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / niharika

class java
1.b=(a+b)-(a=b);

2.a^=b^=a^=b;

3.a=a+b;
b=a-b;
a=a-b;

4.a=a*b;
b=a/b;
a=a/b;


rohanraju143@gmail.com from NIT Waramgal

Is This Answer Correct ?    2 Yes 1 No

write a program to swap Two numbers without using temp variable...

Answer / yash paranjape

a=a^b;
b=a^b;
a=a^b;


i.e
a^=b;
b^=a;
a^=b;

more simplifie i.e in just one line
a^=b^=a^=b;

This also works fine

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More C Interview Questions

how to exchnage bits in a byte b7<-->b0 b6<-->b1 b5<-->b2 b4<-->b3 please mail me the code if any one know to rajeshmb4u@gmail.com

3 Answers   Honeywell, Huawei,


Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?

0 Answers   Celstream,


write an algorithm which can find the largest number among the given list using binary search ............... this was asked in the interview

2 Answers   Satyam, UNIS, Wipro,


Is it better to use a macro or a function?

0 Answers  


Write a c code segment using a for loop that calculates and prints the sum of the even integers from 2 to 30, inclusive?

4 Answers  






what is the c.

3 Answers   IBM, TCS,


A stack can be implemented only using array?if not what is used?

3 Answers   InterGlobal,


Design a program using an array that lists even numbers and odd numbers separately from the 12 numbers supplied by a user.

8 Answers  


what is the function of void main()?

8 Answers  


WHAT IS FLOAT?

3 Answers  


WAP to accept rollno,course name & marks of a student & display grade if total marks is above 200?

4 Answers  


which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT;

5 Answers   Assurgent, TCS,


Categories