Explanation
The program finds and prints the maximum number from a list of numbers entered by the user.
Header
The code begins with including the standard input-output header file using #include <stdio.h>
.
Variable Declaration
The main()
function is defined, and three integer variables d
, m
, and o
are declared:
int d
: Stores the total number of numbers entered by the user.int m
: Used as a loop counter.int o
: Stores the maximum number found in the array.
Program Description
The program prints a description message for the user to understand its functionality.
User Input
The user is prompted to enter the total number of numbers they want to find the maximum from. The input is read using scanf("%d", &d);
.
Array Input and Initialization
The program dynamically creates an array to store the numbers entered by the user:
int j[d];
: Declares an arrayj
of sized
to store the numbers.for (m = 0; m < d; m++)
: Loops through each position in the array.printf("\tNumber %d: ", m + 1);
: Prompts the user to enter a number.scanf("%d", &j[m]);
: Reads the entered number and stores it in the array.
Finding the Maximum Number
The program finds the maximum number in the array:
o = j[0];
: Initializeso
to the first element of the array.for (m = 0; m < d; m++)
: Loops through each position in the array.if (j[m] > o)
: Checks if the current element is greater than the current maximum.o = j[m];
: Updates the maximum number if the condition is true.
Output Result
The program prints the maximum number found in the array.
Program End
The program ends with printf("\n\n");
to print a new line and return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
int d, m, o;
printf("\n\n Program to find the maximum number from the numbers stored in an array entered by the user. Example 3 8 5 = 8 \n\n");
printf("Enter total number of numbers you want to find the maximum number from - (Example: 5) - and press enter: ");
scanf("%d", &d);
int j[d];
for (m = 0; m < d; m++)
{
printf("\tNumber %d: ", m + 1);
scanf("%d", &j[m]);
}
o = j[0];
for (m = 0; m < d; m++)
{
if (j[m] > o)
{
o = j[m];
}
}
printf("The maximum number from the entered array of numbers is %d \n", o);
printf("\n\n");
return 0;
}
Output
Program to find the maximum number from the numbers stored in an array entered by the user. Example 3 8 5 = 8
Enter total number of numbers you want to find the maximum number from - (Example: 5) - and press
enter: 5
Number 1: 45
Number 2: 76
Number 3: 56
Number 4: 87
Number 5: 67
The maximum number from the entered array of numbers is 87