Write a program to swap values of two variables with and without using third variable.

Asked by Last Modified  

248 Answers

Follow 21
Answer

Please enter your answer

A mathematics enthusiast, B.Sc. (Maths), M.Sc (Pure Maths) PhD Maths (Pursuing).

I assume you know Python 3, so I'll answer using that, similar can be done in other languages.1) #suppose you want to switch values of a and b temp = a #we create a temp variable which will store the value of a a = b; b = temp#Now, if we hadn't created temp variable then b would take the value of a but...
read more
I assume you know Python 3, so I'll answer using that, similar can be done in other languages.1) #suppose you want to switch values of a and b temp = a #we create a temp variable which will store the value of a a = b; b = temp#Now, if we hadn't created temp variable then b would take the value of a but mind that a has a value of b, so b will take the value of a which is not what you want.2) There is another way and that is using tuples it's most easy youdon't need to make any 3rd variable. it's simply:a,b = b, a #you can add parenthesis if you want. that's it you've swapped the values. read less
Comments

Tutor

If the kid is not ready to sit, it means he has logical thinking. Try to teach him in taking real-life examples like discussion sums in which you can include his name and his friends. In this way, he will be taking an interest.
1 Comments

Student who can understand problems of other students better

Enter value of x,y than x = x+y than y = x-y than x = x-y print swapped value x,y.
Comments

Programming Language Instructor in C, C++, JAVA.

If you are coding in C++ language then this will help you: #include<iostream.h> int main(){ int a=10, b=20; a=a+b; b=a-b; a=a-b; cout<<a<<" "<<b; return 0; }
Comments

Certified Tamil Teacher with 25 Years of Professional Teaching Experience

With 3rd Variable: int temp, a, b; temp = a; a = b; b = temp; Without 3rd Variable: int a,b; a = a + b; b = a - b; a = a - b;
Comments

Working as a Data Scientist in Bangalore, India

Python script first = 10 second = 20 print(first, second) first,second = second, first print(first, second) For any further queries, contact me
Comments

With third variable Swap(a,b): c=a a=b b=c Without third variable Swap(a,b): a = a+b b= a-b a= a-b
read more
With third variable Swap(a,b): c=a a=b b=c Without third variable Swap(a,b): a = a+b b= a-b a= a-b read less
Comments

Investment in education gives the best return.

I am using python! >> A = 2 >> B = 3 >> A = A + B # A = 5 >> B = A - B # 5-3 = 2, now ,B = 2 >> A = A - B # 5-2 = 3, now ,A = 3
Comments

Programming is an creative Art . I help students to be creative artist by learning coding skill.

A=2 B=4 A=A+B B= A-B A=A-B
Comments

BEST URBAN PRO TUTOR PARTNER

#include<stdio.h> int main() { int a=10, b=20; printf("Before swap a=%d b=%d",a,b); a=a+b;//a=30 (10+20) b=a-b;//b=10 (30-20) a=a-b;//a=20 (30-10) printf("\nAfter swap a=%d b=%d",a,b); return 0; } Before swap a=10 b=20 After swap a=20...
read more
#include<stdio.h> intmain() { inta=10,b=20; printf("Beforeswapa=%db=%d",a,b); a=a+b;//a=30(10+20) b=a-b;//b=10(30-20) a=a-b;//a=20(30-10) printf("\nAfterswapa=%db=%d",a,b); return0; } Before swap a=10 b=20 After swap a=20 b=10 read less
Comments

View 246 more Answers

Related Questions

"प्रसृते विजने प्रदेशे " what is the meaning of this line ?

प्रसृते ( फैले हुए ) , विजने ( सुनसान , लोगों से रहित ) प्रदेशे ( स्थान पर ) अर्थात् दूर -दूर तक सुनसान जगह पर । ( जहां दूर-दूर तक कोई आदमी दिखाई न दे रहा हो ऐसी जगह पर ) * This line is from lesson 11...
Renu
How to promote classes in a particular locality.
you can pay to UrbanPro, they wil market behalf of you ..
Manoj
What is the chance to get any student for my online tutoring, here in UrbanPro? please share your experiences truly.
Outcome always depends on your quality of content and style of delivery. Be thorough in your concept and try to relate them with real-life examples so that students will get no other choice to go anywhere.
Ajit Kumar M.
What is living being?
A living being is any organism or an existing form that possesses the characteristics of life or being alive. In other words, living beings are those organisms which can grow, breath, move, reproduce,...
Shrasti

What is the meaning of " आलस्यं हि मनुष्याणां शरीरस्थो महान् रिपुः |
नास्ति उद्यंसमो बन्धुः कृत्वा यं नावसीदति ||" 

Hi Rohan, below is the Hindi translation. आलस्यं हि मनुष्याणां शरीरस्थो महान् रिपुः | means अर्थात् : मनुष्यों के शरीर में रहने वाला आलस्य ही ( उनका ) सबसे बड़ा शत्रु होता है | means our greatest enemy...
Rohan

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons


Abacus Classes & Evolution of Abacus | Mastermind Abacus
Abacus is an instrument that was found 2500 years prior and was utilized for doing mental arithmetic calculations. The instrument was made of casings (wood and so forth) which had beads made of sand,...

B.Com 1st Year Law Test - Unit 1
B.Com 1st Year - Law Duration: 1 HoursMax Marks: 20 Q1. Define the term Contract. Explain with the help of its essential features? Q2. Explain the term Void, Voidable & Innelgal Contract? Q3....

Spring - Dependency Injection (DI)
Spring - Dependency Injection (DI) DI is a framework which provides loose coupling in code. Here loose coupling means no hard coding of the object. Instead of hard coding, we will be injecting these object...

How to get better Grades?
Two things - determination and dedication should be a part of your life.Study for you and your goals.Study for your parents they are spending bucks.Do not think of harwards, PhDs, majors. Forget the word...
K

Kushal Sharma

0 0
0

Recommended Articles

Quality education does not only help children to get a successful career and life, but it also hugely contributes to society. The formal education of every child starts from school. Although there are numerous schools, parents find it challenging to choose the right one that would fit their child. It is difficult for them...

Read full article >

Appearing for exams could be stressful for students. Even though they might have prepared well, they could suffer from anxiety, tension etc. These are not good for their health and mind. However, following a few exam preparation tips can save them from all these and help them to score good marks. Let’s find out all...

Read full article >

Once over with the tenth board exams, a heavy percentage of students remain confused between the three academic streams they have to choose from - science, arts or commerce. Some are confident enough to take a call on this much in advance. But there is no worry if as a student you take time to make choice between - science,...

Read full article >

With the current trend of the world going digital, electronic renaissance is a new movement that is welcomed by the new generation as it helps makes the lives of millions of people easier and convenient. Along with this rapidly changing movement and gaining popularity of Internet, e-Learning is a new tool that emerging...

Read full article >

Looking for Tuition ?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you