C Programming
We will be discussing about the c program to swap two numbers. But first we should know what is swapping?
Swapping is the exchange of values in the variable.For example, variable 'a' is assigned with value '20' and 'b' variable with '90', after swapping the valve of a is 90 and b is 20.
I will explain the logic below after the program.
C program to Swap 2 numbers.
Explaination:
- Header file stdio.h - standard input and output to get input and show output, conio.h - console input and output.
- void main - function returning value will be null.
- int - declare the type of variable.
- clrscr - clear the screen.
- printf - used to print the output screen.
- scanf - used to accept input (%d), and read (&a,&b).
- getch - used to hold the screen until the user gives any type of input.
Output:
logic behind the program: In the above program, a is assigned to the value 5(a=5), b is assigned to the value 8(b=8).
a=a+b; \* a=5+8=13*\ the value of 'a' becomes 13.
b=a-b; \* b=13-8=5*\ thus, the valve of 'b' becomes 5.
a=a-b; \* a=13-5=8*\ thus, finally the value of 'a' becomes 13. In this way swapping of two numbers is done.
In following days we will discuss more programs in detail. If you like to learn more just follow me regularly. If you have any question to ask mention below in comment.Thank you......


Comments
Post a Comment