Q:

C Program to swap two numbers using arithmetic operator:

0

In this method, we will calculate the sum of two given numbers and assign one of them. The numbers can then be swapped using the subtraction from the sum. 

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 = 10, b = 20;
    a = a + b; // a now becomes 30
    b = a - b; // b becomes 10
    a = a - b; // a becomes 20
    printf("After Swapping: a = %d, b = %d", a, b);
    return 0;
}

Output:

Enter Value of a = 10
Enter Value of b = 20

After Swapping: a = 20, b = 10

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now