Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 11

Sum of digits and palindrome number check

CExpression evaluationJNTUH R22 · CS108ES

Aim

To write a C program that reads a positive integer, finds the sum of its individual digits, and checks whether the number is a palindrome (reads the same backwards).

Algorithm

  1. Read the number n. If it is not positive, print a message and stop.
  2. Copy n into temp. Set sum = 0 and reverse = 0.
  3. While temp is greater than 0:
    • Take the last digit: digit = temp % 10.
    • Add it to the sum: sum = sum + digit.
    • Add it to the reverse: reverse = reverse × 10 + digit.
    • Remove the last digit: temp = temp / 10.
  4. Print the sum of digits.
  5. If reverse equals n, print that n is a palindrome; otherwise print that it is not.

Program

C
#include <stdio.h>

int main(void) {
    int n;
    printf("Enter a positive integer: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    if (n <= 0) {
        printf("The number must be positive\n");
        return 1;
    }

    int temp = n, sum = 0, reverse = 0;
    while (temp > 0) {
        int digit = temp % 10;    /* last digit */
        sum = sum + digit;
        reverse = reverse * 10 + digit;
        temp = temp / 10;         /* remove the last digit */
    }

    printf("Sum of digits = %d\n", sum);
    printf("Reverse       = %d\n", reverse);
    if (reverse == n) {
        printf("%d is a palindrome\n", n);
    } else {
        printf("%d is not a palindrome\n", n);
    }
    return 0;
}

Output

A palindrome

Output 1
Enter a positive integer: 12321
Sum of digits = 9
Reverse       = 12321
12321 is a palindrome

Not a palindrome

Output 2
Enter a positive integer: 4567
Sum of digits = 22
Reverse       = 7654
4567 is not a palindrome

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

n % 10 gives the last digit of a number and n / 10 removes it, so the loop visits the digits from right to left. For 4567 the digits come out as 7, 6, 5, 4: the sum is 22 and the reverse is 7654.

The loop works on a copy (temp), because the original n is needed at the end to compare with the reverse.

An int can hold values up to 2147483647 on common compilers. The reverse of a large 10-digit number can be bigger than that, so this program is meant for numbers of up to 9 digits.

Common mistakes

  • Running the loop on n itself. After the loop n is 0, so the comparison reverse == n is always false. Work on a copy.

  • Forgetting to set sum and reverse to 0 before the loop. Local variables start with garbage values in C.

  • Writing reverse = digit * 10 + reverse. The old reverse must be shifted one place left first: reverse = reverse * 10 + digit.

  • Using temp >= 0 as the loop condition. temp stays 0 after the last digit, so the loop never ends.

Viva questions

Try to answer each one aloud before you open it.

  • How do you get the last digit of a number, and how do you remove it?

    n % 10 gives the last digit, because it is the remainder when dividing by 10. n / 10 removes the last digit, because integer division drops the remainder.

  • What is a palindrome number?

    A number that reads the same from left to right and from right to left, such as 121 or 12321.

  • Why is a copy of the number used in the loop?

    The loop divides the number by 10 until it becomes 0. The original value is still needed afterwards to compare with the reverse.

  • What is the sum of digits of 12321?

    1 + 2 + 3 + 2 + 1 = 9.

  • How many times does the loop run?

    Once for each digit of the number, so 5 times for 12321.

Learn the concepts

All Programming for Problem Solving Lab experiments →