Showing posts with label temporary. Show all posts
Showing posts with label temporary. Show all posts

C and C++ Program for Swapping Two Numbers Without Third Variable

This post contains C and C ++ programs for swapping two given numbers without using any temporary variable. Only the two variables are required, not a third.

C Program
#include<stdio.h>
void main()
{

C and C++ swapping numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}