Program to delete duplicates from a link list

Sum of Digits | Codechef solution

 Problem (Sum of Digits | Codechef solution):-

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

Input of Sum of Digits | Codechef solution

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

Output of Sum of Digits | Codechef solution

For each test case, calculate the sum of digits of N, and display it in a new line.

Constraints

  •  T  1000
  •  N  1000000

Example

Input
3 
12345
31203
2123
Output
15
9
8

Code:-

#include <iostream>
using namespace std;

int main() {
    int n,a,r,digit_sum=0;
    cin>>n;
    while(n--)
    {
        cin>>a;
        digit_sum=0;
        while(a!=0)
        {
            r=a%10;
            digit_sum+=r;
            a=a/10;
        }
        cout<<digit_sum<<endl;
    }
   
    return 0;
}

Recommended Post:

codechef problems:-

 

 

Wipro :-

 

 

Infytq :-

 

 

Key Points;-

Hackerrank:-

C-tutorial:-

 

See more:-

 

Leave a Reply

Your email address will not be published.