/* Write a program segment that reads a sequence of integers until a 0 is encountered, and outputs the number of non-zero numbers read. For example, if the input is: 2 13 -7 21 -11 0 The code will read in all the integers, and produce the following output: Number of non-zero integers: 5 */ import java.util.Scanner; public class HW4q2a { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int userInt=kb.nextInt(); int count=0; while (userInt!=0) { count++; userInt=kb.nextInt(); } System.out.println("Number of non-zero integers: "+count); } } //------------------------------------------ /* Write a program segment that reads a sequence of integers until a 0 is encountered, and outputs the number of even integers and the number of odd integers read (not counting the final 0). For example, if the input is: 2 13 -7 21 -11 0 The code will read in all the integers, and produce the following output: Number of even integers: 1 Number of odd integers: 4 */ import java.util.Scanner; public class HW4q2b { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int userInt=kb.nextInt(); int countEven=0; int countOdd=0; while (userInt!=0) { if (userInt%2==0) countEven++; else countOdd++; userInt=kb.nextInt(); } System.out.println("Number of even integers: "+countEven); System.out.println("Number of odd integers: "+countOdd); } } //------------------------------------- /* Write a program segment that reads a sequence of integers until a 0 is encountered. For each number read, output the number of times the number can be divided by 2 before you get 0. For example, if the input is: 2 9 -7 4 -3 0 The code will read in all the integers, and produce the following output: 2 4 3 3 2 */ import java.util.Scanner; public class HW4q2c { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int userInt=kb.nextInt(); while (userInt!=0) { int countDiv=0; while (userInt>0) { userInt=userInt/2; countDiv++; } System.out.println(countDiv); userInt=kb.nextInt(); } } } //------------------------------------- /* Given a String variable s, write a code segment that outputs each letter in the string twice. For example, if s = "Hello", the code should output "HHeelllloo". */ import java.util.Scanner; public class HW4q2d { public static void main(String[] args) { Scanner kb=new Scanner(System.in); String userStr=kb.nextLine(); int length=userStr.length(); int pos=0; while (length>pos) { System.out.print(userStr.charAt(pos)); System.out.print(userStr.charAt(pos)); pos++; } System.out.println(); } } //------------------------------------- /* Given an int variable size, write a program segment that outputs a triangle of asterisks '*', which is size rows high, and size columns wide, starting with a row of size asterisks, and ending with a row of a single asterisk. For example, if size = 5, the output should be: ***** **** *** ** * ---------------*/ import java.util.Scanner; public class HW4q2e { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int userInt=kb.nextInt(); while (userInt>0) { int starCount=0; while(starCount