Write a c program to swap two numbers.

C Program to swap two numbers without third variable

In this example, you will learn to swap two numbers in C programming using two different techniques.

Swap Numbers 

#include<stdio.h>
int main() {
      double first, second, temp;
      printf("Enter first number: ");
      scanf("%lf", &amp;first);
      printf("Enter second number: ");
      scanf("%lf", &amp;second);
      // Value of first is assigned to temp
      temp = first;
      // Value of second is assigned to first
      first = second;
      // Value of temp (initial value of first) is assigned to second
      second = temp;
      printf("\nAfter swapping, firstNumber = %.2lf\n", first);
      printf("After swapping, secondNumber = %.2lf", second);
      return 0;
}

Out Put

Enter first number: 1.20
Enter second number: 2.45
After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20

1 thought on “Write a c program to swap two numbers.”

Leave a Comment