Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
5. Write a program that swaps the values of two variables using a third variable.
/*
File: Prgrm05.c
Author: Aditya Saini
Date: Sept 17, 2019
Description: Program to swap the value of two variables using the third variable.
*/
#include <stdio.h>
int main (void)
{
int variable_1, variable_2, temp_variable;
//Input the value of variables.
printf ("Input the value of first variable: ");
scanf ("%d", &variable_1);
printf ("Input the value of second variable: ");
scanf ("%d", &variable_2);
//Input the value of variables.
printf ("Input the value of first variable: ");
scanf ("%d", &variable_1);
printf ("Input the value of second variable: ");
scanf ("%d", &variable_2);
//Swapping the values.
temp_variable = variable_1;
variable_1 = variable_2;
variable_2 = temp_variable;
//Output the result.
printf ("\nAfter Swapping\n");
printf ("Value of first variable: %d \n", variable_1);
printf ("Value of second variable: %d \n", variable_2);
variable_1 = variable_2;
variable_2 = temp_variable;
//Output the result.
printf ("\nAfter Swapping\n");
printf ("Value of first variable: %d \n", variable_1);
printf ("Value of second variable: %d \n", variable_2);
};
Output
Input the value of first variable: 15
Input the value of second variable: 17
After Swapping
Value of first variable: 17
Value of second variable: 15
Input the value of second variable: 17
After Swapping
Value of first variable: 17
Value of second variable: 15
No comments:
Post a Comment
Please do not post spam links.