UrbanPro

Take Tuition from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

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

Asked by Last Modified  

248 Answers

Learn Tuition

Follow 21
Answer

Please enter your answer

A mathematics nerd, B.Sc. (Maths), M.Sc (Pure Maths).

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 you
don'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

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
  1. #include<stdio.h>  
  2.  int main()    
  3. {    
  4. int a=10, b=20;      
  5. printf("Before swap a=%d b=%d",a,b);      
  6. a=a+b;//a=30 (10+20)    
  7. b=a-b;//b=10 (30-20)    
  8. a=a-b;//a=20 (30-10)    
  9. printf("\nAfter swap a=%d b=%d",a,b);    
  10. return 0;  
  11. }   
Before swap a=10 b=20
After swap a=20 b=10

 

read less
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

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

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

View 246 more Answers

Related Questions

Distinguish between diploblastic & triploblastic animals.
Diploblastic; body derived from two cell layer (endoderm&ectoderm)and triploblastic ;body derived from complete three embryonic cell layers i,e. Ectoderm,mesoderm,endoderm.
Ashish
0 0
8

What is the work-energy principle?

The principle of work and kinetic energy (also known as the work-energy ) states that the work done by the sum of all forces acting on a particle equals the change in the kinetic energy of the particle.
Jayesh
0 0
7
What is the use of coordinate geometry ?
It's uses are spread in all fields like trigonometry, calculus, dimensional geometry, etc. Suppose we have to find the distance between 2 points, we can use the help of coordinate geometry for this. The...
Kudo

DA,CBand OM are each perpendicular to line segment AB, where O is the point of intersection ofAC and DB. if AD=2.4cm, CB=3.6cm then OM=

In a right triangle BAD, We have OM parallel to side DA of triangle BAD. So triangle BMO is similar to triangle BAD by AA similarity criteria. Thus corresponding sides of similar triangles are proportional....
Minime3225

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

Ask a Question

Related Lessons

तृतीया विभक्ति (उपपद विभक्ति )
साथ के योग में विभक्ति साकम्, सार्धम्, समम् के साथ तृतीया विभक्ति होती है नक्षत्रेण सह चन्द्रमा उदेति = ताराओं के साथ चन्द्रमा उगता है ।अन्यया भाषया सह संस्कृतमपि अवश्यं शिक्षेयुः...

Learning Path For Mobile Application Development
I. Traditional Approach: 1. Native Android Application Development: Android is a mobile operating system that runs on phones, tablets, smartwatches, TVs and more. You can learn Android Application Development...

Do You Know How Is Size Of Structure Defined?
Size of the structure is defined based on multiplies of bigger data type member in the structure. Example: If a structure contains integer, char, short data type, then size of the Structure will be multiples...

English: Verbs And Types
Verbs and types: Verbs are words that express action or state of being, and they are an essential part of a complete sentence. There are three types of verbs: Action verbs, linking verbs, and helping...
A

Atharva

2 0
0

What are crucial conversations? How is anger triggered?
Recently I came across a book about Crucial conversations. The language and the content were extremely engaging. I wanted to talk in a series here about some insights that I got from that book. Usually,...
S

Sailakshmi K V

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
X

Looking for Tuition Classes?

The best tutors for Tuition Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Take Tuition with the Best Tutors

The best Tutors for Tuition Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more