1//program to find the sum of digits:
2
3#include<stdio.h>
4int main()
5{
6 int num,sum=0,r,temp;
7 printf("Enter the number:\n "); //taking input from the user
8 scanf("%d",&num);
9
10 temp=num; //assigning num to temporary variable
11
12 while(temp!=0)
13 {
14 r=temp%10;
15 sum=sum+r;
16 temp=temp/10;
17 }
18 printf("\nGiven number = %d",num);
19 printf("\nSum of the digits = %d",sum);
20}
21
22//code By dungriyal
1#include <stdio.h>
2int main()
3{
4 int n, sum = 0, r;
5
6 printf("Enter a number\n");
7
8 for (scanf("%d", &n); n != 0; n = n/10) {
9 r = n % 10;
10 sum = sum + r;
11 }
12
13 printf("Sum of digits of a number = %d\n", sum);
14
15 return 0;
16}
1#include <stdio.h>
2
3int individualSum(num);
4
5void main ()
6{
7 int num, ret;
8
9 ret = individualSum(num);
10 printf("\nThe sum of individual digit is %d\n", ret);
11}
12
13int individualSum(num)
14{
15 int i, rem, sum = 0;
16 printf("Enter the number: ");
17 scanf("%d",&num);
18
19 while(num!=0)
20 {
21 rem = num % 10;
22 num = num /10;
23 sum = sum + rem;
24 }
25
26 return sum;
27}
28
1Enter the number
2300
3Given number = 300
4Sum of the digits 300 = 3
5
6
7Enter the number
816789
9Given number = 16789
10Sum of the digits 16789 = 31