Program to swap two number using third variable

Write a program to swap two numbers using the third variable.

C Program

#include <stdio.h>
int main()
{
    int firstNumsecondNum;
    printf("Enter first number : ");
    scanf("%d", &firstNum);
    printf("Enter second number : ");
    scanf("%d", &secondNum);
    printf("Numbers before swap is first= %d, second= %d\n"firstNumsecondNum);
    int thirdNum = firstNum;
    firstNum = secondNum;
    secondNum = thirdNum;
    printf("Numbers after swap is first= %d, second= %d\n"firstNumsecondNum);
    return 0;
}

Input:

Enter first number : 10
Enter second number : 20

Output:

Numbers before swap is first= 10, second= 20
Numbers after swap is first= 20, second= 10


No comments:

Post a Comment