‘C’ to calculate sum of digits of an integer.

C File =>


/* Write a function in ‘C’ to calculate sum of digits of an integer. Use this function in main */

#include<stdio.h>
int main() 
{
      int num;

     // defining function 
      void sum_digits(int n)
      {
           int r,sum=0;
           int num=n;
 
           while(n>0)
           {
               r=n%10;
               sum=sum+r;
               n=n/10;
            }
           printf("\n Sum of digits in %d is %d",num,sum);
       }
 
       printf("\n Enter number : ");
       scanf("%d",&num);
 
      //calling function 
       sum_digits(num);
       
       return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.