Algorithm and Flowchart to Swap Two Integer Numbers with and without third variable

[333474 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:

                 



Want to Learn How to write own Algorithm and Flowcharts



Want to test your logical skills in Algorithms?


Health is Wealth? Check Best Medical Insurance for you:-



Comments

6 comment
  • Ganta shivani

    Thank you it is useful

  • max

    thx dad

  • Tamanna Ansari

    Thank you the flowcharts were quite helpful!

  • Jhansi

    It is very useful for the beginners Good explanation

  • J.kalaiyarasi

    Very nice thank you

  • Ayush

    Helpful










Search
Have Technical Doubts????


Hot Deals ends in













Technical Quizzes Specially For You:

Search Tags

    Algorithm to Swap Two Integer Number

    Flowchart to Swap Two Integer Number

    Swap Number using Third Variable

    Algorithm to swap two numbers without using temporary variable

    Flowchart for swapping two numbers without using temp variable