Given the variables
String stars = "*****";
String stripes = =====;
what do these loops print?
- a. int i = 0;
while (i < 5)
{
System.out.println(stars.substring(0, i));
i++;
}
- b. int i = 0;
while (i < 5)
{
System.out.print(stars.substrings (0, i));
System.out.println(stripes.substrings(i, 5));
i++;
}
- c. int i = 0;
while (i < 10)
{
if (i % 2 == 0) { System.out.println(stars); }
else { System.out.println(stripes); }
}
Explanation of Solution
a.
//Definition of class Test
class Test
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 5)
{
//Print the output
System.out.println(stars.substring(0, i));
//Increment the value of "i"
i++;
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=5” is checked using “while” loop. Print the output and increment the value of “i”.
Output:
*
**
***
****
Explanation of Solution
b.
//Definition of class Test
class Test
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 5)
{
//Print the output
System.out.println(stars.substring(0, i));
//Print the output
System.out.println(stars.substring(i, 5));
//Increment the value of "i"
i++;
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=5” is checked using “while” loop. Print the output by calling the two sub string function and increment the value of “i”.
Output:
*****
*
****
**
***
***
**
****
*
Explanation of Solution
c.
//Definition of class Main
class Main
{
//Definition of method main
public static void main(String[] args)
{
//Initialize the variables
String stars = "*****";
String stripes = "=====";
//Initialize the value of "i" to 0
int i = 0;
//Check the condition using while loop
while (i < 10)
{
//Check the condition for "i%2" equals to 0
if (i % 2 == 0)
{
//Print the output
System.out.println(stars);
}
else
{
//Print the output
System.out.println(stripes);
}
}
}
}
Explanation:
The above code snippet initializes the variable “i” to “0”. The condition “i<=10” is checked using “while” loop. Inside the while loop, check the condition of “i%2” using the “if” statement.
Output:
*****
*****
*****
*****
*****
*****
*****
.
.
.
.
Infinite loop
Want to see more full solutions like this?
Chapter 4 Solutions
Big Java Late Objects
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Concepts Of Programming Languages
Java: An Introduction to Problem Solving and Programming (7th Edition)
Starting Out with C++: Early Objects (9th Edition)
- int func(int a, int b) { return (aarrow_forward#include using namespace std; const int y = 1; int main () { int static y = 2; int i = 3, j = 4, m = 5, n = 6; %3D int a = [] (int x, int i = 1) { return x * i; } (y, 3) ; %3D int b = [=] (int x) { return [=] (int b) { return b + j; } (x) % 7; } (a); int c = [=] (int x) mutable ->int { m = 6; return [6] (int j) mutable { y = a * b; return y / j; } (x) -m; } (b) ; cout << a << endl; cout << b << endl; cout << c << endl; cout << m << endl; cout << y < endl; return 0; This program outputs 5 lines. What are they? Please explain your work and your answer.arrow_forward// SuperMarket.java - This program creates a report that lists weekly hours worked // by employees of a supermarket. The report lists total hours for // each day of one week. // Input: Interactive // Output: Report. import java.util.Scanner; public class SuperMarket { public static void main(String args[]) { // Declare variables. final String HEAD1 = "WEEKLY HOURS WORKED"; final String DAY_FOOTER = " Day Total "; // Leading spaces are intentional. final String SENTINEL = "done"; // Named constant for sentinel value. double hoursWorked = 0; // Current record hours. String hoursWorkedString = ""; // String version of hours String dayOfWeek; // Current record day of week. double hoursTotal = 0; // Hours total for a day. String prevDay = ""; // Previous day of week. boolean done = false; // loop control Scanner input = new…arrow_forwardint m =0; while(++m < 2 ) System.out.print( m ); System.out.print( m ); a. 112233 Ob. 12 C. 123 Od. 001122arrow_forwardint y=0,i; for (int i=0;i<10;++i) y+=i; 36 66 45 55arrow_forwardNumber of digits int main() {int c=0;int i=3856; int l=i; while(l>0) {l=l/10; c++; } printf("%d",c); return 0; } Summation of digits int main() {int sum=0; int i=384; int l=i; while(l>0) { sum+=l%10; l=l/10;} printf("%d",sum); return 0; }arrow_forward#include #include #include int main() { int i, j; printf("Columns | JIn"); for (i = 1; i< 4; ++i) { printf("Outer %6d\n", i); for (j = 0; j< i; ++j) { printf(" Inner%10d\n", j); } } /* heading of outer for loop */ /* heading of inner loop */ %3D return (0); } Create a new code modifying it to use "while loop" instead of the "for loop"arrow_forward#includecstdio.h> int x=10; void func1() 7 printf("x=%d\n", x); return; 8 10 int func2(int a, int b) 15 return a+b; 16 18 19 ... func3(...) 20 21 .. return ... int main() 27 { int x-1, y=2; func1(); 28 29 30 int z=func2(x, y); printf("z-%d\n", z); 31 34 Figure 2. C Program for Q2 What is the output after execution of line 29 and 32? a. b. func3() in line 19 to 23 is incomplete. Write the function, func30 to compute and print the volume of cuboid with length x, width y and height z. c. Complete line 33 to call func3() that you created in part (b).arrow_forwardMultiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single line using for loop. Example: 5 5,10,15,20,25,30,35,40,45,50 class Main {public static void main(String[] args) {}}arrow_forward“As one variable goes up, the other variable goes down” is a description of:arrow_forwardint a = 10, b =7; System.out.println(a>b?a:b); Will always print what?arrow_forwardmain.cpp 1 @include 2 using namespace std; int maxResult() 4-( int maxVal = 0; 6. for (int i = 0; i <= n; i += a) { for (int j = e; j <= n i;j += b) !! 10 (float)(n - (i + j)) / (float)(c); 11 float z = 12 if (floor (z) { int x = i / a; int y 13 ceil(z)) 14 15 16 j/ b; max (maxVal, x + y + (int)z); 17 maxVal = 18 19 20 21 22 return maxVal; 23 } 24 int main() 26 { 25 27 28 cout << maxResult( ); 29 30 return 0; 31 } Input Compilation failed due to following erors) main.cpp:7:23: error: 'n was not declared in this scope 7 for (int i = 0; i <= n; i t a) main.cpp:7:31: error: 'a' was not declared in this scope 7 for (int i 0; i <= n; i t a) main.cpp:9:36: error: 'b' was not declared in this scope for (int j = 0; j <- n i; j b) %3D main.cpp:11:45: error: was not declared in this scopearrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education