banner image

Swapping of 2 Numbers in C Programming

Swapping of 2 numbers refers to commutation of that particular numbers which is also known as reverse of that numbers. For instance the swapped value of of 928 is 829 in which the digits of a numbers are placed exactly opposite from previous number.

This is a question mostly asked in first semester of BIT course. By the way, there are 2 best ways to complete swapping in C programming - a.) Swapping the digits using third variable b.) Swapping without using third variable. And here we will learn both them simultaneously.

Swapping of 2 Numbers Using 3rd Variable.

In the first, we will swap 2 numbers using third variable, in which we use 3 int variable and that are integer values - f, s, temp. 'temp' is a temporary variable which is only use to store value of f and s temporarily. In the below figure you can see the swapping of 2 numbers.
Swapping two numbers using third variable

Swapping of 2 Numbers Without Using 3rd Variable

We have already get swapped value of a number using 3rd variable and that is little bit easier than get swapped value without using 3rd variable. Now, in second process without using 3rd variable we can also get swapped value of a 2 number where we only declared two variables ie a and b. Look the below figure for swapping code
Swapping two numbers without using third variable
These two types of swapping questions are frequently asked in C programming subject of first semester of Purbanchal University. 
Swapping of 2 Numbers in C Programming Swapping of 2 Numbers in C Programming Reviewed by Ariyal on September 05, 2015 Rating: 5

1 comment:

  1. To know the programming technique: "Make Your Program Trial Version in C" visit:
    udebayan.blogspot.com

    ReplyDelete

Post AD

Powered by Blogger.