Sum of digits

Sum of digits
Sum of digits
Sum of digits

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C programming concepts. You can learn C language at your own speed and time. One can learn concepts of C language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a C program to Sum of digits

PROGRAM: Sum of digits

/* Sum of digits using while loop */
#include <stdio.h>

int main()
{
 int n, s = 0;
 
 printf("Please enter a number: ");
 scanf("%d",&n);
 
 while (n > 0)
 {
  s = s + n%10;
  n = n/10;
 }
 
 printf("Sum of digits is: %d", s);
 return 0;
}

------------------------------------------------------

/* Sum of digits using for loop */
#include <stdio.h>
int main()
{
 int n, s;
 
 printf("Please enter a number: ");
 scanf("%d",&n);
 
 for(s=0;n>0;n=n/10)
 {
  s = s + n%10;
 }
 
 printf("Sum of digits is: %d", s);
 return 0;
}

Leave a Reply

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