write a program to generate a (1)
write a program to generate a (1)
Ans:
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
THE OUTPUT IS:
The output of this program will depend on the user’s input. For example, if the
user enters 3 for the number of terms and chooses 1 for a triangle, the output will
be:
1
2 2
3 3 3
If the user chooses 2 for an inverted triangle, the output will be:
3 3 3
2 2
1
If the user enters anything other than 1 or 2 for the choice, the program will
print Incorrect Choice.
Ans:
import java.util.Scanner;
Enter n: 4
Sum=1.6666666666666667