Painful Giveaway!

Shahwat is a very good problem solver. He is very famous in his city. In any problem, the mayor asks…

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

#include <stdio.h>

int main(){

    long int N,i;
    scanf("%li",&N);
    for(i = 0;i<=N;i++)
        printf("%li ",i);
    printf("\n");

    return 0;
}

What’s wrong with my code?

#include<stdio.h>

int main() {
    int N,number_of_digits,j,i;

    scanf("%d",&N);

number_of_digits=1;
for (j=1;j<=N;j*=10){
     number_of_digits += (N - j + 1); }

if (number_of_digits<=1000000){
    for(i=0;i<=N;i++){
    printf("%d ",i);

    }}
    return 0;
}

did i miss understand the last line ?? ( It’s guaranteed that the count of the total printed numbers will never exceed 10^6 .))

pls reply …

  • Your program won’t work if the input, N is negative (yes, N can be negative too)
  • You are printing a space after the last digit which is not allowed.
1 Like

As far as I can understand, the last line will only help you decide the int variable type you should choose (whether it can be int N, or long int N or long long int N or whatever). It’s nothing more than that.