UrbanPro

Learn C Language from the Best Tutors

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

Search in

What is the algorithm to be used to swap two numbers without using a temporary variable?

Asked by Last Modified  

Follow 2
Answer

Please enter your answer

MCA, guiding and tutoring Computer Science and Applications 2012

There are various ways using operators and expressions. The simplest way is always recommended with some physical world scenarios. As you asked for algorithm only. Declare and initialize two variables first, second Step 1 → Add first and second and store to first Step 2 → Subtract second...
read more

There are various ways using operators and expressions. The simplest way is always recommended with some physical world scenarios. 

As you asked for algorithm only.

Declare and initialize two variables first, second

Step 1 → Add first  and second and store to first

Step 2 → Subtract second from first and store to second

Step 3 → Subtract second from first and store to first

Swap completed.

Insight: 

Assuming variable having value first=10 and second=20

Now adding first with second and storing the result 10+20=30 in first.

Now subtracting the second which is still 10 from first and storing the result 30-10=20 in second

Here we achieved the initial swap

Then subtracting second which is 20 from first 30-20=10 and storing the result in first complene the final swap as now value inside variable first is 10 and second is 20.

Thank you

read less
Comments

Related Questions

Why does execution of a program in C/C++/Java start from only main?
Main function is calling the other functions of he program and compiler gets this direction while Compiling the code. If it does not find main function then it will also not find the other functions in the code. So, main is required in C program.
Isha
How do I check my C programming skills?
You should try to solve your daily life problems with c , i.e - build your own budget calculator or chess game and many more. Moreover make a habbit of solving 10 questions daily.
Ramnarayanan
0 0
5
Is it necessary to learn the C language and also C++ before joining an engineering college?
it is not necessary to learn C and C++, but if you want to make your are career in computer then learn C & C++ because these are basic computer language.
J.k.kavyadharshini
How and where do I start learning C programming?
Goals Fundamental concepts of C programming language. Write your first C program. Understand different keywords & Data types in C. Understand Variables & Scope of Variables. Learn Enumeration or Enum...
Krishna
0 0
5
Is C/C++ language is still important in 2019?
Of course, C language is only the heart of any processor or controller.Because of complexity in C peoples are looking to work on High-level languages like JAVA etc.
Tejas
0 0
7

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

Ask a Question

Related Lessons

C Program Sample Application
//Standard Library Functions(Header Files used) #include<stdio.h> #include <conio.h> //Main method int main() { // function for clearing screen clrscr(); // function to print the output...

PRACTISE makes you PERFECT ; ; ; There is no SUBSTITUTE for HARD WORK ;;;;Breathe SUCCESS like OXYGEN
Proper Planning ( reg what portions to be covered today) revising today's class portions & clarifying doubts solving Maths problems regularly ,noting down formulae separately trying to understand...

Program to swap the value of two variables without using third variable(simplest way)...
int main() { int a=10,b=20; printf("values of a before swap ="%d,a); printf("values of b before swap ="%d,b); a=a+b; b=a-b; a=a-b; printf("******************"); printf("values of a after swap...

C for Begginers
C is an procedure oriented programming language. For any begginer the word program is new. Program: Set of instructions to be followed by machine or computer. Instruction Examples: Arithmetic instruction...

Recommended Articles

Brilliant Academy is one of the reputed institutes for B.Tech tuition classes. This institute is specialised in delivering quality tuition classes for B.E, Engineering - all streams and Engineering diploma courses. Incorporated in 2012, Brillant Academy is a brainchild of Mr Jagadeesh. The main motto of the academy is to...

Read full article >

Lasya Infotech is a Hyderabad based IT training institute founded in 2016 by O Venkat. Believing in his innovation, passion and persistence and with a diverse blend of experience, he started his brainchild to deliver exemplary professional courses to aspiring candidates by honing their skills. Ever since the institute envisions...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Looking for C Language Classes?

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 C Language Classes?

The best tutors for C Language Classes are on UrbanPro

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

Learn C Language with the Best Tutors

The best Tutors for C Language 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