• Write a function int MAX (int a, int b) which returns the maximum of 2 integers;
  • Test the MAX function using a simple instruction.
  • Write a main function that enters N integers, asks the user to enter N integers and calculates their maximum using the MAX function (without using an array!)

Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <limits.h>
 
int MAX (int a, int b)
{
    return (a>b?a:b);    
}

int main()
{
 
    int N;
    int i;
    int max;
    int nb;
    
	printf("Max(5,7)=%d\n",MAX(5,7));

    do{
        printf("Enter N: ");
        scanf("%d",&N);
    }while(N<=0);
    
    max=INT_MIN;
    for(i=0;i<N;i++)
    {
        printf("Enter number %d : ", i+1);
        scanf("%d",&nb);
        max=MAX(nb,max);
    }
    printf("Max=%d\n",max);
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Calculation of a Polynomial of degree N