Wednesday, 10 September 2014

Swapping of two Numbers in C Language with Example

C Language program for Swapping of two Numbers in an easy way. The questions may be asked for this C program is, Write a simple swapping of 2 numbers program in C language. & find the swap of few numbers using C source code. It is an useful C program for beginners.

SWAPPING TWO VALUE IN C
Swapping 2 numbers in C program and this is used to swap the 2 numbers without using third variable or with the use of third variable. It makes the first value to assign the temp variable and next one is to assign the value2 for value 1 and finally assign the temp value to value2 and display it as an output.

//Header files
#include <stdio.h>
#include <conio.h>
 
void main()
{
    //Program variables
    int value1,value2,temp;
 
    clrscr();//Function used to clear previous output
    printf("Enter first value  ");  //Display function
    scanf("%d",&value1);  //Getting value function
 
    printf("Enter second value : ");
    scanf("%d",&value2);
 
    //Calculation for swapping two numbers
    temp=value1;
    value1=value2;
    value2=temp;
 
    printf("After swapping value1 = %d and value2 = %d",value1,value2);
    getch();
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.