brel-logo-white brel-logo-white
  • Travel
  • Case Study
  • How to
  • Technology
  • More
    • Sports
    • Health
    • Articles
    • Review
Notification
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
BreldigitalBreldigital
  • HomeHome
  • Finance
  • Health
  • Lifestyle
  • Sports
  • Contact Us
Search
  • Quick Access
    • Home
    • History
    • My Saves
    • My Interests
    • My Feed
  • Categories
    • Travel
    • Sports
    • Health

Top Stories

Explore the latest updated news!

What idea is emphasized through repetition

The a value of a function in the form f(x) = ax2 + bx + c is negative. which statement must be true?

What experimental evidence led to the development of this atomic model from the one before it?

Stay Connected

Find us on socials
248.1k Followers Like
61.1k Followers Follow
165k Subscribers Subscribe
Breldigital > Blog > ds > Swap two numbers without using third variable
ds

Swap two numbers without using third variable

By John Published November 10, 2022
Share

This is a Python Program to exchange the values of two numbers without using a temporary variable.

Problem Description
The program takes both the values from the user and swaps them without using temporary variable.

Problem Solution
1. Take the values of both the elements from the user.
2. Store the values in separate variables.
3. Add both the variables and store it in the first variable.
4. Subtract the second variable from the first and store it in the second variable.
5. Then, subtract the first variable from the second variable and store it in the first variable.
6. Print the swapped values.
7. Exit.

Program/Source Code
Here is source code of the Python Program to exchange the values of two numbers without using a temporary variable. The program output is also shown below.

a=int(input(“Enter value of first variable: “))
b=int(input(“Enter value of second variable: “))
a=a+b
b=a-b
a=a-b
print(“a is:”,a,” b is:”,b).

Runtime Test Cases

Case 1
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3

Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56

TAGGED: Swap two numbers without using third variable
John November 10, 2022 November 10, 2022

Search

brel-logo-white brel-logo-white
Explore a wide range of brands and categories with our comprehensive coverage, and stay up-to-date with the latest news and trends by subscribing to our updates.
Categories:
  • Entertainment
  • Travel
  • Sport
  • Contact Us

Quick Links

  • My Feed
  • My Interests
  • History
  • My Saves

About US

  • Adverts
  • Our Jobs
  • Term of Use

© 2023 Brel Digital All Rights Reserved. All logos and images used on this website are registered trademarks of their respective companies

Welcome Back!

Sign in to your account

Lost your password?