C program to swap the values of two numbers

C program to swap the values of two numbers

As you all know, swap means exchange, so in this program, we are exchanging the values of two variables (num1 and num2), and for that, we are using a third variable called temp, so that you can better understand the process of swapping in programming.

c program to swap the values of two numbers

#include <stdio.h>

int main() {
    int num1, num2, temp;
    
    printf("Enter first number: ");
    scanf("%d", &num1);
    
    printf("Enter second number: ");
    scanf("%d", &num2);
    
    temp = num1;
    num1 = num2;
    num2 = temp;
    
    printf("After swapping:\n");
    printf("First number: %d\n", num1);
    printf("Second number: %d\n", num2);
    
    return 0;
}

If num1 = 5 & num2 = 6;

After Swapping:

It becomes num1 = 6 & num2 = 5;

Keep Coding🧑‍💻

Similar articles:

About The Author

Leave a reply

Your email address will not be published. Required fields are marked *