Ask Question
12 August, 05:02

Create a Racket procedure compute_pos that reads numbers from the keyboard until 0 is read, count how many positive numbers are read, and also compute the sum of only positive numbers, and display the count and the sum of positive numbers in different lines. Note that 0 is not included in the computations.

+3
Answers (1)
  1. 12 August, 08:05
    0
    import java. util. Scanner;

    public class Solution {

    public static void main (String args[]) {

    compute_pos ();

    }

    public static void compute_pos () {

    Scanner scan = new Scanner (System. in);

    int sum = 0;

    int counter = 0;

    / / Prompt the user to enter an input

    System. out. println ("Enter your value");

    / / user input is assign to the variable value

    int value = scan. nextInt ();

    while (value! = 0) {

    if (value > 0) {

    sum + = value;

    counter++;

    }

    System. out. println ("Enter your value");

    value = scan. nextInt ();

    }

    / / Display the value of sum to the user

    System. out. println ("The sum of the positive number is: " + sum);

    / / Display the value of counter to the user

    System. out. println ("The number of positive number is: " + counter);

    }

    }

    Explanation:

    The first line import the Scanner class which allow the program to read input from the user. The class Solution is then defined. Then the main method which signify the beginning of execution in the program is defined. A method is called inside the main method.

    The method compute_pos is defined. Inside the compute_pos method, a scanner object scan is declared to accept input from the user via the keyboard.

    The variable sum and counter is declared and assigned to 0.

    A prompt is displayed to the user to input a value. The user input is accepted and stored in the variable value.

    Then a while loop is use to check the user input if it is not equal to zero. If it is equal to zero, the loop will end. Inside the loop, we first check to see if the user input is greater than 0, if it is, we add it to the variable sum and increase our counter by 1. At the end of the loop, a prompt is again display to accept input from the user.

    Outside the loop, we display the sum and counter to the user.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Create a Racket procedure compute_pos that reads numbers from the keyboard until 0 is read, count how many positive numbers are read, and ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers