This C program adds 1 to each digit of a given number. When a digit is not 9, it simply increases by 1: 3→4, 5→6. When a digit is 9, it wraps to 0 and carries +1 to the next digit to the left. This carry propagates: if the next digit is also 9, it too wraps to 0 and carries further. The carry from one digit’s result adds to the next digit’s own +1.
Examples: 12345 → 23456 (no 9s, each digit just +1). 3491 → 4602 (the 9 wraps to 0 and its carry adds to the 4, making it 4+1+1=6). 999 → 1110 (all 9s cascade into 1s with an extra leading digit).
How It Works — Step by Step
Trace 3491:
| Position | Digit | +1 own | Carry in | Sum | Result digit | Carry out |
|---|---|---|---|---|---|---|
| Units | 1 | +1 | 0 | 2 | 2 | 0 |
| Tens | 9 | +1 | 0 | 10 | 0 | 1 |
| Hundreds | 4 | +1 | 1 | 6 | 6 | 0 |
| Thousands | 3 | +1 | 0 | 4 | 4 | 0 |
Result digits (most significant to least): 4, 6, 0, 2 → 4602
Trace 999 (all nines):
| Position | Digit | +1 own | Carry in | Sum | Result digit | Carry out |
|---|---|---|---|---|---|---|
| Units | 9 | +1 | 0 | 10 | 0 | 1 |
| Tens | 9 | +1 | 1 | 11 | 1 | 1 |
| Hundreds | 9 | +1 | 1 | 11 | 1 | 1 |
| Extra | — | — | 1 | 1 | 1 | 0 |
Result: 1110 (4 digits from a 3-digit input)
C Program to Add One to Each Digit
/* Add one to each digit of a number in C
* When a digit is 9, it wraps to 0 and carries +1 to the next digit left.
* Compile: gcc -ansi -Wall -Wextra add_one_digits.c -o add_one_digits */
#include <stdio.h>
int main(void)
{
int num, digits[20], result[21];
int len = 0, rlen, carry = 0, i, sum;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}
/* extract digits least-significant first */
while (num > 0) {
digits[len++] = num % 10;
num /= 10;
}
/* add 1 to each digit, propagate carry left */
for (i = 0; i < len; i++) {
sum = digits[i] + 1 + carry;
result[i] = sum % 10;
carry = sum / 10;
}
rlen = len;
if (carry) result[rlen++] = carry;
/* print from most-significant to least-significant */
printf("Result: ");
for (i = rlen - 1; i >= 0; i--)
printf("%d", result[i]);
printf("\n");
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra add_one_digits.c -o add_one_digits
./add_one_digits
Sample Input and Output
Enter a positive integer: 12345 Result: 23456
Enter a positive integer: 3491 Result: 4602
Enter a positive integer: 19 Result: 30
(1→2, 9 wraps to 0 with carry; the carry adds to the 2 making it 3 → 30)
Enter a positive integer: 999 Result: 1110
Enter a positive integer: 9 Result: 10
Code Explanation
- digits[len++] = num % 10 — extracts the least significant digit first. The array is filled from index 0 (units) upward. For 3491: digits = [1, 9, 4, 3].
- sum = digits[i] + 1 + carry — every digit gets its own +1 plus any carry from the previous position. On the first iteration, carry = 0.
- result[i] = sum % 10 — keeps only the units digit of the sum. If sum = 10, the result digit is 0 and carry becomes 1.
- carry = sum / 10 — extracts the tens digit of sum. This is 0 unless sum ≥ 10 (which only happens when digit = 9 and carry_in makes sum = 10 or 11). Since digits are at most 9 and carry is at most 1, sum ≤ 11, so carry is always 0 or 1.
- if (carry) result[rlen++] = carry — after processing all digits, a remaining carry of 1 becomes a new leading digit. This is what turns 999 into 1110.
- Reverse print loop — digits were collected least-significant first, so printing from
rlen-1down to 0 gives the correct left-to-right order.
Why the Original Approach Was Wrong
A common buggy approach builds the result digit-by-digit using a flag variable that only tracks one level of carry. For consecutive 9s like 999, that single flag gets overwritten on the second 9, and the accumulated carry is lost — producing 0 instead of 1110. The array-based approach above correctly propagates carry through any sequence of 9s.
What This Program Teaches
- Digit extraction and reconstruction — the
% 10// 10loop extracts digits least-significant first. A separate loop prints them in the correct (reversed) order. This two-phase pattern appears in number reversal, digit sum, and decimal-to-binary conversion programs. - Carry propagation — adding 1 to a digit and propagating overflow is the fundamental operation of binary adders and multi-digit arithmetic. The same pattern applies to adding two arbitrary integers digit by digit.
- Array sizing — the result array needs one extra element (
result[21]) to hold a potential extra carry digit. Forgetting this is a classic off-by-one buffer overflow. - Why carry ≤ 1 — each digit is at most 9, plus own +1, plus at most 1 incoming carry = 11. So carry out is always 11/10 = 1 or less. This guarantees the carry stays single-bit throughout the loop.
Related Programs
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.
2 comments on “Add One to Each Digit of a Number in C”
This program uses a user defined function ‘getSumOfDigit’ to find the sum of digits of a number.
hey!
I can’t see that function call here!
Did you post the comment on a wrong program?