Friday, May 3, 2019

Recursion 8 : Maximum Element in Array

  • Problem Description

    Write a program to find the maximum element in an array using recursion.

    Input and Output Format:


    Input consists of n+1 integers.

    Refer sample input and output for formatting specifications.

    All text in bold corresponds to input and the rest corresponds to output.

  • Test Case 1

    Input (stdin)
    6

    2 5 1 7 4 2

    Expected Output
    Maximum element in the array is 7
  • Test Case 2

    Input (stdin)
    4

    9 8 7 6

    Expected Output
    Maximum element in the array is 9
Solution

#include <stdio.h>
 
int main()
{
  int array[100], maximum, size, c;
  scanf("%d", &size);
 
  for (c = 0; c < size; c++)
    scanf("%d", &array[c]);
 
  maximum = array[0];
 
  for (c = 1; c < size; c++)
  {
    if (array[c] > maximum)
    {
       maximum  = array[c];
    }
  }
 
  printf("Maximum element in the array is %d",maximum);
  return 0;
}

1 comment:

  1. Nice way to find element in array. there is also good way to find element in array Find max element in array

    ReplyDelete

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...