Skip to lab content

UNIT 02 · Expression Evaluation

Digit sum and numeric palindrome

EXERCISE 02CC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a C program to find the sum of individual digits of a positive integer and test given number is palindrome.
Input format & conventions

One non-negative integer up to 999,999,999,999,999,999. The program also handles 0; leading zeros are not part of an integer value.

UNDERSTAND THE IDEA

Explanation

The remainder n % 10 extracts the last decimal digit. Dividing by 10 removes that digit.

Accumulate the digit sum and build the reversed number with reverse = reverse × 10 + digit. Preserve the original number before modifying the working copy. The 18-digit limit keeps the reversal within long long.

PLAN BEFORE CODING

Algorithm

  1. Save the original input and initialize sum and reverse to zero.
  2. Extract one digit with % 10.
  3. Add it to the sum and append it to the reverse.
  4. Remove the digit with integer division by 10.
  5. Compare the reverse with the original number.

SEE THE CONTROL FLOW

Flowchart

Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.

Flowchart for Digit sum and numeric palindrome: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#include <stdio.h>

int main(void) {
    long long number, remaining, reverse = 0;
    int sum = 0;
    if (scanf("%lld", &number) != 1 || number < 0 || number > 999999999999999999LL) {
        puts("Invalid input.");
        return 1;
    }
    remaining = number;
    do {
        int digit = (int)(remaining % 10);
        sum += digit;
        reverse = reverse * 10 + digit;
        remaining /= 10;
    } while (remaining != 0);
    printf("Digit sum: %d\n", sum);
    printf("Palindrome: %s\n", reverse == number ? "yes" : "no");
    return 0;
}
Open in compiler ↗

Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.

Compile and run locally
gcc -std=c17 digit-sum-palindrome.c -o lab
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Working value 121digit = 1sum = 1; reverse = 1
Working value 12digit = 2sum = 3; reverse = 12
Working value 1digit = 1sum = 4; reverse = 121

CHECK THE BEHAVIOR

Sample input & output

Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.

Sample 1

INPUT
121
OUTPUT
Digit sum: 4
Palindrome: yes

Sample 2

INPUT
1234
OUTPUT
Digit sum: 10
Palindrome: no

Sample 3

INPUT
0
OUTPUT
Digit sum: 0
Palindrome: yes

Common mistakes

  • Do not compare with the working value after it has become zero.
  • An unrestricted reversal can overflow the selected integer type.

WHY THIS GROWTH RATE?

Time and space complexity

Time O(digits); auxiliary space O(1).

Let d be the number of decimal digits. Every iteration extracts one digit with % 10, adds it to the sum, appends it to the reverse and removes it using / 10. Exactly d iterations are needed, including one iteration for zero.

A constant amount of work per digit gives O(d) time. For a positive value N, d = floor(log10 N) + 1, so the time can also be written O(log N). The reverse is stored as a number rather than an extra digit array, giving O(1) auxiliary space within the stated input limit.

Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.