Technologies
C
write a c program to swap two numbers
  CLanguage
  Wednesday, 21 Jun 2017
  1099
    0
Write a C Program for the following :
  • swapping of two numbers with out using third variable.
  • swapping of two numbers using third variable.

1. swapping of two numbers with out using third variable

 #including <stdio.h>
 main()
  {
    int a, b;
    printf("Enter any two positive integers : ");
    scanf("%d %d", &a, &b);
    printf("Before swapping : a = %d and b = %d \n ", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swapping : a = %d and b = %d", a, b);
  }
input : Enter any two positive integers : 2 5
output : Before swapping : a = 2 and b = 5
After swapping : a = 5 and b = 2

2. swapping of two numbers using third variable

  #including 
 main()
  {
    int a, b, t;
    printf("Enter any two positive integers : ");
    scanf("%d %d", &a, &b);
    printf("Before swapping : a = %d and b = %d \n ", a, b);
    t = a;
    a = b;
    b = t;
    printf("After swapping : a = %d and b = %d", a, b);
  }
input : Enter any two positive integers : 2 5
output : Before swapping : a = 2 and b = 5
After swapping : a = 5 and b = 2