Write a C program to find the sum of individual digits of a positive integer and test given numberis palindrome.
Write a C program to find the sum of individual digits of a positive integer and test given numberis palindrome.
Algorithm
start
step1: sum := 0
step2: Read num
step3: temp := num
step4: while temp > 0 then
step5: digit := temp % 10
step6: sum := sum + digit
step7: reversed := reversed * 10 + digit
step8: temp := temp/10
step9: Print sum
step10:if num = teversed then
step11: Print "Num is a palindrome"
step12:else Print "Num is not a palindrome"
stop
Flowchart
C Programming
//sum of individual digits of a positive integer and test given numberis palindrome.
#include <stdio.h>
int main() {
int num,sum = 0,reversed,temp,digit;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp > 0) {
digit=temp%10;
sum += digit; // Add the last digit to sum
reversed = reversed * 10 + digit; // Build the reversed number
temp /= 10; // Remove the last digit
}
printf("Sum of individual digits: %d\n", sum);
// Check if given number and reversed are the same
if (num == reversed) {
printf("%d is a palindrome.\n", num);
} else {
printf("%d is not a palindrome.\n", num);
}
return 0;
}
OUTPUT
Enter a number: 12321
Sum of individual digits: 9
12321 is a palindrome.
๐ป Run This C Program
Write a C program to find the sum of individual digits of a positive integer and test given numberis palindrome.
The selected C program is loaded automatically. Edit the code,
provide input through STDIN and click Run.
The compiler is embedded using OneCompiler. Your program runs in
the embedded C environment; Study Glance does not execute C code
directly on your web server.