C Program to find Sum of Digits of a Number

Get Certified in C Programming for Free and Take Your Skills to the Next Level

In programming, there are often situations where you need to manipulate individual digits of a number. One common operation is to find the sum of the individual digits of a given number. In this article, we will explore a C program to achieve this task and discuss alternative approaches to solving it.

Ways to find Sum of digits of a Number in C

Method 1:

#include <stdio.h>
int main() {
int number, sum = 0, digit;
printf("Enter a number: "); 
scanf("%d", &number);
while (number != 0) 
{ 
digit = number % 10; 
sum += digit;
number /= 10;
}
printf("Sum of the individual digits: %d\n", sum);
return 0;
}

In the above program, we declare three variables: `number`, `sum`, and `digit`. The `number` variable is used to store the input number from the user. The `sum` variable is initialized to zero and will be used to accumulate the sum of the digits. The `digit` variable is used to store the individual digits extracted from the number.

The program prompts the user to enter a number using the `printf` and `scanf` functions. Inside the `while` loop, we extract the last digit of the number using the modulo operator `%`. We then add the extracted digit to the `sum` variable. Finally, we divide the number by 10 to remove the last digit, and the process continues until the number becomes zero.

Once the loop finishes, the program prints the sum of the individual digits using the `printf` function.

Method 2:

Now let’s consider an alternative approach to solving this problem using string manipulation. This method involves converting the number to a string and then iterating over the characters of the string to calculate the sum of the digits.

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
int main() {
char number_str[20];
int sum = 0;
printf("Enter a number: ");
scanf("%s", number_str);
for (int i = 0; i < strlen(number_str); i++) {
sum += number_str[i] - '0'; 
}
printf("Sum of the individual digits: %d\n", sum);
return 0;
}

In this alternative approach, we declare a character array `number_str` to store the number as a string. We also declare a variable `sum` to accumulate the sum of the digits.

The program prompts the user to enter a number using the `printf` and `scanf` functions. We then iterate over each character of the string using a `for` loop. Inside the loop, we convert each character to its corresponding digit by subtracting the character `’0’`. This conversion works because in the ASCII character set, the characters `’0’` to `’9’` are consecutive. We add the converted digit to the `sum` variable.

Finally, the program prints the sum of the individual digits using the `printf` function.

Both the approaches discussed above yield the same result for finding the sum of the individual digits of a number. The choice between them depends on the specific requirements of your program or personal preference.

Output:

Enter a number: 123456
Sum of the individual digits: 21

Conclusion:

In conclusion, we have explored two different approaches to finding the sum of the individual digits of a number in C. The first approach uses arithmetic operations to extract and manipulate digits, while the second approach converts the number to a string and operates on its characters. Both methods provide reliable ways to solve this problem, and you can choose the one that best suits your needs.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *