C program to perform swap two numbers

 C program to perform swap two numbers:  
 •With a Temporary variable                                     
 •Without a Temporary variable

Here I shared C program of performing  swapping of two numbers using 
both the methods with a temporary variable and without a temporary variable.


With a temporary variable

For example if user enter 45 and 12 then the output will be:

Enter two numbers:-
45
12
Before swapping values of a=45  and b=12

After swapping values of a=12   and b=45

#include<stdio.h>
int main()
{
    int a,b,temp;
     printf("\n Enter two numbers:-");
     scanf("%d%d",&a,&b);
     printf("\n Before swapping values of a=%d \t and b=%d \n ",a,b);
       temp=a;
       a=b;
       b=temp;
     printf("\n After swapping values of a=%d \t and b=%d \n ",a,b); 
     return 0;
}

Without a temporary variable

For example if user enter 46 and 35 then the output will be:

Enter two numbers:-
46
35
Before swapping values of a=46  and b=35
After swapping values of a=35   and b=46




#include<stdio.h>
int main()
{
   int a,b;
    printf("\n Enter two numbers:-");
    scanf("%d%d",&a,&b);
    printf("\n Before swapping values of a=%d \t and b=%d \n ",a,b);
      a=a+b;
      b=a-b;
      a=a-b;
    printf("\n After swapping values of a=%d \t and b=%d \n ",a,b); 
   return 0;
}

NOTE: You have any problem in above C Program do not hesitate and write your problem in the comment box. I will support your problem

Share this

0 Comment to "C program to perform swap two numbers"

Post a Comment