C Program to Swap Two Numbers

Program1: Using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int num1, num2, swap;
    printf("\n Enter two number - ");
    printf("\n First Number : ");
    scanf("%d",&num1);
    printf("\n Second Number : ");
    scanf("%d",&num2);
    swap=num1;
    num1=num2;
    num2=swap;
    printf("\n The value of first and second number after swapping is \n");
    printf("\n First Number = %d \n Second Number = %d",num1, num2);
    getch();
}

Program 2: Without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int num1, num2;
    printf("\n Enter two number - ");
    printf("\n First Number : ");
    scanf("%d",&num1);
    printf("\n Second Number : ");
    scanf("%d",&num2);
    num1=num1+num2;
    num2=num1-num2;
    num1=num1-num2;
    printf("\n The value of first and second number after swapping is \n");
    printf("\n First Number = %d \n Second Number = %d",num1, num2);
    getch();
}

Run:


1 comment:

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...