• Now Online : 38
  • admin@codemyne.net

Introduction

The program shows how to calculate sum of digits in a number. Here while loop is used.  For example take the number as 345 then sum of digits is 12.

#include<stdio.h>
#include<conio.h>
        void main()
        {
            int n,sum=0,remainder;
            printf("/n Enter a number:");
            scanf("%d",&n);
            while(n!=0)
            {
                remainder=n%10;
                sum=sum+remainder;
                n=n/10;
            }
            printf("sum=%d",sum);
        }

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment