Custom Search

Monday 21 November 2011

C/C++ Program to write a function that will interchange the value of two variable, Program to interchange values of 2 variables


C/C++ Program to write a function that will interchange the value of two variable, Program to interchange values of  2 variables

Program
#include<stdio.h>
#include<conio.h>
void interchange(int, int);
void main()
{
int x,y;
clrscr();
printf("Enter two Numbers:\n");
scanf("%d %d", &x, &y);
printf("\nOriginal value is:%d %d", x,y);
interchange(x,y);
getch();
}
void interchange(int m,int n)
{
int temp;
temp=m;
m=n;
n=temp;
printf("\nThe value after interchange is: %d %d",m,n);
}

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.

No comments:

Post a Comment

Laptops