******************************************************************************************
* Program Summary: For, while, and do-while loops; nested loops
*
add code to create:
*
* a for loop nested inside another for loop
* a while loop
* a do-while loop
*****************************************************************************************/
package prg420week3_codingassignment;
public class PRG420Week3_CodingAssignment {
public static void main(String[] args) {
// The following code should print asterisks: 1 on line 1, 2 asterists on line 2,
// 3 on line 3, 4 on line 4… for as many lines as the variable linesOfAsterisks.
// To do this, we can use 2 nested for loops. The first for loop is coded for you.
// You will need to add another for lop, NESTED INSIDE the first, that prints
// a certain # of asterisks based on the # of times the loop code has been executed.
// The result should look like this:
// *
// **
// ***
// ****
// *****
// etc.
int linesOfAsterisks = 5;
for (int i = 1; i <= linesOfAsterisks; i++) { // for each line…
// LINE 1. ADD A NESTED FOR LOOP THAT DISPLAYS ONE ASTERISK ON LINE 1, TWO ASTERISKS ON LINE 2, 3 ASTERISKS ON LINE 3, ETC.
System.out.println();
}
//////////////////////////////////////////////////////////////////////
// Add a while or do-while loop that displays the numbers from 10 to 1 in that order, like so:
// 10
// 9
// 8
// 7
// …
// 1
//////////////////////////////////////////////////////////////////////
int num=10;
//LINE 2. ADD A LOOP THAT DISPLAYS NUMBERS 10 TO 1 IN DECREASING ORDER (HINT: DECREMENT OPERATOR)
///////////////////////////////////////////////////////////////////////
// Write a loop that adds 7s one at a time until the sum becomes > 157.
// Then print out both the sum and the number of 7s that were aded.
// Write a while or do-while loop, whichever you think is most appropriate.
//////////////////////////////////////////////////////////////////////////
int sum = 0;
int numberOfSevens = 0;
//LINE 3. ADD ANOTHER LOOP THAT ADDS 7s UNTIL SUM > 157. THEN DISPLAY SUM AND NUMBER OF SEVENS ADDED.
}
Select your paper details and see how much our professional writing services will cost.
Our custom human-written papers from top essay writers are always free from plagiarism.
Your data and payment info stay secured every time you get our help from an essay writer.
Your money is safe with us. If your plans change, you can get it sent back to your card.
We offer more than just hand-crafted papers customized for you. Here are more of our greatest perks.
Get instant answers to the questions that students ask most often.
See full FAQ