C Online Compiler
Example: Sum of Squares of Digits in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Sum of Squares of Digits #include <stdio.h> int main() { // Step 1: Declare variables int number; int sum_of_squares = 0; int digit; // Step 2: Prompt user for input printf("Enter an integer: "); scanf("%d", &number); // Store the original number for display later int original_number = number; // Step 3: Handle negative numbers by converting to positive for digit processing if (number < 0) { number = -number; } // Step 4: Loop to extract digits and calculate sum of squares while (number > 0) { // Extract the last digit digit = number % 10; // Square the digit and add to the sum sum_of_squares += digit * digit; // Remove the last digit from the number number /= 10; } // Step 5: Display the result printf("The sum of the squares of the digits of %d is: %d\n", original_number, sum_of_squares); return 0; }
Output
Clear
ADVERTISEMENTS