Computer Science, asked by nitya724, 1 year ago

write a program to take two values and interchange them by using an extra variable


kjmongia13: in which programming language do u want it ?

Answers

Answered by GauravJangra
2
int main()
{
int a,b,temp;
cout<<"enter 1st value";
cin>>a;
cout<<"enter 2nd value";
cin>>b;

temp=a;//value of a stored into temp variable
a=b;//value of b is stored into a,now if b=1,then both a and b contain 1;
b=temp;//value of temp(or a) is send into b

cout<<"swaped values are:-/n"<<a<<b;
cout<<"/nthank you";
getchar();
}


Answered by AskewTronics
0

Below are the Q-Basic program and output for the above question:

Output :

If the user input as 3 and 4, then it will prints 4 and 3.

If the user input as 4 and 5, then it will prints 5 and 4.

Explanation:

INPUT "Enter the first number for swapping",first_number 'Take the first number from user.

INPUT "Enter the Second number for swapping",Second_number' Take the second number from user.

Third_Number=first_number'Swap the number using third variable.

first_number=Second_number

Second_number=Third_Number

PRINT "The number after Swapping is : ",first_number,"and ",Second_number'Print the number after swapping.

Code Explanation :

  • The above code is in Q-Basic language, in which the first and the second line is used to take the input from the user.
  • Then the three-line expression is using to replace the value, for this, it will transfer the second variable value on the third variable and then the first variable value on the second variable and then the third variable value is on the first variable.

Learn More :

  • Q-BASIC : https://brainly.in/question/2343849
Similar questions