C Programming
We will be discussing about the c program to reverse a given number.
C program to reverse a given number.

- 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 (&n).
- getch - used to hold the screen until the user gives any type of input.
- \n is used to print next statement in next line in output screen.
- I have used while loop.
logic behind the program
- while conditions: the given number n, should be greater then 1. if condition is true, it execute the follow statement,
- a=n%10, the given number is divide bt 10 and the reminder is considered as answer.
- r=r*10+a; r is multiplied by 10 and added with the a.
- n=n/10; n is divided with 10 and the loop is continues until n less than 1.
- The loop is ended when the condition is false.
Manually:
n=9876
a=9876%10= 6
r=0*10+ 6= 6
=> r=6
n=9876/10= 987.6
since 987.6 >1 we take only 987, because we consider integer value.
n=987
a=987%10=7
r=0*10+ 7= 7
=> r=7
n=987/10=98.6
since 98.7 >1, we take only 98, because we consider integer value.
n=98
a=98%10=8
r=0*10+ 8= 7
=> r=8
n=98/10=9.8
since 9.8 >1, we take only 9 because we consider integer value.
n=9
a=9%10=9
r=0*10+ 9=9
=> r=9
n=9/10=0.9
since 0.9 <1, the condition will be false.
To know more about while loop. click here while loop
Output:
The above program is used to reverse the given number, it is also an example for while loop.
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