Question discussion

What will be the output of the following C program?

359282Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following C program?
#include <stdio.h>
int main()
{
    int a[5];
    for (int i = 0; i < 5; i++)
    {
        a[i] = i * i - 2 * i + 1;
    }
    for (int i = 0; i < 5; i++)
    {
        printf("%d ", a[i]);
    }
    return 0;
}
  • A1 0 2 4 9
  • B1 0 1 4 9
  • C1 1 2 4 9
  • D1 2 3 4 9
Correct answer: B

Explanation

a[i] = i² − 2i + 1 = (i − 1)², so the values for i = 0 to 4 are 1 0 1 4 9.

Discussion 0

Nobody has replied yet. Start the discussion!