Q:

C Program To Swap Two Numbers (7 Different Ways)

belongs to collection: Basic C Programming Examples

0

you have to make this program in the following way:

  1. C Program To Swap Two Numbers Using Third Variable
  2. C Program To Swap Two Numbers Without Using Third Variable
  3. C Program To Swap Two Numbers Using Function
  4. C Program To Swap Two Numbers Using Pointer
  5. C Program To Swap Two Numbers Using Call by Reference
  6. C Program To Swap Two Numbers Using Call by Value
  7. C Program To Swap Two Numbers Using XOR Operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C Program To Swap Two Numbers Using Third Variable

Algorithm

  • Program Start
  • Declare Variable
  • Input Number
  • Assign Numbers into Variables
  • Print Values before Swapping
  • Swapping Two Numbers Using Third Variable
  • Display Values after Swapping
  • Program End

Program

//C Program To Swap Two Numbers Using Third Variable 

#include <stdio.h>
int main()
{
int v1, v2, temp;
printf("Enter two Number");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
temp = v1;
v1 = v2;
v2 = temp;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}

Output

Enter Two Number
10
20
Before Swapping 
 First Variable = 10 
 Second Variable = 20
After Swapping 
 First VBariable = 20 
 Second Variable = 10

C Program To Swap Two Numbers Without Using Third Variable

Program

//C Program To Swap Two Numbers Without Using Third Variable 

#include <stdio.h>
int main()
{
int v1, v2;
printf("Enter two Number : ");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 + v2;
v2 = v1 - v2;
v1 = v1 - v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}

Output

Enter two Number : 20 50
Before Swapping
 First variable = 20
 Second variable = 50
After Swapping
 First variable = 50
 Second variable = 20

C Program To Swap Two Numbers Using Function

Algorithm

  • Program Start
  • Declare Variable
  • Input Number
  • Assign Numbers into Variables
  • Calling Function To swap to numbers
  • Print Values before Swapping
  • Swapping Two Numbers Using Third Variable
  • Display Values after Swapping
  • Program End

Program

//C Program To Swap Two Numbers Using Function 

#include <stdio.h>
void swap(int v1, int v2)
{
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 + v2;
v2 = v1 - v2;
v1 = v1 - v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
}
int main()
{
int n1, n2;
printf("Enter two Number : ");
scanf("%d%d", &n1, &n2);
swap(n1,n2);
return 0;
}

Output

Enter two Number : 10  30
Before Swapping
 First variable = 10
 Second variable = 30
After Swapping
 First variable = 30
 Second variable = 10

C Program To Swap Two Numbers Using Pointer

Program

#include<stdio.h>

void swap(int *x, int *y);
int main()
{
  int a,b;
  printf("Enter Two Numbers : ");
  scanf("%d %d",&a,&b);
  printf("Before Swapping a = %d and b = %d \n",a,b);
  swap(&a,&b);

  printf("After Swapping a = %d and b = %d ",a,b);

  return 0;
}
void swap(int *x,int *y)  //Here x and y is a pointer variable 
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}

Output

Enter Two Numbers : 50  100
Before Swapping a = 50 and b = 100
After Swapping a = 100 and b = 50

C Program To Swap Two Numbers Using Call by Reference

Program

#include<stdio.h>

void swap(int *x, int *y);
int main()
{
  int a = 100, b = 200;
  printf("Before Swapping a= %d and b= %d \n",a,b);
  swap(&a,&b);

  printf("After Swapping a= %d and b= %d ",a,b);

  return 0;
}
void swap(int *x,int *y)
{
    int t;
    t=*x;
    *x=*y;
    *y=t;
}

Output

Before Swapping a= 100 and b= 200
After Swapping a= 200 and b= 100

C Program To Swap Two Numbers Using Call by Value

Program

//C Program To Swap Two Numbers Using Call by Value  

#include<stdio.h>
void swap(int x,int y);
int main()
{
  int a=10,b=20;

  printf("Before Swapping a= %d and b= %d \n",a,b);
  swap(a,b);

  return 0;
}
void swap(int x,int y)
{ int t;
    t=x;
    x=y;
    y=t;
  printf("After Swapping a= %d and b= %d ",x,y);
}

Output

Before Swapping a= 10 and b= 20
After Swapping a= 20 and b= 10

C Program To Swap Two Numbers Using XOR Operator

Program

//C Program To Swap Two Numbers Without Using XOR Operator
#include <stdio.h>
int main()
{
int v1, v2;
printf("Enter two Number : ");
scanf("%d%d", &v1, &v2);
printf("Before Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
v1 = v1 ^ v2;
v2 = v1 ^ v2;
v1 = v1 ^ v2;
printf("After Swapping \n First variable = %d \n Second variable = %d \n", v1, v2);
return 0;
}

Output

Enter two Number : 40  60
Before Swapping
 First variable = 40
 Second variable = 60
After Swapping
 First variable = 60
 Second variable = 40

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Basic C Programming Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program To Find Fibonacci Series (5 Different Wa... >>
<< Armstrong Number Program In C (5 Different Way To ...