Reversing a number of unknown digits

Reversing a number of unknown digits - C Programming example

This C program code will take a number as an input and will return its reversed form as the output.

Reversing a number

C Programming Code

#include<stdio.h>

#include<conio.h>

main()

{

int n,reve;

printf("Input the number you want to reverse \n");

scanf("%d",&n);

reve=0;

while(n!=0)

{

reve=reve*10;

reve=reve+n%10;

n=n/10;

}

printf("The desired reversed number is %d",reve);

getch();

}

Output of the C program

Reversing a number output

Explanation of the C program

·#include<stdio.h>

#include<stdio.h>

ØThis are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.

·main()

ØIn C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.

·Int age,year;

Øint is a variable or data type. A variable declared to be of type int can be used for storing integers.. Here we declare “n” and “reve” variable in int type.

·printf() function

ØC uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen.

·scanf() function

ØThis function is used to get input from the user of the program. scanf(“%d,&age) is used here. So, this program will read in a integer value for “age” variable that the user enters on the keyboard (%d is for integer values. As there is “&n”, the number user enters will be the value of “n” variable).

reve=0;

Here we have declared before the loop that the value of reve is 0.

while(n!=0)

{

reve=reve*10;

reve=reve+n%10;

n=n/10;

}

printf("The desired reversed number is %d",reve);

While loop is used in this program to make this simple, easily executable and understandable.

The condition for the loop is n!=0 . In C programming != means not equals to. So basically the condition stands as long as n doesn’t become equal to 0.

Then the loop is started with { and then we have set that

reve = reve *10;

Reve = reve + n%10;

N= n/10

So lets take a number such as 1234.

So here reve was previously set as 0. So for the first turn

Reve = 0*10

Reve = 0+1234%10;

So here reve is = 1234%10 = 4 as the reminder is $ and as this is integer value the program won’t read any decimal point numbers.

Reve=4

And after that in the loop n is now = 123

N=123/10

And the loop goes on.

Then Reve = 4*10=40

Reve= 40+ 123%10=40+3=43

Then n=12

And in the same way the loop goes on.

Until n reaches the value of ) or less than one in this case as decimal point numbers isn’t accepted in integer so decimal point will also be considered as 0.

Then finally the loop is ended with}

printf("The desired reversed number is %d",reve);

getch();

And printf is written to show the output and finally getch(); is written to end the program.