C Program to find sum of two numbers



#include<stdio.h>
#include<conio.h>
void main()
{
int number,reverse=0,d;
clrscr();
printf("\nEnter number:");
scanf("%d",&number);
printf("Reverse the number %d",number);
while(number>0)
{
d=number%10;
number=number/10;
reverse=reverse*10+d;
}
printf("\nReverse number:%d",reverse);
getch();
}

Explanation:

The above program will print the reverse of a given number. For example if you a insert number as 452 then the output will be 254.We are using some simple arithmetic operations in the above program to find reverse of the given number. In order to reverse the number we are using while loop. This while loop will be run till the number does not become 0. For getting the reverse of a number we have to extract the digits of given number one by one. '%' (modulus) operator is used here for extracting the digits of the given number (Modulus operator is used for the purpose of finding remainder in division operation).After extracting the digit we can remove that digit from the original number by using '/' (division) operator. You can understand the above program with the help of following example:

number=452,reverse=0 number>0
iteration1:
d=(number%10)=452%10=2
number=(number/10)=452/10=45
reverse=(reverse*10+d)=0*10+2=2
number>0
iteration2:
d=45%10=5
number=45/10=4
reverse=2*10+5=25
number>0
Iteration 3:
d=4%10=4
number=4/10=0
reverse=25*10+4=254
number!>0
So finally reverse=254
The output of the above program is given below:

Output:

c program