PART A · PRESCRIBED EXERCISE 04

Digit sum, reverse and palindrome

Understand the algorithm. Run the code.
Explain the result with confidence.

Read the programOpen the playground
Aim & definitionStep-by-step algorithmWorked sampleExtra test cases3 viva answers

LAB A04 · 01

Understand the task.

Aim: Digit sum, reverse and palindrome.

DEFINITION

Numeric palindrome. A nonnegative integer is a numeric palindrome when reversing its decimal digits produces the same integer value.

Review the related theory in Unit 2.

LAB A04 · 02

Know what goes in and what comes out.

One integer from 0 to 999999999. This bound keeps the reverse within the range of standard long. Leading zeros are not retained as part of an integer value.

Sample input

121

Expected output

Digit sum = 4
Reverse = 121
Palindrome = YES

Source programs read the documented input directly, without prompt text, so the same sample works in the embedded compiler’s STDIN field.

LAB A04 · 03

Plan the steps before writing code.

  1. Store the input in original and copy it into remaining. Initialize sum and reverse to zero.
  2. Extract the last digit with remaining % 10.
  3. Add the digit to sum and append it to reverse using reverse * 10 + digit.
  4. Remove the last digit with remaining / 10 and repeat while remaining is positive.
  5. Compare reverse with original and display all three results.

LAB A04 · 04

One complete program. Ready to run.

Digit sum, reverse and palindromemain.c
#include <stdio.h>
int main(void)
{
    long original, remaining, digit, sum = 0, reverse = 0;
    if (scanf("%ld", &original) != 1 || original < 0 || original > 999999999L) {
        puts("Enter an integer from 0 to 999999999.");
        return 1;
    }
    remaining = original;
    do {
        digit = remaining % 10;
        sum += digit;
        reverse = reverse * 10 + digit;
        remaining /= 10;
    } while (remaining > 0);
    printf("Digit sum = %ld\nReverse = %ld\n", sum, reverse);
    printf("Palindrome = %s\n", original == reverse ? "YES" : "NO");
    return 0;
}
EXPECTED OUTPUTDigit sum = 4 Reverse = 121 Palindrome = YES

Try this programDownload A04-digits-reverse-palindrome.c
Compile on your own computer
gcc A04-digits-reverse-palindrome.c -o program

Run the generated program and enter the sample input. The examples use standard C90-compatible syntax and explicit function declarations. For these learning programs, enter numeric values representable in the stated C types and within the documented ranges.

LAB A04 · 05

Connect each statement to its purpose.

  • The do-while loop processes zero once, correctly giving sum 0, reverse 0 and palindrome YES.
  • original stays unchanged because the loop repeatedly shortens remaining.
  • The reverse accumulator shifts previously collected digits one decimal position before appending the new digit.
  • A number ending in zeros loses those zeros at the front of its reversed numeric value: 1200 reverses to 21.

LAB A04 · 06

Follow the sample calculation.

Step or componentWhat happens
Startremaining 121; sum 0; reverse 0
Digit 1remaining 12; sum 1; reverse 1
Digit 2remaining 1; sum 3; reverse 12
Digit 1remaining 0; sum 4; reverse 121

LAB A04 · 07

Check more than one input.

Try this input or caseExpected behaviour
0Sum 0; reverse 0; YES.
12340Sum 10; reverse 4321; NO.
999999999Sum 81; same reverse; YES.

Time and storage

O(d) time for d decimal digits and O(1) extra space.

Start with the sample, then test a boundary and a case that changes the result. A correct sample alone does not establish that every input is handled correctly.

LAB A04 · 08

Use the real C compiler.

Choose an example and load it into the embedded C compiler. Select I/O inside the compiler, check the sample STDIN, and press Run. Change the input or edit the source to explore a different case.

Sample input (STDIN)

121

Expected output

Digit sum = 4
Reverse = 121
Palindrome = YES

Load the compiler when you’re ready to practise.

Your C workspace

Edit real C code and see real compiler output.

Compiler provided by OneCompiler. Opening the editor connects to that service; code and input you run are processed there. If the embed is unavailable, open the C compiler in a new tab and paste the source below.

View / copy the selected source
#include <stdio.h>
int main(void)
{
    long original, remaining, digit, sum = 0, reverse = 0;
    if (scanf("%ld", &original) != 1 || original < 0 || original > 999999999L) {
        puts("Enter an integer from 0 to 999999999.");
        return 1;
    }
    remaining = original;
    do {
        digit = remaining % 10;
        sum += digit;
        reverse = reverse * 10 + digit;
        remaining /= 10;
    } while (remaining > 0);
    printf("Digit sum = %ld\nReverse = %ld\n", sum, reverse);
    printf("Palindrome = %s\n", original == reverse ? "YES" : "NO");
    return 0;
}

LAB A04 · 09

Explain why the program works.

Why keep the original input?

The loop destroys the working copy by dividing it by 10. The saved original is required for the final comparison.

Why does % 10 extract a digit?

For a nonnegative decimal integer, the remainder after division by 10 is its units digit.

Is 00100 tested as a five-character palindrome?

No. Numeric input becomes the value 100. A string palindrome task would preserve characters and has a different algorithm.

LAB A04 · 10

Build the next skill.

Read the viva answers, try the extra cases, and return to the lab index to mark this exercise as practised.