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);
}



C++ Program:
#include<iostream.h>
void main()
{
int x,y,temp;
cout<<"Enter the value of x and y\n";
cin>>x>>y;
cout<<"Before Swapping\n x= "<<x<<"\ny = "<<y;
temp = x;
x= y;
y = temp;
cout<<"After Swapping\n x= "<<x<<"\ny= "<<y;
}

No comments :

Post a Comment