program to swap two num
Answers
Answered by
3
java program to swap two numbers:
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[ ])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
}
import java.util.Scanner;
class SwapNumbers
{
public static void main(String args[ ])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y); temp = x; x = y; y = temp; System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
}
firdose:
thanks dr
Answered by
2
Python program and the output for the above question is listed below:
Output:
If the user input as 6 and 5, then the output is 5 and 6.
If the user input as 9 and 7, then the output is 7 and 9.
Explanation:
first_number =int(input("Enter the first number for swapping: "))#Take the first number.
Second_number =int(input("Enter the Second number for swapping: "))#Take the second number.
#Swap the number.
Third_number=first_number
first_number=Second_number
Second_number=Third_number
print("The first number and the second number after swapping is:",first_number,"and ",Second_number)#Print the number after swapping.
Missing information : There must be "Number" on the place of "NUM".
Code Explanation :
- The above code is in python language, in which the first and the second line is used to instruct the user and take the input from the user.
- Then the three expressions are used to swap the number with the help of the third variable.
- Then the number is printed after swapping with the help of print function.
Learn More :
- Python : https://brainly.in/question/14689905
Similar questions