Monday, October 19, 2020

swapping numbers in c (2 unique different methods)

First Method:

#include <stdio.h>

int main()

{

    int a, b, c;

    printf("Enter A & B:");

    scanf("%d%d", &a, &b);

    printf("Before swapping: a = %d, b = %d", a, b);

    c=a;

    a=b;

    b=c;

    printf("\nAfter swapping: a = %d, b = %d", a, b);

    return 0;

}


Second Method:

#include <stdio.h>

int main()

{

    int a, b, c;

    printf("Enter A & B:");

    scanf("%d%d", &a, &b);

    printf("Before swapping: a = %d, b = %d", a, b);

    a=a+b;

    b=a-b;

    a=a-b;

    printf("\nAfter swapping: a = %d, b = %d", a, b);

}

No comments:

Post a Comment

How to Fight Overthinking or Negative Thoughts?

How to Combat Overthinking or Negative Thoughts? 1. Don’t Fight Your Thoughts Trying to suppress or control negative thoughts often backfire...