Calculation of the average grade of a student with 4 subjects

C program to read four subject grades, validate inputs between 0 and 10, calculate the average, and display it with two decimal precision.

Brief Introduction

Calculation of the average grade of a student with 4 subjects (c)
DESCRIPTION:
C program that reads the grades of 4 subjects from the keyboard, calculates the average, and prints it on the screen.

ADDITIONAL NOTES:
The program is simple and robust: it checks that the entered grades are between 0 and 10 and requests re-entry in case of error.

EXPLANATIONS:
- The 'voti' array is used to store the 4 grades entered by the user.
- The initial for loop reads the grades one by one, with validity check.
- If a grade is invalid (outside the 0-10 range), the index is decremented to repeat the input.
- The second for loop calculates the sum of all grades.
- The average is calculated by dividing the sum by 4 (number of subjects).
- Finally, the average is printed with two decimal places.

Example Code: c

#include <stdio.h>

int main() {
    // Array to store the grades of the 4 subjects
    float voti[4];
    float somma = 0.0;
    float media;

    // Loop to read grades from the keyboard
    for (int i = 0; i < 4; i++) {
        printf("Enter the grade for subject %d (0-10): ", i + 1);
        scanf("%f", &voti[i]);

        // Simple check for valid values
        if (voti[i] < 0 || voti[i] > 10) {
            printf("Error: the grade must be between 0 and 10. Try again.\n");
            i--; // decrement i to repeat the input
        }
    }

    // Calculate the sum of the grades
    for (int i = 0; i < 4; i++) {
        somma += voti[i];
    }

    // Calculate the average
    media = somma / 4;

    // Print the result
    printf("\nThe average grade is: %.2f\n", media);

    return 0;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

C development environment (e.g. GCC, Visual Studio Code, Code::Blocks)

Keyboard input (stdin)

Console output (stdout)

No external libraries required

Similar Topics

Calculation of the area of a rectangle (base x height)
C program calculates rectangle area by prompting user for positive base and height values with input validation and displays result with two decimals.
Calculation of the average of three integers
C program that reads three integers from keyboard, calculates their average using float division, and prints the result with two decimal places.
Printing the 5 Times Table
C program that prints the 5 times table from 1 to 1 using a for loop, ideal for beginners learning basic loops and multiplication in C programming.
Counting Characters in a String Without Using strlen
Learn how to count characters in a C string manually without using strlen by iterating until the terminator for better understanding of string handling.
Swapping the values of two integer variables
Complete C example demonstrating how to swap two integer variables using a temporary variable with pointers and clear Italian comments.
Calculation of the perimeter of a circle given the radius
C program to calculate and print the perimeter of a circle based on user input radius using float type and defined PI constant for accuracy.
Program that prints the greater of two numbers
C program that reads two integers from keyboard and prints the greater number using basic input output and conditional statements for beginners.
Program that prints the lowercase alphabet
This C program prints the lowercase alphabet from a to z using a for loop and char variables, demonstrating basic C programming concepts clearly and simply.