Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link

Sum of Digits : FLOW006 | CodeChef Solution

1 min read



Problem

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

Input Format

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

Output Format

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


Constraints

  •  T  1000
  •  N  1000000
  • 1 \leq T \leq 1000

Sample 1:

Input
Output
3 
12345
31203
2123
15
9
8

Solution

#include <iostream>
using namespace std;

// Solution from : Code Radius [ https://radiuscode.blogspot.com/ ]

int main()
{
int t;
cin>>t;

while(t--)
{
int n;
cin>>n;

int sum=0;

while(n>0)
{
sum+=(n%10);
n/=10;
}

cout<<sum<<"\n";

}
return 0;

}

Please First Try to Solve Problem by Yourself.

You may like these posts

  • ProblemChef has NN slippers, LL of which are left slippers and the rest are right slippers. Slippers must always be sold in pairs, where each pair contains one left and one right s…
  • ObjectiveThis is a simple challenge to help you practice creating variables and printing to stdout. You may also want to complete Solve Me First in Python before attempting this ch…
  • ProblemYou and your friend are playing a game with hoops. There are NN hoops (where NN is odd) in a row. You jump into hoop 11, and your friend jumps into hoop NN. Then you jump in…
  • ProblemAlice and Bob are meeting after a long time. As usual they love to play some math games. This times Alice takes the call and decides the game. The game is very simple, Alice…
  • Problem Gru has not been in the limelight for a long time and is, therefore, planning something particularly nefarious. Frustrated by his minions' incapability which has kept him…
  • ProblemYou're given an integer N. Write a program to calculate the sum of all the digits of N.Input FormatThe first line contains an integer T, the total number of testcases. Then …

Post a Comment