This Post Contains A C Program For Exchanging The Values Of Two
Variables Without Using A Third [Temporary] Variable With Correct Source Code
& Output. This Program Is Written, Compiled & Executed At TurboC3.0 Compiler & Will Help You To Understand The Concept Of Variable Manipulation From C Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple
& Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C Program For Variable Exchange.
/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int a = 0, b = 0;
clrscr();
/* Accepting The Values Of Two Variabe From User */
printf("\n Please Enter Number For Variable A : ");
scanf("%d", &a);
printf("\n Please Enter Number For Variable B : ");
scanf("%d", &b);
printf("\n Values Of Two Variables [i.e. A & B] Before Exchange : %d %d", a, b);
/* Main Code For Exchanging The Values Of Two Variables Using Temporary Variable */
b = a + b;
a = b - a;
b = b - a;
/* Printing The Values Of Two Variables After Exchange */
printf("\n\n Values Of Two Variables [i.e. A & B] After Exchange : %d %d", a, b);
getch();
}
/* End Of Program */
Output :
Please Enter Number For Variable A : 1
Please Enter Number For Variable B : 2
Values Of Two Variables [i.e. A & B] Before Exchange : 1 2
Values Of Two Variables [i.e. A & B] After Exchange : 2 1
& Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C Program For Variable Exchange.
/* Declaration Of Header Files */
#include<stdio.h>
#include<conio.h>
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int a = 0, b = 0;
clrscr();
/* Accepting The Values Of Two Variabe From User */
printf("\n Please Enter Number For Variable A : ");
scanf("%d", &a);
printf("\n Please Enter Number For Variable B : ");
scanf("%d", &b);
printf("\n Values Of Two Variables [i.e. A & B] Before Exchange : %d %d", a, b);
/* Main Code For Exchanging The Values Of Two Variables Using Temporary Variable */
b = a + b;
a = b - a;
b = b - a;
/* Printing The Values Of Two Variables After Exchange */
printf("\n\n Values Of Two Variables [i.e. A & B] After Exchange : %d %d", a, b);
getch();
}
/* End Of Program */
Output :
Please Enter Number For Variable A : 1
Please Enter Number For Variable B : 2
Values Of Two Variables [i.e. A & B] Before Exchange : 1 2
Values Of Two Variables [i.e. A & B] After Exchange : 2 1
No comments:
Post a Comment