Sum of all the digits of a Number

Spread the love

Problem Statement

Write a program to find the sum of all the digits of a number.

Code

// Sum of all the digits of a Number

#include <stdio.h>
int main(){
  int N,i,n,j;
  printf("How many numbers do you want to enter: \n");
  scanf("%d\n", &N);
  int a[N];
  printf("Enter the numbers: \n");
  for(i=0; i<N; i++){
    scanf("%d\n", &a[i]);
  }
  for (i=0; i<N; i++){
    int sum=0;
    while(a[i]!=0){
    n= a[i]%10;
    sum += n;
    a[i] /= 10; 
    }
    printf("%d\n", sum);
    }
/* Tech - https://tech.arclasses.net */
    return 0;
}

Sample Input

4
1234
456
12
1

Sample Output

10
15
3
1

Read more…


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *