Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:12:52+00:00 2026-05-15T18:12:52+00:00

This program will calculate the average grade for 4 exams using a for loop

  • 0

This program will calculate the average grade for 4 exams using a for loop by prompting
the user for exam grades, one at a time, then calculate the average and display the result.

public class ExamsFor4 {

public static void main(String[] arguments) {


int inputNumber; // One of the exams input by the user.
int sum = 0;     // The sum of the exams.
int i;       // Number of exams.
Double Avg;      // The average of the exams.

TextIO.put("Please enter the first exam: ");        // get the first exam.
inputNumber = TextIO.getlnInt();    

for ( i = 1; i <= 4; i++ ) {  

    sum += inputNumber;                 // Add inputNumber to running sum.
    TextIO.put("Please enter the next exam: ");     // get the next exam.   
    inputNumber = TextIO.getlnInt();

        if (i == 4) {
            Avg = ((double)sum) / i;
            TextIO.putln();
            TextIO.putln("The total sum for all " + i +" exams is " + sum);
            TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
            break;

        }
} 

}   // end main ()
}  // end class ExamsFor4

My result:

Please enter the first exam: 100
Please enter the next exam: 99
Please enter the next exam: 98
Please enter the next exam: 97
Please enter the next exam: 96
The total sum for all 4 exams is 394
The average for the exams entered is 98.50.

This would be correct except for the last print out of: ‘Please enter the next exam: 96‘
I tried putting the IF statement between the ‘sum‘ line and the TextIO.put 'Enter next exam', but that isolates it.

Thanks, from a Network Dude trap in a Programmer’s world.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:12:53+00:00Added an answer on May 15, 2026 at 6:12 pm

    You should probably put the if-statment outside the for-loop. That way you don’t need the if-statement. Second the statement in the loop should be < 4 instead of <= 4.

    public class ExamsFor4 {
    
    public static void main(String[] arguments) {
    
    
    int inputNumber; // One of the exams input by the user.
    int sum = 0;     // The sum of the exams.
    int i;       // Number of exams.
    Double Avg;      // The average of the exams.
    
    TextIO.put("Please enter the first exam: ");        // get the first exam.
    inputNumber = TextIO.getlnInt();    
    
    for ( i = 1; i < 4; i++ ) {  
    
        sum += inputNumber;                 // Add inputNumber to running sum.
        TextIO.put("Please enter the next exam: ");     // get the next exam.   
        inputNumber = TextIO.getlnInt();
    } 
    
    Avg = ((double)sum) / i;
    TextIO.putln();
    TextIO.putln("The total sum for all " + i +" exams is " + sum);
    TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
    break;
    
    }   // end main ()
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i run the code as is my output is: This program will calculate
Here's my current code , and here's my current output: This program will calculate
I wrote this program to calculate very big numbers without using any BigInteger method.
I'm needing to write a program (C#) that will allow the user to create
I am trying to write a class that will calculate checksums using multiple processes,
So my question is on this: This program is supposed to calculate the area
I am working on building a little game program like this one for a
I have a program that will calculate the minimal area taken by fitting rectangles
I am creating a program which will calculate Betwenness Centrality for all nodes in
I'm trying to make a program to calculate the cos(x) function using taylor series

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.