🎓 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
Arrays in C provide an efficient way to store and process a collection of data. Calculating the average of a set of numbers is a fundamental operation and is especially straightforward when using arrays. This post will guide you through a C program that calculates the average of a set of numbers using arrays.
2. Program Overview
1. Define the array and its size.
2. Prompt the user to input numbers into the array.
3. Calculate the sum of all elements in the array.
4. Divide the sum by the number of elements to get the average.
5. Display the calculated average.
3. Code Program
#include <stdio.h>
int main() {
int n, i;
float sum = 0.0, average;
printf("Enter the numbers of data: ");
scanf("%d", &n);
float num[n];
for(i = 0; i < n; i++) {
printf("Enter number %d: ", i+1);
scanf("%f", &num[i]);
sum += num[i];
}
average = sum / n;
printf("Average = %.2f\n", average);
return 0;
}
Output:
Enter the numbers of data: 5 Enter number 1: 10 Enter number 2: 20 Enter number 3: 30 Enter number 4: 40 Enter number 5: 50 Average = 30.00
4. Step By Step Explanation
1. After defining the necessary variables, the user is prompted to specify how many numbers they wish to input.
2. We then define an array num of float type to hold the user's numbers.
3. A for loop iterates n times, prompting the user to enter each number, which is stored in the array. As each number is entered, it's added to the sum.
4. After collecting all numbers, we calculate the average by dividing the sum by n, the number of data points.
5. The result, average, is then printed to the screen.
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