Add 1 to each digit of a five digit number

If a five-digit number is input through the keyboard, write a program in C to print a new number by adding one to each of its digits. For example if the number that is input is 12391 then the output should be displayed as 23402.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
    long int num, add_num;
	int digit, next;

	printf("Enter the 5 digit number\t:");
	scanf("%ld",&num);

	//1st digit
	digit = (num/10000)%10;
	next = (digit+1)%10;
	add_num = next;
	//2nd digit
	digit = (num/1000)%10;
	next = (digit+1)%10;
	add_num=(add_num*10)+next;
	//3rd digit
	digit = (num/100)%10;
	next = (digit+1)%10;
	add_num=(add_num*10)+next;
	//4th digit
	digit = (num/10)%10;
	next = (digit+1)%10;
	add_num=(add_num*10)+next;
	//5th digit
	digit = num%10;
	next = (digit+1)%10;
	add_num=(add_num*10)+next;

	printf("After Addition your number %ld becomes %ld",num,add_num);

	getch();
}

Output

Enter the 5 digit number     :25699
After Addition your number 25699 becomes 36700

Explanation

A five digit number is taken as an input from the user in the variable named 'num'. %ld is the format specifier used for long int data type. A variable named 'add_num' is used to carry the number after adding 1 to each of its digit.

The logic used is as follows:
take out the first (leftmost) digit of the number.
Give it to variable 'digit'.
add 1 to the digit and then modulo it by 10.
If the digit is 9, then adding 1 to it gives 10.
But, we need 0 instead of 10.
So, for this after adding 1 to the digit, modulo operation with 10 is performed as 10 % 10 = 0.
any number from 1 to 9 modulo 10 will give the number itself
assign this value to add_num.
Take out the 2nd digit from left.
Give it to variable 'digit'.
add 1 to the digit and then modulo it by 10.
Now multiply add_num by 10 and add this new digit to it.
Do this till the last digit.


Let us understand this with the help of an example:
Let, num = 25699

digit = (num/10000)%10;
next = (digit+1)%10;
add_num = next;
digit = (25699/10000)%10 = 2 % 10 = 2
next = (2+1)%10 = 3 % 10 = 3
add_num = 3
digit = (num/1000)%10;
next = (digit+1)%10;
add_num = (add_num*10)+next;
digit = (25699/1000)%10 = 25 % 10 = 5
next = (5+1)%10 = 6 % 10 = 6
add_num = (3*10)+6 = 36
digit = (num/100)%10;
next = (digit+1)%10;
add_num = (add_num*10)+next;
digit = (25699/100)%10 = 256 % 10 = 6
next = (6+1)%10 = 7 % 10 = 7
add_num = (36*10)+7 = 367
digit = (num/10)%10;
next = (digit+1)%10;
add_num = (add_num*10)+next;
digit = (25699/10)%10 = 2569 % 10 = 9
next = (9+1)%10 = 10 % 10 = 0
add_num = (367*10)+0 = 3670
digit = num%10;
next = (digit+1)%10;
add_num = (add_num*10)+next;
digit = 25699 % 10 = 9
next = (9+1)%10 = 10 % 10 = 0
add_num = (3670*10)+0 = 36700

add_num = 36700