Q:

C PROGRAM EXAMPLE FOR SWAPPING 2 NUMBERS:

0

C PROGRAM EXAMPLE FOR SWAPPING 2 NUMBERS:

What is swapping?

Swaping is nothing but interchanging. It may be the value of 2 numbers or something to exchange between them. For example, consider below example.

 

  • Before swapping, the values of A and B are as below.
    A = 10
    B = 20
  • After swapping, the values of A and B will be as below.
    A = 20
    B = 10

 

All Answers

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

#include <stdio.h>
int main()
{
   int A, B, temp;
 
   printf("Please enter the 1st number : ");
   scanf("%d",&A);
   printf("\nPlease enter the 2nd number : ");
   scanf("%d",&B);
 
   printf("\nBefore swapping:\n");
   printf("A - %d \nB - %d", A, B);
   temp = A;
   A = B;
   B = temp;
 
   printf("\nAfter swapping:\n");
   printf("A - %d \nB - %d", A, B);
 
   return 0;
}

OUTPUT :

Please enter the 1st number : 20
Please enter the 2nd number : 30
Before swapping:
A – 20
B – 30
After swapping:
A – 30
B – 20

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now