Write a Program to Calculate Sum of Digits of a Given Number in C

 


Flowchart  

 

 

 

 

Program  



#include <stdio.h>
#include <conio.h>
void main()
{
    int a, sum = 0, rem;
    printf("enter a value of a: ");
    scanf("%d", &a);
    while (0 < a)
    {
        rem = a % 10; // make a remainder
        a = a / 10; // divide a number
        sum = sum + rem; // sum
    }
    printf("Sum of the input number is %d \n", sum);
    getch();
}

  

output  

enter a value of a: 45

The Sum of the input number is 9 


Follow our page for regular updates.

0 Comments