Algorithm and Flowchart to Swap Two Integer Numbers

[19542 views]


What is Variable?

Variable is a name given to the memory so that whenever we use that variable our computer can understand that. For eg: Let's say a=10 ,it means whenever we use a in our program our computer can recognize as number 10. Variable can store numbers ,characters, etc. depending on which language you are using.

What is Constant?

Constants are the values which cannot be changed by the program. Like in the previous example a=10; Value 10 is fixed only we can change that.

What is Swapping?

Swapping is used in various programs like sorting the array. It is mainly used in the area when we want to store old values without using much space.

So in Conclusion if we have taken a=10 and b=20 then after swapping it should become b=10 and a=20. Swapping can be done using various methods like taking a third variable and then swapping it or without using third variable swapping it through + and - operators or * and / operators. I will be discussing first method using third variable and then without using third variable.

Flowchart for Swapping two numbers using third variable:

Flowchart for Swapping two numbers using third variable
Remove WaterMark from Above Flowchart

Algorithm for Swapping two numbers using third variable:

Declare a variable a,b and c as integer; Read two numbers a and b; c=a; a=b; b=a; Print a and b

Here in this algorithm we declare 3 variables to store integers ,then we input two numbers lets say 10 and 20. In the computer it gets stored as a=10 and b=20 and we declare a variable c because if we put a=b then the value of b gets stores in a and then value gets removed ,so we put the value of a in c i.e. c=a, so c becomes 10 then we put a=b so a becomes 20. Now we put value of c that stored the value of a in b ,b=a b becomes 10. At last we print the values

Always Remember that the right hand side value of "=" sign gets stored in the left hand side.

Flowchart for Swapping two numbers without using third variable:

Flowchart for Swapping two numbers without using third variable
Remove WaterMark from Above Flowchart

Algorithm for Swapping two numbers without using third variable:

STEP 1: START STEP 2: ENTER A, B STEP 3: PRINT A, B STEP 4: A = A + B STEP 5: B= A - B STEP 6: A =A - B STEP 7: PRINT A, B STEP 8: END

Implementation of the Program in C:

#include<stdio.h> int main(){ int a,b,c; //Declaring the variables printf("Enter two variables before swapping:\n"); printf("a:"); scanf("%d",&a); printf("b:"); scanf("%d",&b); printf("Values stored in a:%d and b:%d",a,b); c=a; a=b; b=c; printf("\nValues after swapping a=%d and b=%d",a,b); }

Output of the program:

        

Don't Understand Algorithm and Flowchart? Learn quickly using our Expert's Notes



Want to get in-depth understanding of Algorithms?


Are You Good enough in Algorithms? Test it now?


Comments

1 comment
  • Ganta shivani

    Thank you it is useful



Search
Recommended Deals End in










Java Interview Questions For Beginner Notes
Quiz for Learners
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags

    Algorithm to Swap Two Integer Number

    Flowchart to Swap Two Integer Number

    Swap Number using Third Variable