Write a C Program to Compute the Sum of Digits of a Given Integer

July 12, 2022 0 Comments

In this tutorial learn how you can write a c program to compute the sum of digits of a given integer.

We are using a while loop and some maths tricks for this tutorial. Some tricks are below:

Given integer % 10 to get the last digit.

And Given integer / 10 to remove the last digit.

And repeat the process through the while loop.

C programming is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, etc.


write c program to compute sum of digits of a given integer


Write a Program to Compute the Sum of Digits of a Given Integer

Here is the code to compute the sum of digits of a given integer in C programming.

#include <stdio.h> //WAP to calculate  factorial of  a number. int main() {     int num, i, fact = 1;     printf("Please enter any integer:");     scanf("%d", &num);     for (i = 1; i <= num; i++)     {         if (num <= 0)             printf("The factorial not possible");         else             fact = fact * i;     }     printf("The factorial of %d is %d.", num, fact);      return 0; }

Test Live At Jdoodle



Conclusion

This is how you can compute the sum of digits of a given integer in c programming. Comment below if you like our tutorial.

Leave a comment

Gallery