This is program to interchange two numbers using function. The program is given below. The program can be extended by using more numbers and interchanging them. Go enjoy the program. Lets begin…..
Program to interchange() two numbers using function
//clear the screen.
//declare variables as int.
//Input the numbers
cout<<“Enter the a.”<<endl;
cout<<“Enter the b.”<<endl;
void change(int a,int b)
cout<<“a = “<<a<<endl;
cout<<“b = “<<b<<endl;
Enter the a.
Enter the b.
a = 2
b = 1
How does it work
- You enter the numbers.
- The function changes the position of numbers in variables.
- The output is printed.
The program can be extended by using more numbers and changing there positions. Go extend it.
- Include ‘iostream.h’ and ‘conio.h’ files.
- Declare the function prototype.
- Add void main.
- Start program by first clearing the screen.
- Declare the variables as int (name them as you want.)
- Add the cout and cin of numbers.
- Call the function.
- Add the function definition outside the void main().
At the end
You learnt creating the C++ program of Interchanging or swapping two numbers using function. So now enjoy the program.
Please comment on the post and share it.
And like it if you liked.