Please help me in problem- PRACTICE

link - here is the link to the question. Can somebody plz help me, I’m stuck. My code is given below.

#include <bits/stdc++.h>

using namespace std;

int main() {
// your code goes here
int t;
cin>>t;
for(int i=0;i<t;i++){
int n;
int ones =0, tens=0, hundreds=0, thousands=0, tenthousands=0, tenlakhs=0, lakhs=0;
cin>>n;
switch (n){

        case ( 10 > n):
        cout<<n;
        break;
        
        case ( 9<n || n<100):
        ones = n%10;
        tens = n/10;
        tens = trunc(tens);
        cout<<ones+tens;
        break;
        
        case ( 99<n || n<1000):
        tens = trunc((n%100)/10);
        ones = tens%10;
        hundreds = trunc(n/100);
        cout<<ones+tens+hundreds;
        break;
        
        case ( 999<n || n<10000):
        hundreds = trunc((n%1000)/100);
        tens = trunc((n%100)/10);
        ones = n%10;
        thousands = trunc(n/1000);
        cout<<ones+tens+hundreds+thousands;
        break;
        
        case ( 999<n || n<10000):
        hundreds = trunc((n%1000)/100);
        tens = trunc((n%100)/10);
        ones = n%10;
        thousands = trunc((n%10000)/1000);
        tenthousands = trunc(n/10000);
        cout<<ones+tens+hundreds+thousands+tenthousands;
        break;
        
        case ( 999<n || n<10000):
        hundreds = trunc((n%1000)/100);
        tens = trunc((n%100)/10);
        ones = n%10;
        thousands = trunc((n%10000)/1000);
        tenthousands = trunc((n%100000)/10000);
        lakhs = trunc(n/100000);
        cout<<ones+tens+hundreds+thousands+tenthousands+lakhs;
        break;
        
        case ( 999<n || n<10000):
        hundreds = trunc((n%1000)/100);
        tens = trunc((n%100)/10);
        ones = n%10;
        thousands = trunc((n%10000)/1000);
        tenthousands = trunc((n%100000)/10000);
        lakhs = trunc((n%100000)/10000);
        tenlakhs = trunc(n/1000000);
        cout<<ones+tens+hundreds+thousands+tenthousands+tenlakhs;
        break;
        
    }
}
return 0;

}

It would be very grateful if you help me.

Bro I don’t understand you code but simple use single while loop until n >0 and
find remender and add this remender until n>0 .
see below my code

#include<bits/stdc++.h>
#define lli long long int
using namespace std;
int main()
{
    lli t;
    cin>>t;
    for(lli i=0;i<t;i++)
    {
        lli n,ans=0,rem=0;
        cin>>n;
        while(n>0)
        {
            rem=n%10;
            ans+=rem;
            n=n/10;
        }
        cout<<ans<<endl;
    }
}

According to the problem you should just find outthe sum of the digits of a number N
code in c++
#include
using namespace std;

void sum(){sum=0,
double n;
cin>>n;
while(n!=0){
sum=sum+n%10;
n=n/10;
}
cout<<sum;
}

int main() {
int t;
cin>>t;
while(t–){
sum();
}
return 0;
}

You see, this is why loops were invented. The code is very confusing compared to the question!

2 Likes

Oh i understood my problem. thank you​:blush::blush::smiley::smiley: