Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

what will be the out for i=4 please explain the steps in details to how to get i

ID: 3544176 • Letter: W

Question

what will be the out for i=4

please explain the steps in details to how to get it., just the final answer is not enough.

I need to understand the back and forth between two functions.

#include <stdio.h>

int sumFrom1toN(int N)

{

    int i;

    int result=0;

    printf("inside the function ");

    for (i=0;i<N;i++)

        result=result+(i+1);

    return result;

}

int main(void)

{

    int i;

    printf("Enter an integer ");

    scanf("%d", &i);

    printf("The sum of numbers from 1 to %d is %d ", i, sumFrom1toN(i));

    return 0;

}

Explanation / Answer

The answer is 10 as it for i=4 , ans will be 4+3+2+1=10.


What does the function do ??

that is in

Main function , you enter an i , for this case you entered 4.

In printf statement sumFrom1toN(i) is called to get printed.

Now question is what does sumFrom1toN(i) do ?

It takes a value n , initialize result=0

then a loop is run for i=0 to i=n, in that wha it does , it sum up every value of i to the result .

As for this case n=4

so loop will run 4 times

In first loop result=result+1 so result will become 1

In Second loop result=result+2 so result will become 3

In Third loop result=result+3 so result will become 6

In forth loop result=result+4 so result will become 10



so result is 1+2+3+4=10 , it is returned to get printed.