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 8924313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:22:42+00:00 2026-06-15T07:22:42+00:00

Can someone please help me. I’m having a hard time on fixing the logic

  • 0

Can someone please help me. I’m having a hard time on fixing the logic of my codes. this program is supposed to compute the average of a numbers entered by the user assuming that the size of the array is set to 100 and if User enters -99, program will be terminated
i know the problem is within the while loop processing

    Scanner input = new Scanner(System.in);

    int[] num = new int[100];

    int ctr=0, sum=0, ave = 0;

    while(num[ctr]!= -99)
    {
        System.out.print("Enter number: ");
        num[ctr]= input.nextInt();

        sum += num[ctr];

        ctr++;

    }

    System.out.print("Numbers are " );

    for(int x = 0; x<ctr; x++)
    {
        System.out.print(num[x] + " ");
    }

    ave = sum / (ctr-1);
    System.out.println("Average is " + ave);

    }

}
  • 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-06-15T07:22:43+00:00Added an answer on June 15, 2026 at 7:22 am

    You should read the first input outside the while while loop. Because when the user enters -99, inside the loop you increment ctr so the check will be on the wrong number. So actually you are never checking the current number you wanted to check.

    int ctr=0, sum=0, ave = 0;
    System.out.print("Enter number: ");
    num[ctr]= input.nextInt();
    while(num[ctr]!= -99)
    {
       sum += num[ctr];
       ctr++;
       System.out.print("Enter number: ");
       num[ctr]= input.nextInt();
    }
    

    But you need to handle the case which the user only enters one number, in this case you’re dividing by zero, and you’re getting the wrong average. You should also do:

    ave = sum / (ctr);
    

    instead of:

    ave = sum / (ctr-1);
    

    Now you’ll get good results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone please help me. why does this return an error: Dim stuff As
Can someone please help me out with a JavaScript/jQuery solution for this arithmetic problem:
Could someone please help explain why I can't get this to work? I properly
can someone please help me with this? i need to check through the System
Can someone please help me eliminate the extra whitespace at the bottom of this
Can someone please help me with this preg_match if (preg_match('~[^A-Za-z0-9_\./\]~', $filepath)) // Show Error
can someone please help me with this javascript:void(null) I found it used in link
Can someone please help me simpling this redundant piece of code? if (isset($to) ===
Can someone please help me, I have this xml snippet <?xml version=1.0 encoding=utf-8 ?>
Can someone please help. I have been searching and encountered/modified this code I am

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.