Question

1. What is the output of each of the following code segments if the inputs are...

1. What is the output of each of the following code segments if the inputs are as follows:

5 0 15 -5 2

Scanner input = new

Scanner(System.in);

int sum = 0;

for(int i = 0; i < 5; i++){

System.out.print(

“Enter a number”);

int number = input.nextInt();

if(number <= 0)

break;

sum += number;

}

System.out.println(sum);

Second Segment:

Scanner input = new

Scanner(System.in);

int sum = 0;

for(int i = 0; i < 5; i++){

System.out.print(

“Enter a number”);

int number = input.nextInt();

if(number <= 0)

continue;

sum += number;

}

System.out.println(sum);

0 0
Add a comment Improve this question Transcribed image text
Answer #1
1)  5
Explanation:
-------------
calculates the sum of the numbers until a non-positive number is seen
5 0 15 -5 2
so, after 0 is entered, program stops and prints sum calculated.
that's why answer is 5

2)  22
Explanation:
-------------
calculates the sum of all positive numbers entered
5 0 15 -5 2
=>  5 + 15 + 2
=>  22
that's why answer is 22
Add a comment
Know the answer?
Add Answer to:
1. What is the output of each of the following code segments if the inputs are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • 8. What is the output of this program if the input: 1 2 3 4 -5...

    8. What is the output of this program if the input: 1 2 3 4 -5 0 26? importjava.util.Scanner; public class Q_09 Your answer: public static void main(String[] args) int n, neg_count = 0, sum = 0; Scanner input = new Scanner(System.in); System.out.println("Enter n:"); n = input.nextInt(); while(n !=0) neg_count++; sum +=n; System.out.println("sum =" + sum); System.out.println("#of negatives:" + neg_count + "\t" +"Total = "+sum);

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • Please answer both questions. Thanks Question 26 (1 point) What is the output generated by the...

    Please answer both questions. Thanks Question 26 (1 point) What is the output generated by the code segment below, assuming that data is a Scanner object used to read the following text: As the world turns. data.useDelimiter(" "); int count = 0; while (data.hasNext()) { char input data.next().charAt(0); if (! Character.isLetter (input)) { count++; System.out.println (count); A/ Question 27 (1 point) Assuming that the user provides 114 as input, what is the output of the following code snippet? Please make...

  • I need the following java code commented import java.util.Scanner; public class Main { public static void...

    I need the following java code commented import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); int productNo=0; double product1; double product2; double product3; double product4; double product5; int quantity; double totalsales=0; while(productNo !=0) System.out.println("Enter Product Number 1-5"); productNo=input.nextInt(); System.out.println("Enter Quantity Sold"); quantity=input.nextInt(); switch (productNo) { case 1: product1=1.00; totalsales+=(1.00*quantity); break; case 2: product2=2.00; totalsales+=(2.00*quantity); break; case 3: product3=6.00; totalsales+=(6.00*quantity); break; case 4: product4=23.00; totalsales+=(23.00*quantity); break; case 5: product5=17.00; totalsales+=(17.00*quantity); break;...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • Debug the following java code so that it will have given out put at the bottom....

    Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT