Posts

Showing posts with the label C program to swap numbers

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 .