Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

Patterns

Excersises


C Program to Sum of Digits using While Loop


The following example shows, how to sum all digits using while loop in C program.

Example : pgm.c

#include<stdio.h>
int main()
{
  long int num,digit,sum=0;
  printf("\nEnter any number:");
  scanf("%ld",&num);//123
  while(num!=0)
  {
    digit=num%10;
    num=num/10;
    sum=sum+digit;
  }
  printf("\nSum is : %ld",sum);
  return 0;
}

Output:

Enter any number:12345

Sum is : 15