C program to swap to number.

Posted by mangesh on March 14, 2018

/* C program to swap two number. */

Description :

We need to exchange the value of variable. i.e if there are two variable x, y. and x = 5, y= 6 then our program should make x=6, y=5.

Logic :

We can store the value of x in a temp variable ( temp = x ). now we can overwrite the variable x by y ( x = y ). Finally be must copy the the temp value at y ( y = temp ).

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
    int num1,num2,temp;
    printf("Enter first number: ");
    scanf("%d",&num1);
    printf("Enter second number: ");
    scanf("%d",&num2);
    printf("First number is %d, Second number is %d",num1,num2);
    temp = num1;
    num1 = num2;
    num2 = temp;
    printf("\nAfter swaping number ");
    printf("\nFirst number is %d, Second number is %d",num1,num2);
getch();
}


Output :

c program for swap two numbers` tested in turbo c 3.2

Skills Required :

Knowledge about variable storage.

Written with from Mangesh.

Related Post
1 C program to check number is even or odd.
2 C program to swap to number.
3 C program to check whether the year is leap year or not.
4 C program to check whether enter alphabet is vowel or not.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.