C Program to Calculate Sum of Digits of Number Using For Loop

Hello friends! In this tutorial, I’m going to teach “C Program to Calculate Sum of Digits of Number Using For Loop” to you. Shall I start the tutorial to teach the possibilities below? Let’s go.

C Program to Calculate Sum of Digits of Number Using For Loop

C Program to Calculate Sum of Digits of Number Using For Loop

Source Codes

#include <stdio.h>
int main()
{
   int n, t, sum = 0, remainder;

   printf("Enter an integern");
   scanf("%d", &n);

   t = n;

   while (t != 0)
   {
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }

   printf("Sum of digits of %d = %dn", n, sum);

   return 0;
}
#include <stdio.h>
int main()
{
   int n, sum = 0, r;

   printf("Enter a numbern");

   for (scanf("%d", &n); n != 0; n = n/10) {
      r = n % 10;
      sum = sum + r;
   }

   printf("Sum of digits of a number = %dn", sum);

   return 0;
}

Final Words

I hope this article helps you to clarify about “C Program to Calculate Sum of Digits of Number Using For Loop“. If you face any issues please let me know via the comment section. Share this article with other C/C++ developers via social networks. Thank You!

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment