Knowee
Questions
Features
Study Tools

Consider the C program shown below -int sumOdd(int *a, int n){  if(n <= 0) return 0;  else if (*a % 2 != 0) return *a + sumOdd (a + 1, n - 1);  return sumOdd (a + 1, n - 1);}int main(){  int a[] = {1,2, 3, 4, 5 ,6};  printf("%d\n", sumOdd(a, 6));  return 0;}The output of this program is -912158

Question

Consider the C program shown below -int sumOdd(int *a, int n){  if(n <= 0) return 0;  else if (*a % 2 != 0) return *a + sumOdd (a + 1, n - 1);  return sumOdd (a + 1, n - 1);}int main(){  int a[] = {1,2, 3, 4, 5 ,6};  printf("%d\n", sumOdd(a, 6));  return 0;}The output of this program is -912158

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the program is 9.

Here's the step-by-step explanation:

The function sumOdd is a recursive function that sums up all the odd numbers in an array.

The array a in the main function is {1,2,3,4,5,6}.

The function sumOdd is called with this array and its length (6) as arguments.

In the sumOdd function, it checks if the number is odd (*a % 2 != 0). If it is, it adds the number to the sum and calls the function again with the next number in the array (a + 1) and the remaining length (n - 1).

If the number is not odd, it simply calls the function again with the next number and the remaining length, without adding anything to the sum.

So, for the array {1,2,3,4,5,6}, the function will add up 1 + 3 + 5 = 9.

Therefore, the output of the program is 9.

This problem has been solved

Similar Questions

What will be the output of the following C program?void count(int n){  static int d=1;      printf(“%d “, n);      printf(“%d “, d);      d++;      if(n>1) count(n-1);      printf(“%d “, d);   }   void main(){       count(3);   }a.3 1 2 1 1 1 2 2 2b.3 1 2 1 1 1 2c.3 1 2 2 1 3 4 4 4d.3 1 2 2 1 3 4

What will be the output of the following program?12345678910#include <stdio.h> int main() { int a[10]; *a = 7; *(a+1) = 12; int p = *(a+1) / *a; printf("%d", p); return 0;}

What is output of below program?int main(){int i,j,count;count=0;for(i=0; i<5; i++);{for(j=0;j<5;j++);{    count++;}}printf("%d",count);return 0;}                                                                                               (A) 55(B) 54(C) 0(D) 1

What would be the output of the following program? #include <stdio.h> int main( ) { int j=1; while (j <= 255) { printf ( "%c %d\n ", j, j ); j++; } return 0; }

What will be the output of the following C code?#include<stdio.h>int main(){ int a=0, b=1, c=2; *((a+1 == 1) ? &b : &a) = a ? b : c; printf("%d, %d, %d\n", a, b, c); return 0;} Ops:   A. 0,2,2    B. 2,2,2    C. 0,1,2    D. 1,1,2

1/3

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.