Write a C program that will accept and print the maximum and minimum of 8 values inputted by a user


 

Solution

Follow the following steps to achieve this task

  1. Read the entered array size and store that value into the variable LENGTH.
  2. Read the entered elements using scanf and store the entered array elements into the array using for loop for(i=0;i<LENGTH;i++).
  3. Initialise min, max values with the 1st element of the array.

  4. Compare min, max values with a[i],
  5. If min value is greater than a[i] then initialise min=a[i] and if max value is less than a[i] then initialise max=a[i]. Repeat this step for each element of the string using for loop which is having the structure for(i=1;i<LENGTH
  6. ;i++).
  7. Print the minimum of the array and maximum of the array values.

// Online C compiler to run C program online
#include <stdio.h>
#define LENGTH 8
int main() {
    int a[LENGTH],i,min,max;
    printf("Array size was set to 8\n");
    
    for(i=0; i< LENGTH; i++){
        printf("Enter elements in index array[%d] ", i);
        scanf("%d",&a[i]);
    }
 
    min=max=a[0];
    for(i=1; i< LENGTH; i++)
    {
         if(min>a[i])
  min=a[i];   
   if(max<a[i])
    max=a[i];       
    }
     printf("The minimum value in the array is : %d",min);
          printf("\n The maximum value in the array is : %d",max);
    return 0;
}
Ameh Joseph Onyeke

Hello and welcome to my blog! My name is Ameh Joseph Onyeke a passionate educator and tech enthusiast with a BSc in Computer Science from Benue State University, a PGDE from the University of Maiduguri, and a Master’s in Software Engineering from Bayero University Kano. My journey blends a strong foundation in computing with a love for teaching, aiming to simplify complex tech concepts for learners at all levels. Through this blog, I provide answers and insights across 10+ programming languages to support students and tech enthusiasts in building their skills and confidence. Whether you're solving coding problems or sharpening your programming abilities, this blog is here to guide you. Let’s learn and grow together!

Post a Comment

Previous Post Next Post