Adsense 728 * 15

Search

Custom Search

Program to Calculate the Sum of Digits of a Number


Logic


1. Take a input number from user.

2.    while(num>0){        // If num is greater than zero

        /* Return modulus of a number */

        val = num%10;       
                 
                   /* Do the sum operation */

        sum = sum +val;

/*Divide by 10, if enter number is 456 then 
  after this operation num is 45 */     
        num = num / 10;    

    }  


C Program to Calculate the Sum of Digits of a Number

#include<stdio.h> void main() { int num,sum=0,val; printf("Enter number"); scanf("%d",&num); while(num>0){ val = num%10; sum = sum +val; num = num / 10; } printf("The sum of digits is %d",sum); }

No comments:

Post a Comment