Computer Science, asked by kapildevsahu13, 11 months ago

What will be the output of following program int incr (int i) { static int count = 0;
count = count i; return (count); } main ({intij; for (i = 0; i <=4i--)j-incr(i); }​

Answers

Answered by BRAINLYBOOSTER12
4

REQUIRED OUTPUT :

1

2

3

:) STAY HAPPY AND SAFE

Answered by pruthaasl
0

Answer:

On successful execution of the given program, the output will be j=10

Static Variables:

  • Static is a storage class in C++
  • The keyword static is used to declare a variable as static.
  • When a variable is declared as static, it is initialized only once in the entire program.
  • If the value of the variable is altered further in the program, then that value is retained in the next iteration.
  • The compiler persists the variable till the end of the program.

incr function:

  • The incr function declared at the beginning of the code returns the value of the count variable whenever it is called in the main function.
  • The return data type of this function is an integer.
  • The incr function includes a variable called count of the integer data type having storage class as static.
  • When the incr function is called, it increments the value of count by the value of variable i from the main function, and returns the incremented value of count to the calling function.

Main function:

  • The main function consists of two variables i and j, of integer data type.
  • In the next line, for loop is declared to perform five iterations with the value of variable i going from zero to four.
  • The for loop calls the incr function and assigns the value returned from the incr function to the variable j.

Explanation:

Step 1:

When the incr function is called in the first iteration i=0, the value of count will be zero.

This value is returned to the main function and assigned to j.

So, after first iteration count=0 and j=0

Step 2:

When the incr function is called in the second iteration i=1, the value of count will become one, i.e., count = 0+1

This value is returned to the main function and assigned to j.

So, after the second iteration count=1 and j=1

Step 3:

When the incr function is called in the third iteration i=2, the value of count will become 3, i.e., count=1+2

This value is returned to the main function and assigned to j.

So, after the third iteration count=3 and j=3

Step 4:

When the incr function is called in the fourth iteration i=3, the value of count will become 6, i.e., count=3+3

This value is returned to the main function and assigned to j.

So, after the fourth iteration count=6 and j=6

Step 5:

When the incr function is called in the fifth iteration i=4, the value of count will become 10, i.e., count=6+4

This value is returned to the main function and assigned to j.

So, after the fifth iteration count=10 and j=10

Therefore, the output of the program will be j=10

#SPJ3

Similar questions