Untitled
unknown
plain_text
2 years ago
2.1 kB
17
Indexable
public class BasicLoopsLab { public static void main(String[] args) { //TASK 0 basic while loop //Predict the output of the code below. Run to verify your answer. //Modify the code to print the numbers from 0 to 200 by 5's. (0, 5, 10, 15 ...200) int i = 0; while (i <= 200) { System.out.println(i); i+=5; } System.out.println("\n\n\n"); //TASK 1 count backwards //Write a while loop that counts backwards from 50 downto 1. (50, 49, 48, 47, ...1) int counter = 50; while (counter >=1){ System.out.println(counter); counter--; } System.out.println("\n\n\n"); //TASK 2 Convert while to for loop //Remove the block comment. Predict the output. Run and verify. //Convert the while loop to a for loop that does the same task. int k = 1; for(;k <=10; k++){ System.out.println(k + " squared equals " + k * k); k++; } System.out.println("\n\n\n"); //TASK 3 Do-while //Write a do-while loop to do the same task as in task 1. int counter1 = 50; do{ System.out.println(counter1); counter1--; }while(counter1 >=1); System.out.println("\n\n\n"); //TASK 4 For-loop //Use a for loop to print a table // of inches and centimeters for inches 1 - 25. //#Note 1 inch = 2.54 centimeters for(int inch = 1; inch <=25; inch++){ System.out.println(inch+" inch = "+inch*2.54+ " centimeters"); } System.out.println("\n\n\n"); //TASK 5 Leap Year /* February 29 is a date that usually occurs every four years, and is called leap day. This day is added to the calendar in leap years as a corrective measure, because the Earth does not orbit around the sun in precisely 365 days. Write a program which uses a loop to print the next 20 Leap Years starting with 2016. Choose any loop you wish. NOTE: Do not do the calculations yourself. Start the year at 2016, loop 20 times adding 4 each time. */ int year = 2016; for(int y = 1; y <=20;y++){ System.out.println(year + " is leap year."); year += 4; } System.out.println("\n\n\n"); } //end main } //end class
Editor is loading...