Sum of digit in c

Problem Statement

Write a C program to sum of digits in an integer.

Solution

#include<stdio.h>      //Header File

int main()                    //Main function

{

    int num,sod=0,digit;

    scanf("%d",&num);      //Reading Input

    while(num>0)

    {

    digit=num%10;             //Getting Each digit from an input

    sod=sod+digit;              //Adding the digits

    num=num/10;            

    }

    printf("%d",sod);         //Printing Output

    return 0;

}

Input

12345 

Output  

15  

Sum of Digits
Sum of digits c program

                                                                                                                     

  

Rajesh

This Blog will help you to find out the c programs solution easily and also you can able to understand the program.Please follow our blog for more useful updates.

Post a Comment (0)
Previous Post Next Post