-1

My problem is when I testcase the first test that want only up to five how can make my code stop there? you'll see what I mean in the picture the test case was up to 15 which was correct

int main() {

    for(int i=1;i<=15;i++){
        if(i%3==0 && i%15==0)cout<<"FizzBuzz"<<endl;
        else if(i%5==0) cout<<"Buzz"<<endl;
        else if(i%3==0) cout<<"Fizz"<<endl;
        else if(i%15==0) cout<<"FizzBuzz"<<endl;
        else cout<<i<<endl;
    }
    return 0;
}

Expected output 1 2 Fizz 4 Buzz

my codes output is until 15: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz

  • 2
    Your loop shouldn't be [hard-coded](https://stackoverflow.com/questions/1895789/what-does-hard-coded-mean) to end at 15. Hackerrank is providing you input which is supposed to read and be used in your code. – TrebledJ Mar 02 '19 at 06:35
  • 1
    @TrebuchetMS I consider that to be a decent answer. Note that in cases of beginner questions like these, not giving a code solution is actually an accepted way of answering, since it would not be actually helpful to the OP in the long run. You could mention that in the answer. If OP goes on asking (like how to read input), you can require demonstration of application of basic principle (like using a variable for the loop condition) and then give more hints - if you are that helpfully minded. Or stay with your answer to the current question. That politely and readable would earn my upvote. – Yunnosch Mar 02 '19 at 06:49
  • Please post the expected output and the real output in textual form. – R Sahu Mar 02 '19 at 06:50
  • Please don't post pictures of text. It would be way better to copy and format the inputs and outputs into stackoverflow site. Also would be nice to pass a link to the hackerrank challenge. Side note - nice, I guess that's [Queen -Crazy Little Thing Called Love](https://www.youtube.com/watch?v=zO6D_BAuYCI) – KamilCuk Mar 02 '19 at 06:51
  • Arturito, as was mentioned in other comments, people at stackoverflow really aren't fans of pictures of text. Your question, while very much a beginners question, is not bad otherwise. (It could be a duplicate... not sure about that.) If you [edit] to provide textual info in textual shape, it could lose the downvote. If you also work on general readability and apperance it might even get an upvote - mine. – Yunnosch Mar 02 '19 at 06:55
  • Another thing people here very much like is a [mcve]. Your code quote does not qualify for lack of being verifiable. I.e. it does not compile as is. Fixing that would be part of "working on appearance". – Yunnosch Mar 02 '19 at 06:57

1 Answers1

0

you have hardcoded the loop till 15. that means the loop will run 15times when the program is executed. but in this case it should repeat till input number. so take an input and loop till it. Ex:

int n;
cin>>n;    
for(int i=1;i<=n;i++){ 
    //your code
}
priojeet priyom
  • 899
  • 9
  • 17