top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to swap the 2 number without using temp variable?

+2 votes
472 views
How to swap the 2 number without using temp variable?
posted Mar 9, 2016 by Spoorthi Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+2 votes
#include <stdio.h>
int main()
{
  int x = 10, y = 5;

  // Code to swap 'x' and 'y'
  x = x + y;  // x now becomes 15
  y = x - y;  // y becomes 10
  x = x - y;  // x becomes 5

  printf("After Swapping: x = %d, y = %d", x, y);

  return 0;
}
answer Mar 10, 2016 by Ashish Kumar Khanna
0 votes
#include<stdio.h>

int main() {
   int a, b;

   printf("\nEnter value for num1 & num2 : ");
   scanf("%d %d", &a, &b);

   a = a + b;
   b = a - b;
   a = a - b;

   printf("\nAfter swapping value of a : %d", a);
   printf("\nAfter swapping value of b : %d", b);

   return (0);
}

Output:

Enter value for num1 & num2 : 10 20

After swapping value of a : 20
After swapping value of b : 10
answer Mar 9, 2016 by Shivaranjini
0 votes
#include<stdio.h>
main()
{
    int a,b;
    printf("Enter a and b\n");
    scanf("%d %d",&a,&b);
    b= (a*b)/a=b;
    printf("Swapped value of a = %d\t b = %d\n",a,b);
}
answer Mar 10, 2016 by Chirag Gangdev
...