I am a beginner in C language and encountered some problems while practicing "summing integer segments". However, I am not sure where my code went wrong and I would like to seek help. It displays an error message saying 'Only output one line'. #202408
Replies: 2 comments
|
You actually did a fantastic job with this, and your core logic is 99% correct. The error you are getting is a strict formatting issue caused by a sneaky "double newline." In your while loop, if you print exactly 5 numbers, your else statement triggers and prints a newline (\n). However, right after your loop finishes, you have an unconditional printf("\n");. This means if your numbers fit perfectly into rows of 5, your code will print an extra, empty blank line right before the Sum = X line. Automated graders will fail your code for having that empty line! To fix this, it is highly recommended to use the modulo operator (%) for line breaks. It checks if your current count is cleanly divisible by 5. Here is the cleaned-up, passing version of your code: C int main() { } Run this through your grader, and it should immediately give you the pass. |
|
Okay, I have solved the problem according to what you taught me. Thank you very much for your help!
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Question
Body
Question:
Given two integers A and B, output all integers from A to B and the sum of these numbers.
Input format:
Input two integers A and B in one line, where one is 100 ≤ A ≤ B ≤ 100, separated by a space.
Output format:
Firstly, output all integers from A to B in sequence, with each 5 digits occupying a line and each digit occupying a width of 5 characters, aligned to the right. Finally, output the sum of all numbers in the format of sum=x on a single line.
My Code:
Guidelines
All reactions