Menu

Add Topic

C Programming

waiting answer January 16, 2021

Swapping of Two Numbers in C

Answers
January 16, 2021

To understand this example, you should have basic knowledge of C input/output, data types, and programming operators.

Program Description: swapping of two numbers in c

    #include
    int main() {
          int a, b, temp;
          printf("\nEnter first number:");
          scanf("%d", &a);
          printf("\nEnter the second number:");
          scanf("%d", &b);

          temp = a;
          a = b;
          b = temp;

          printf("\nAfter swapping, the first Number is %d", a);
          printf("\nAfter swapping, the second Number is %d", b);
          return 0;
    }

OUTPUT

         Enter first number:15
         Enter the second number:30
         After swapping, the first Number is 30
         After swapping, the second Number is 15

Program logic explanation:

  • The first number of inputs is assigned to a temporary variable. ie, temp = a;
  • The value of the second variable is assigned to the first variable. ie, a=b
  • Value of temp assigned to the second variable b. ie, b=temp.
0 0
January 16, 2021

Program Description: swapping of two numbers in c / swap two numbers in C

    #include<stdio.h>
    int main() {
          int a = 10;
          int b= 20;
          int temp;
          
          printf("\nBefore Swapping");
          printf("\n-------------------------------------------");
          printf("\nBefore Swapping, first Number is %d", a);
          printf("\nBefore Swapping, second Number is %d", b);

          temp = a;
          a = b;
          b = temp;
          
          printf("\n\nAfter Swapping");
          printf("\n-------------------------------------------");
          printf("\nAfter swapping, first Number is %d", a);
          printf("\nAfter swapping, second Number is %d", b);
          return 0;
    }

OUTPUT

         Before Swapping
         -------------------------------------------
         Before Swapping, first Number is 10
         Before Swapping, second Number is 20

        After Swapping
        -------------------------------------------
        After swapping, first Number is 20
        After swapping, second Number is 10    

0 0

Please Login to Post the answer

Leave an Answer