C Program to program to Swap two numbers




c programming
c programming

Swapping numbers in C means exchanging the values of two C variables with each other.

In this lesson we will learn to write a C Program to Swap Two Numbers: With or Without Temporary Variables.

C program to Swap two numbers using third variables.

/**
C Program to program to Swap two numbers by codebind.com
*/
#include <stdio.h>
int main()
{
  int a,b,temp;
  printf("Enter two Numbers\n");
  printf("\na = ");
  scanf("%d",&a);
  printf("\nb = ");
  scanf("%d",&b);
  temp=a;
  a=b;
  b=temp;
  printf("\na = %d\tb = %d \n", a, b);
}


/*
OUTPUT
Enter two Numbers

a = 55

b = 77

a = 77  b = 55
*/

C program to Swap two numbers without using third variable

/**
C Program to show swap of two no’s without using third variable by codebind.com
*/
#include<stdio.h>

int main() {
  int a, b;

  printf("Enter two Numbers\n");
  printf("\na = ");
  scanf("%d",&a);
  printf("\nb = ");
  scanf("%d",&b);

  a = a + b;
  b = a - b;
  a = a - b;

  printf("\nAfter swapping value of a : %d\n", a);
  printf("\nAfter swapping value of b : %d\n", b);

  return (0);
}


/*
OUTPUT
Enter two Numbers

a = 44

b = 63

After swapping value of a : 63

After swapping value of b : 44

*/

Above we have written Two C programs For swapping two numbers.


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*