In this tutorial you will learn about the C Program to Interchange Numbers and its application with practical example.
C Program to Interchange Numbers
In this tutorial, we will learn to create a C program that will Interchange Numbers in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
Program to Interchange Numbers:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We can perform many input-output operations using c programming. In today’s tutorial, we take the input numbers from the user. Then we will Interchange Numbers with the help of the c programming language.
With the help of this program, we can Interchange Numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Taking the input numbers from the user. 3. Swapping that number using third variable. 4. Print the Result. 5. End the program. |
Program to Interchange Numbers using a third variable:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> int main() { /* Declaring the variable for the program */ double first, second, flag; /* Taing input integer 1 from the user */ printf("Enter first number: "); scanf("%lf", &first); /* Taing input integer 2 from the user */ printf("Enter second number: "); scanf("%lf", &second); // value of first is assigned to flag flag = first; // value of second is assigned to first first = second; // value of flag (initial value of first) is assigned to second second = flag; // %.2lf displays number up to 2 decimal points printf("\nAfter swapping, first number = %.2lf\n", first); printf("After swapping, second number = %.2lf", second); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- first= it will hold the double value.
- second = it will hold the double value.
- flag = it will hold the double value.
Taking the input number 1.
Taking the input number 2.