Monday, August 20, 2018

SUM OF DIGITS

  • Problem Description

    " You're given an integer N. Write a program to calculate the sum of all the digits of N.
    Input

    The first line contains an integer T, total number of testcases. Then follow T lines, each line contains an integer N.
    Output

    Calculate the sum of digits of N.
    Constraints

    1 <= T <= 1000
    1 <= N <= 100000
    "
  • CODING ARENA
  • #include <stdio.h>
    int main()
    {
      int a,n,i,s=0,r;
      scanf("%d",&a);
      scanf("%d",&n);
      for(i=0;i<a;i++)
      {
        while(n)
        {
          r=n%10;
          s=s+r;
          n=n/10;
        }
        printf("%d",s);
      }
          
      

    return 0;
    }
  • Test Case 1

    Input (stdin)
    1
    
    12345
    
    
    Expected Output
    15
  • Test Case 2

    Input (stdin)
    1
    
    31203
    
    
    Expected Output
    9

No comments:

Post a Comment