Sunday, 21 June 2015

C Program Swapping Number.

a)  With using third variable.
Program:

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int x, y,temp;  //Declaring Variable.
  
 
   printf("Enter the value of x and y\n");  //For printing
   scanf("%d%d",&x,&y);  //For Input.
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);  //For printing
   
   //performing Swap 
  temp=b;
  b=a;
  a= temp;
   
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);  //For printing
 
getch();  //Getting a character or waiting for key press
}
 
Output:


b) Without using third Variable
 Program:
#include<stdio.h>
#include<conio.h>
void main()
 {
   int a, b;  //Declaring Variable.
    
   
   printf("\nEnter value for num1 & num2 : ");  //For printing
   scanf("%d %d", &a, &b); //For Input.
    //performing Swap
   a = a + b;   
   b = a - b;
   a = a - b;
   printf("\nAfter swapping value of a : %d", a);  //For printing
   printf("\nAfter swapping value of b : %d", b);  //For printing
   getch();  //Getting a character or waiting for key press
}
Output:

No comments:

Post a Comment