What is the output of the following code?int arr[] = {1, 2, 3, 4, 5};int i;for (i = 0; i < 5; i++) arr[i] = arr[i] + 2;printf("%d\n", arr[3]);
Question
What is the output of the following code?
int arr[] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++)
arr[i] = arr[i] + 2;
printf("%d\n", arr[3]);
Solution
The output of the code will be 6.
Here's the step by step explanation:
-
An integer array
arr
is declared and initialized with the values{1, 2, 3, 4, 5}
. -
A for loop is set up to iterate over the array from the first element (at index 0) to the last element (at index 4).
-
Inside the loop, each element of the array is incremented by 2. So, the array becomes
{3, 4, 5, 6, 7}
. -
After the loop, the value of the element at index 3 (which is the fourth element) of the array is printed. Since arrays are 0-indexed,
arr[3]
refers to the fourth element of the array, which is 6.
So, the output of the code is 6.
Similar Questions
What is the output of the following code?#include <stdio.h>int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("%d\n", arr[3]); return 0;}
What is the output of the following code?#include <stdio.h>int main() { int i = 0; for (; i < 5; i++); printf("%d ", i); return 0;}
What is the output of the following piece of code?int i;i = 10;while (i < 20){ printf("%d", i % 2); i++;}
1.What is the output of the following code?char str[] = "hello";printf("%c\n", str[1]);
The output of the code will be ____. int i=5; printf(“%d %d %d”, i, i++, ++i); ans.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.