Home > Software engineering >  Program working Fine in one compiler but showing error in codechef
Program working Fine in one compiler but showing error in codechef

Time:11-21

I am new to programming and was doing the beginner level problems at codechef. [Problem Code: FLOW006]

The question is as follows:

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

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

Here's the program i wrote:

#include <math.h>
#include <stdio.h>
int main(void) {
    // your code goes here
    int y = 1, T, N, x = 0, d, e, f, g, c;
    scanf("%d", &T);

    if (T <= 1000 && T >= 1) {
        for (x = 0; x < T; x  ) {
            c = pow(10, 1);
            scanf("%d", &N);
            if (N <= 1000000 && N >= 1) {
                e = 0;
                for (y = 0; c < N; y  ) {
                    c = pow(10, y   1);
                    f = N % c;
                    g = pow(10, y);
                    d = f / g;
                    e = e   d;
                }
                printf("%d\n", e);
            }
        }
    }
    return 0;
}

Now, this is working in my compiler but is showing wrong answer when i submit it at codechef. Where did I go wrong?

CodePudding user response:

  1. Do not use float numbers when you deal with integers.

Function summing the digits of the number is very simple without any floats, power operations, just using the division and modulus. This function handles also negative numbers.

int sumDigits(int x)
{
    int sum = 0;
    while(x)
    {
        sum  = abs(x % 10);
        x /= 10;
    }
    return sum;
}

And the test program:

int main(void)
{
    int T, N;
    
    if(scanf("%d", &T) != 1) { /* handle error */ }
    if(T >= 1 && T <= 1000)
    {
        while(T--)
        {
            if(scanf("%d", &N) != 1) { /* handle error */ }
            if(N > 0 && N <= 100000)
            {
                printf("The sum of digits of %d is %d\n", N, sumDigits(N));
            }
            else
            {
                /* handle wrong input */ 
            }
        }
    }
    else
    {
         /* handle wrong input */ 
    }
}

https://godbolt.org/z/KfqTPdfcx

and the result:

The sum of digits of 12345 is 15
The sum of digits of 31203 is 9
The sum of digits of 2123 is 8

CodePudding user response:

I tested your program and it's really wrong. There is a '{' wrong, so it performs different than you imagine. It also doesn't work for numbers with one digit, like 1, 2, 3.

Also, your program doesn't skip the lines between the answers, that's probably a problem too.

Try to find the errors for yourself; if you tried and could not come up with the solution, I will leave what I think is the correct one.

#include <stdio.h>
#include <math.h>
int main(void) {
    int y=1,T,N,x=0,d,e,f,g,c;
    scanf ("%d",&T);
    if (T<1000){
        for (x=0;x<T;x  )
        {
            c=0;
            scanf ("%d",&N);
            e=0;
            for (y=0;c<N;y  )
                if (N<1000000)
                {
                    c=pow(10,y 1);
                    f=N%c;
                    g=pow(10,y);
                    d=f/g;
                    e=e d;
                }
            printf ("%d\n",e);
        }
    }
    return 0;
}
  •  Tags:  
  • c
  • Related