-1

I tried this problem using for loop but i did not got the correct output as specified . i do not know what is the problem in my code ?

#include <stdio.h>
int main()
{    
    int T,N;    
    scanf("%d %d" ,&T,&N);

    for(int i=N;i>0;i=i/10)
    {
        int r=N%10;
        int sum=0;
        sum=sum+r;
        printf("The sum is : %d" ,sum);   
    }    
}

The sum is : 5The sum is : 5The sum is : 5The sum is : 5The sum is : 5

The output is coming like this while we just need sum of digits printed

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • 3
    Sounds like you want the print to be outside of the loop instead. – Blaze Sep 12 '19 at 06:33
  • You do the same calculation over and over again. `sum` will always be equal to `N%10` so you print the same value in every iteration of the loop. – Lukas-T Sep 12 '19 at 06:36
  • @churill Sir , I didn't got you. – Sandarbh Raj Sep 12 '19 at 06:39
  • @Someprogrammerdude I tried my best to ask the question in good manner. Please help me if there is something wrong – Sandarbh Raj Sep 12 '19 at 06:39
  • And by the way, you can find *working* code for this problem with 5 seconds of googling. – Lukas-T Sep 12 '19 at 06:52
  • @ThomasSablik it's C . Used C++ by mistake . Sorry – Sandarbh Raj Sep 12 '19 at 07:04
  • @Downvoters please don't downvote on the subjective notion of obviousness. This code is well-presented, compiles, and the OP has taken time to document the actual and expected behaviour. Yes, as very many questions posted here can be answered, this one is immediately addressable with a line-by-line debugger. – Bathsheba Sep 12 '19 at 07:08
  • Also @RitikSrivastava you'd do well to accept the oldest answer, which explains it well. – Bathsheba Sep 12 '19 at 07:15

1 Answers1

3
for(int i = N; i > 0 ; i = i/10)
{
    int r = N % 10; // calculating remainder of UNMODIFIED input, so will
                    // ALWAYS be last digit

    int sum = 0;    // you are initializing the sum to 0 for every single iteration

    sum = sum + r;  // so this will *always* result in 0 + N % 10

    printf("The sum is : %d", sum);
}

To fix, you need to initialise the sum just once to collect all of the single digits. Additionally, you need to use the modified value:

int sum = 0;
for(int i = N; i > 0 ; i = i/10)
{
    int r = i % 10;
    //      ^  (!)
    sum += r; // alternative variant...
    printf("The sum is : %d\n", sum);
    //                     ^^ for better output formatting
}

Until now we are still printing the sum with every iteration as well. That might be useful, if you want to follow how the sum evolves (assuming input was 1210):

The sum is 0
The sum is 1
The sum is 3
The sum is 4

But actually, you'd rather want to print only the result, wouldn't you? So you'd move the printing out of the loop as well:

for(...)
{
    ...
}
printf("The sum is : %d\n", sum);

Alternative variant: If you don't need the value of N afterwards any more anyway, you can iterate directly on it:

for(  ; N > 0; N /= 10)
//  ^ empty initialization, nothing to be done...
{
    int r = N % 10; // NOW using N is fine...
    ...
}

Finally: if you compare with != instead of >, you can cover negative intput (as you use signed integers...) as well.

Edit according to question:

it asked input and output like this. Input 3 12345 31203 2123 Output 15 9 8

Well, in this case, you need a double loop:

int t;
// well, actually, you should check if you did get correct input:
if(scanf("%d", &t) != 1))
{
    // invalid input
    // appropriate error handling, e. g. printing a message and:
    return -1;
}

for( ; t > 0; --t) // handles the number of tasks to solve
{
    int n; // inside loop: read in a new value with every task
    scanf("%d", &n); // TODO: check input, see above

    int sum = 0;
    for(...) { ... } // loop handling the input value, see above
    printf(...);
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • i really appreciate your effort to guide me . i want to ask a doubt . i was solving this on codechef , i made the required changes u suggested but still its saying wrong answer when i submit . See it asked input and output like this. Input 3 12345 31203 2123 Output 15 9 8 . where 3 is the number of test cases we need to take as input – Sandarbh Raj Sep 12 '19 at 12:50
  • #include int main() { int T; scanf("%d" ,&T); int N; scanf("%d" ,&N); int sum=0; for(int i=N;i>0;i=i/10) { int r=i%10; sum=sum+r; continue; } printf("%d\n" ,sum); } – Sandarbh Raj Sep 12 '19 at 12:52
  • @RitikSrivastava See edits; in your comment: Why would you want a continue inside the loop? There's nothing to skip afterwards anyway... – Aconcagua Sep 12 '19 at 14:14