What is wrong with the following code fragment? AND Rewrite it so that it produces correct output.
Java Code:
if (total == MAX)
if (total < sum)
System.out.println(“total == MAX and < sum.”);
else
System.out.println(“total is not equal to MAX”);
Answer:
Issue here is with curly braces. I have modified the code. It wil work as expected.
when you dont mention the curly braces if statement will treat like single statement. In our case, we did not mention the curly braces, so it treats like with in if condition there is if-else statement. That is the issue here.
if (total == MAX){
if (total < sum)
System.out.println(“total == MAX and < sum.”);
}
else
System.out.println(“total is not equal to MAX”);
What is wrong with the following code fragment? AND Rewrite it so that it produces correct...
use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...
Something is wrong with each of the following pieces of code. Use A or B to label whether the error is from: A. Compiler ErrorB. Logical or Runtime Error Use minor edits to correct the code. int x System.out.println("value of x is:x) if (x820) System.out.println("Your number was even!") System.out.println("Your number is odd!") else double avg numl+num2+num3/3.0 if (x > y) f double max-x; else max-y: 5. double answer Math.sqrt (/25;
What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;
Rewrite the C++ code below so the the following conditions are met: You may create any number of classes. You may edit the main() function. Remove the variable string type completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Get rid of every if and if else statement completely from the entire program. -- Note: Entire program includes all classes, all functions, and main(). Hint: Use polymorphism. #include <iostream> #include <vector> #include <algorithm>...
What is wrong with the following code fragment? char *src = "hello"; char *dst = (char *) malloc(strlen(src)); //too small! strcpy (dst, src); //work properly
(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 {...
Question 28 (1 point) ✓ Saved What does the code fragment print? double x = Math.sqrt (3); double y = 4; if (x + x == y) { System.out.println(x); } else { System.out.println(y); } 4 1
java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...
A. What is the output of the following C++ code fragment? (all variables are of type int) int count-1; int y-100; while (count 3) y=y-1 ; count+t cout << y << endl; cout<< count <<endl What is the value of x after control leaves the following while loop? (all variables are of type int) B. int x0 while (x < 20) sum- sum+x cout << sum<< endl;
Java questions Part 1 Express the code fragment shown below using only a for loop. int n = 0; while (n <= 100) { int sum = sum + n; n = n + 1; System.out.println (sum); } Part 2 Use a loop structure to determine and print all positive integers between 1 and 100 that are divisible (evenly) by 5 and 8