Swapping Two Numbers in C Language

 Swapping Two Numbers in C Language

Introduction

Swapping values of two variables is a common task. This program demonstrates swapping using a temporary variable.


#include <stdio.h> int main() { int a, b, temp; printf("Enter two integers: "); scanf("%d %d", &a, &b); temp = a; a = b; b = temp; printf("After swapping: a = %d, b = %d", a, b); return 0; }

Explanation

  • temp = a;: Stores the value of a in temp.

  • a = b;: Assigns the value of b to a.

  • b = temp;: Assigns the original value of a to b.

Comments

Popular posts from this blog

C Program To Find Largest Number Among Three Integers

C Program To Convert Fahrenheit To Celsius

C Program To Reverse a Number