Program to print sum of digits of a number in C

Program Requirement:
Enter the number n example n=345
sum= 3+ 4 + 5 =12

Algorithm

  1. Enter the number as n
  2. Initially assign sum=0
  3. while(n>0)
  4. {
  5. r=n%10;
  6. sum=sum+r;
  7. n=n/10;
  8. }
  9. Print sum

Explanation:
1) n = 345
2) sum = 0
3) a) check if 345 > 0 returns true
r = n % 10 = 345 % 10 = 5
sum=sum + r = 0 + 5 = 5
n = n / 10 = 345 / 10 = 34
b) check if 34 > 0 returns true
r = n % 10 = 34 % 10 = 4
sum=sum + r = 5 + 4 = 9
n = n / 10 = 34 / 10 = 3
c) check if 3 > 0 returns true
r = n % 10 = 3 % 10 = 3
sum=sum + r = 9 + 3 = 12
n = n / 10 = 3 / 10 = 0
d) check if 0 > 0 returns false
4) Print sum ie 12

Program (Without Function)

#include<stdio.h>
int main()
{
     printf("*********************************************************");
    printf("\n**********************************************************");
    printf("\n**      WAP to print sum of digits of a number          **");
    printf("\n**              Created by Sheetal Garg                 **");
    printf("\n**                Assistant Professor                   **");
    printf("\n**                Phone No:9467863365                   **");
    printf("\n**********************************************************");
    printf("\n**********************************************************\n");
    int i,n,r,s=0;
    printf("Enter the no n \n");
    scanf("%d",&n);
   while(n>0)
   {
       r=n%10;
       n=n/10;
       s=s+r;
   }
   printf("sum is %d",s);
   return 0;
}

Output

***************************************************************************
***************************************************************************
**                    WAP to print sum of digits of a number             **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter the no n
345
sum is 12

Program (Using Function)

#include <stdio.h>
int find_sum_of_digits(int n);
int main()
{
    printf("*********************************************************");
    printf("\n**********************************************************");
    printf("\n**      WAP to print sum of digits of a number          **");
    printf("\n**              Created by Sheetal Garg                 **");
    printf("\n**                Assistant Professor                   **");
    printf("\n**                Phone No:9467863365                   **");
    printf("\n**********************************************************");
    printf("\n**********************************************************\n");
    int i, n;
    printf("Enter the no n \n");
    scanf("%d", &n);
    printf("sum is %d", find_sum_of_digits(n));
    return 0;
}
int find_sum_of_digits(int n)
{
    int r,s = 0;
    while (n > 0)
    {
        r = n % 10;
        n = n / 10;
        s = s + r;
    }
    return s;
}

Output

***************************************************************************
***************************************************************************
**                    WAP to print sum of digits of a number             **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter the no n
345
sum is 12

Program (Using Recursive Function)

#include <stdio.h>
int find_sum_of_digits(int n);
int main()
{
    printf("*********************************************************");
    printf("\n**********************************************************");
    printf("\n**      WAP to print sum of digits of a number          **");
    printf("\n**              Created by Sheetal Garg                 **");
    printf("\n**                Assistant Professor                   **");
    printf("\n**                Phone No:9467863365                   **");
    printf("\n**********************************************************");
    printf("\n**********************************************************\n");
    int i, n;
    printf("Enter the no n \n");
    scanf("%d", &n);
    printf("sum is %d", find_sum_of_digits(n));
    return 0;
}
int find_sum_of_digits(int n)
{
    int r;
    if (n == 0)
        return 0;
    else
    {
        r = n % 10;
        n = n / 10;
        return r + find_sum_of_digits(n);
    }    
}

Output

***************************************************************************
***************************************************************************
**                    WAP to print sum of digits of a number             **
**                          Created by Sheetal Garg                      **
**                            Assistant Professor                        **
**                            Phone No:9467863365                        **
***************************************************************************
***************************************************************************
Enter the no n
345
sum is 12
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.