C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers – If you are looking for swapping program in C language, this article will help you to lean swapping two numbers in C. Just go through this blog post you will able write a program to swap two numbers using two variable num1 and num2.

We are going to publish series of C programs for beginners, students, B.Tech graduates to enhance their programming skills and hands on experience on coding. So it helps in reaching their goals in career. All the best guys in learning c programming with coding compiler blog.

C Program To Swap Two Numbers

Swapping in C language can be done using various techniques, here in this swapping program we used two variables to perform swap two numbers.

C Program To Swap Two Numbers Source Code

Copy paste the below source code in to c compilers and run the program to see the the result.

/* Swapping Two Numbers Using Two Variables In C - SWAPTWONUMBERS.C */

#include<stdio.h>
#include<conio.h>
void main()
{
    int num1, num2 ;
    clrscr() ;
    printf("Enter two numbers : ") ;
    scanf("%d %d", &num1, &num2) ;
    printf("\nBefore swapping : \n\n") ;
    printf("num1= %d \t num2 = %d", num1, num2) ;

    /* Swapping Two Numbers Logic */
    num1 = num1 + num2 ;
    num2 = num1 - num2 ;
    num1 = num1 - num2 ;
    printf("\n\nAfter swapping : \n\n") ;
    printf("num1 = %d \t num2 = %d", num1, num2) ;
    getch() ;
}

C Program To Swap Two Numbers Output

After you compile and run the above program your C compiler asks you to enter two numbers, then it will show output before swapping and after swapping two numbers like below expected output.

Enter two numbers : 10 20

Before swapping :
num1 = 10 num2 = 20

After swapping :
num1 = 20 num2 = 10

Read Other C Programming Tutorials:

C Program To Swap Two Numbers Using Two Variables

C Program To Swap Two Numbers Using Three Variables

 

1 thought on “C Program To Swap Two Numbers Using Two Variables”

  1. Hi, this is a comment.
    To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
    Commenter avatars come from Gravatar.

    Reply

Leave a Comment