Swap Two Numbers
Topic: Swap Two Numbers
Hey Guys, today we are going see a Python Program to swap two numbers
Title: Python Code for Swap Two Numbers (Beginner Friendly+ Easy + Code + Output + Online Compiler)
What is this program and why we are going to use it?
This Program consists of Variables, Input Function and Arithmetic Operator.
By using variable, we are going to store values in variables in int or float format according to our use. By using input function, we are going to input values from the user. For Eg. storing the values:
a=5
b=6
here a and b are variables where 5 is assigned to a and 6 is assigned to b.
Now for input function
By using variable, we are going to store values in variables in int or float format according to our use. By using input function, we are going to input values from the user. For Eg. storing the values:
a=5
b=6
here a and b are variables where 5 is assigned to a and 6 is assigned to b.
Now for input function
a = int(input("Enter a Number a :"))
b = int(input("Enter a Number b :"))
b = int(input("Enter a Number b :"))
So, this will ask user to input two numbers.
Method 1: We directly swap values using Python multiple assignment.
a, b = b, a
Method 2: We use a third variable to store one value temporarily.
z = a
a = b
b = z
By using these methods, the value of a becomes b and the value of b becomes a.
Logic:
Also we are going to use swapping logic here.Method 1: We directly swap values using Python multiple assignment.
a, b = b, a
Method 2: We use a third variable to store one value temporarily.
z = a
a = b
b = z
By using these methods, the value of a becomes b and the value of b becomes a.
Comments
Post a Comment