Write A Program in C To Swap Two Numbers Without Using Third Variable
Write A Program in C To Swap Two Numbers Without Using Third Variable
M3-R4 O-Level Jan-20
M3-R4 : PROGRAMMING & PROBLEM SOLVING
THROUGH
‘C’ LANGUAGE
PART-1
To read about click on the following link
Write A Program To Find
Factorial Of Given Number Using Recursive Function
https://learncomputerpci.blogspot.com/2020/07/write-program-to-find-factorial-of.html#more
First Method
/*Write A Program in C To Swap Two Numbers Without Using Third Variable */
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
clrscr();
printf("Enter first number\n");
scanf("%d",&num1);
printf("Enter Second number\n");
scanf("%d",&num2);
/*you can also use scanf("%d%d",&num1,&num2) to input num1 and num2 */
printf("Before Swapping num1=%d and num2=%d\n",num1,num2);
/* Let num1=10 and num2=20 */
num1=num1+num2; /* Value of num1 will be updated i.e. 10+20=30 */
num2=num1-num2; /* here num1=30 and num2=20, then 30-20=10 */
num1=num1-num2; /* here num1=30 and num2=10, then 30-10=20 */
printf("After Swapping num1=%d and num2=%d\n",num1,num2);
getch();
}
Second Method
/*Write A Program in C To Swap Two Numbers Without Using Third Variable */
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
clrscr();
printf("Enter the value of num1 and num2\n");
scanf("%d%d",&num1,&num2);
printf("Before Swapping num1=%d and num2=%d\n",num1,num2);
/* Let num1=10 and num2=20 */
num1=num1*num2; /* Value of num1 will be updated i.e. 10*20=120 */
num2=num1/num2; /* here num1=120 and num2=20, then 120/20=10 */
num1=num1/num2; /* here num1=120 and num2=10, then 120/10=20 */
printf("After Swapping num1=%d and num2=%d\n",num1,num2);
getch();
}