🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In many scenarios, one may need to find the maximum element in an array, be it for analytics, data processing, or optimizing solutions. This post will guide you on writing a C program to find the maximum element in an array and will break down the steps to understand its working.
2. Program Overview
Our C program will do the following:
1. Accept the number of elements and the array of elements from the user.
2. Traverse the array and find the maximum element.
3. Display the maximum element to the user.
3. Code Program
#include <stdio.h> // Including standard input/output library
int main() { // Main function start
int n, i;
int arr[100]; // Array declaration
int max; // To store maximum value
// Get number of elements from user
printf("Enter the number of elements in array: ");
scanf("%d", &n);
// Input elements
printf("Enter %d integers:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = arr[0]; // Assume first value to be the maximum
// Traverse array elements
for(i = 1; i < n; i++) {
if(arr[i] > max) {
max = arr[i]; // Update the max value
}
}
// Print the maximum value
printf("Maximum element in the array is: %d", max);
return 0; // End of the program
}
Output:
Enter the number of elements in array: 5 Enter 5 integers: 52 12 10 6 80 Maximum element in the array is: 80
4. Step By Step Explanation
1. Header File: We start with the stdio.h header, which gives us access to the standard input and output functions in C.
2. Variable Declaration:
- n is for storing the number of elements.
- arr[100] holds the array values.
- max stores the maximum value.
3. Taking Input:
- We ask the user for the number of elements and then take input for each of these elements.
4. Finding Maximum:
- Initially, we assume the first element is the maximum.
- Then, we traverse the array. If we encounter an element larger than our current maximum (max), we update max with this new value.
5. Displaying Result: The maximum value is printed to the console.
By the end of this program, you will have learned an efficient way to find the maximum element in an array. This method uses a single loop, ensuring performance and speed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment