What will be the output of the following C program? int main()
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;
}
Show answerHide answer
Correct answer: (B) 1 0 1 4 9
Explanation
i*i - 2*i + 1 = (i - 1)², so for i = 0 to 4 the values are 1, 0, 1, 4, 9.