1- Write a program to draw a right-justified triangle given the height as input. The first row has one asterisk (*) and increases by one for each row. Each asterisk is followed by a blank space and each row ends with a newline.
Ex: If the input is:
3
the output is:
* * * * * *
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/* Type your code here. */
int height = scnr.nextInt();
// draw right-justified triangle
for (int i = 1; i <= height; i++) {
for(int j=1;j<=i; j++){
// print each asterisk is followed by a blank space
System.out.print("* ");
}
// to ends witha newline
System.out.println();
}
}
}
2-(1) Given positive integer n, write a for loop that outputs the even numbers from n down to 0. If n is odd, start with the next lower even number. Hint: Use an if statement and the % operator to detect if n is odd, decrementing n if so.
Enter an integer: 7 Sequence: 6 4 2 0
(2) If n is negative, output 0. Hint: Use an if statement to check if n is negative. If so, just set n = 0.
Enter an integer: -1 Sequence: 0
Note: For simplicity add a space after each output value, including the last value.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int n;
int i;
System.out.println("Enter an integer:");
n = scnr.nextInt();
System.out.print("Sequence:");
/* Type your code here. */
Please answer these questions and I will give you a Thumbs up