Program1: Using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, swap;
printf("\n Enter two number - ");
printf("\n First Number : ");
scanf("%d",&num1);
printf("\n Second Number : ");
scanf("%d",&num2);
swap=num1;
num1=num2;
num2=swap;
printf("\n The value of first and second number after swapping is \n");
printf("\n First Number = %d \n Second Number = %d",num1, num2);
getch();
}
Program 2: Without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2;
printf("\n Enter two number - ");
printf("\n First Number : ");
scanf("%d",&num1);
printf("\n Second Number : ");
scanf("%d",&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n The value of first and second number after swapping is \n");
printf("\n First Number = %d \n Second Number = %d",num1, num2);
getch();
}
Run:
Previous Next
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2, swap;
printf("\n Enter two number - ");
printf("\n First Number : ");
scanf("%d",&num1);
printf("\n Second Number : ");
scanf("%d",&num2);
swap=num1;
num1=num2;
num2=swap;
printf("\n The value of first and second number after swapping is \n");
printf("\n First Number = %d \n Second Number = %d",num1, num2);
getch();
}
Program 2: Without using third variable
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num1, num2;
printf("\n Enter two number - ");
printf("\n First Number : ");
scanf("%d",&num1);
printf("\n Second Number : ");
scanf("%d",&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf("\n The value of first and second number after swapping is \n");
printf("\n First Number = %d \n Second Number = %d",num1, num2);
getch();
}
Run:
Previous Next
One program two concepts.
ReplyDeleteGood.
Carry on.