Posts

Showing posts from June, 2020

Obtain the sum of the first and last digits of this number.

Write a program to obtain the sum of the first and last digits of this number. Solution: #include <iostream> using namespace std; int main() {     // your code goes here     int t;     cin>>t;         for(int i=0; i<t; i++)     {         int n;         cin>>n;         if (n<10)         {             cout<<n<<endl;         }         else{         int last = n%10;// get the last digit             while(n>9)         {             n/=10;// get first digit     ...

Sum of digits in the given number.

Write a program to calculate the sum of all the digits of N . N is an integer. Input The first line contains an integer T , the total number of testcases. Then follow T lines, each line contains an integer N . Output For each test case, calculate the sum of digits of N , and display it in a new line. Constraints 1 ≤ T ≤ 1000 1 ≤ N ≤ 1000000 Example Input 3 12345 31203 2123 Output 15 9 8 Solution:  C++: #include <iostream> using namespace std; int main() {     // your code goes here     int t;     cin>>t;         for(int i=0; i<t; i++)     {         int n;         cin>>n;         int sum=0;         while(n>0)     {         sum+=n%10;        ...