Ticker

6/recent/ticker-posts

C Language (Swapping Programs)

PERFORM TO SWAP VALUES BETWEEN TWO VARIABLES



PROGRAM TO SWAP VALUES USING THIRD VARIABLE

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf("Enter first value:");
scanf("%d",&a);
printf("Enter second value:");
scanf("%d",&b);

printf(" - - VALUES BEFORE SWAPPING - - -");
printf("\nValue of A: %d",a);
printf("\nValue of B: %d",b);

// Performing swapping
c= a;
a = b; 
b = c;

printf(" \n\n- - VALUES AFTER SWAPPING - - -");
printf("\nValue of A: %d",a);
printf("\nValue of B: %d",b);

getch();
}


* * * PROGRAM OUTPUT * * *



PROGRAM TO SWAP VALUES WITHOUT USING THIRD VARIABLE
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
printf("Enter first value:");
scanf("%d",&a);
printf("Enter second value:");
scanf("%d",&b);

printf(" - - VALUES BEFORE SWAPPING - - -");
printf("\nValue of A: %d",a);
printf("\nValue of B: %d",b);

// Performing swapping
a = a+b;
b = a-b; 
a = a-b;

printf("\n\n - - VALUES AFTER SWAPPING - - -");
printf("\nValue of A: %d",a);
printf("\nValue of B: %d",b);

getch();
}


* * * PROGRAM OUTPUT * * *
















Post a Comment

0 Comments