#Homework Help

1 messages · Page 1 of 1 (latest)

bitter pier
#

Hey, I'm a complete beginner and I'm trying to understand this program and answer this prompt: "add a line within the for loop to print out the values of i and sum at each iteration."

Can someone please point me in the right direction? Thanks.

#include <stdio.h>

int main(void)
{
    int sum = 0; // Stores the sum of the integers
    int max = 0; // User -entered max integer to sum to
    int i = 1;
    
    printf("Enter the maximum integer to sum until: ");
    scanf(" %d", &max);
    
    // Compute sum of integers from 1 to max
    for (i = 1; i <= max; i++) {
        sum += i;
    }

    printf("Sum of integers from 1 to %d: %d\n", max, sum);
return 0;
}
bright kindleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

still glade
#

Have you made any progress?

dense glen
#

...at each iteration.
That means "within the loop"

You already have two statements that print something to the screen.
The last one of those prints the values of two variables within your program.
You can almost copy that line verbatim and place it within your for() loop.

for (i = 1; i <= max; i++) {
    sum += i;
    // copy the print statement here, then fix it to print `i` and `sum` instead
}

Super easy, right?

bitter pier
#
#include <stdio.h>

int main(void)
{
    int sum = 0; // Stores the sum of the integers
    int max = 0; // User-entered max integer to sum to
    int i = 1;
    
    printf("Enter the maximum integer to sum until: ");
    scanf(" %d", &max);
    
// Compute sum of integers from 1 to max
for (i = 1; i <= max; i++) {
    sum += i;

// Print out the values of i and sum at each iteration
printf("i = %d \t sum = %d\n", i, sum);
}

printf("Sum of integers from 1 to %d: %d\n", max, sum);
return 0;
}```
wraith pagoda
#

Yes

bitter pier
bright kindleBOT
#

@bitter pier Has your question been resolved? If so, run !solved :)

bitter pier
#

!solved