Question 1
Write a program to generate the following output.
*
* #
* # *
* # * #
* # * # *
Answer
public class KboatPattern
{
public static void main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
}
System.out.println();
}
}
}
Output
Question 2
Write a program to print the series given below.
5 55 555 5555 55555 555555
Answer
public class Kboat5Series
{
public static void main(String args[]) {
for (int i = 1; i <= 6; i++) {
for (int j = 0; j < i; j++) {
System.out.print('5');
}
System.out.print(' ');
}
}
}
Output
Question 3(i)
Write a program in Java to display the following patterns.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Answer
public class KboatPattern
{
public static void main(String args[]) {
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}
Output
Question 3(ii)
Write a program in Java to display the following patterns.
1 * * * *
* 2 * * *
* * 3 * *
* * * 4 *
* * * * 5
Answer
public class KboatPattern
{
public static void main(String args[]) {
char ch = '*';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 ; j++) {
if(i == j)
System.out.print(i + " ");
else
System.out.print(ch + " ");
}
System.out.println();
}
}
}
Output
Question 3(iii)
Write a program in Java to display the following patterns.
#
* *
# # #
* * * *
# # # # #
Answer
public class KboatPattern
{
public static void main(String args[]) {
char ch1 = '#', ch2 = '*';
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (i % 2 == 0)
System.out.print(ch2 + " ");
else
System.out.print(ch1 + " ");
}
System.out.println();
}
}
}
Output
Question 3(iv)
Write a program in Java to display the following patterns.
1 * * * *
2 2 * * *
3 3 3 * *
4 4 4 4 *
5 5 5 5 5
Answer
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
for (int k = 1; k <= 5 - i; k++) {
System.out.print('*');
}
System.out.println();
}
}
}
Output
Question 3(v)
Write a program in Java to display the following patterns.
* * * * 5
* * * 4
* * 3
* 2
1
Answer
public class KboatPattern
{
public static void main(String args[]) {
char ch = '*';
for (int i = 5; i >= 1; i--) {
for (int j = i-1; j >= 1 ; j--)
System.out.print(ch + " ");
System.out.print(i);
System.out.println();
}
}
}
Output
Question 3(vi)
Write a program in Java to display the following patterns.
A B C D E
A B C D
A B C
A B
A
Answer
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j);
}
System.out.println();
}
}
}
Output
Question 3(vii)
Write a program in Java to display the following patterns.
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
Answer
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print( j + " ");
System.out.println();
}
}
}
Output
Question 3(viii)
Write a program in Java to display the following patterns.
J
J A
J A V
J A V A
Answer
public class KboatPattern
{
public static void main(String args[]) {
String str = "JAVA";
int len = str.length();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(str.charAt(j) + " ");
}
System.out.println();
}
}
}
Output
Question 3(ix)
Write a program in Java to display the following patterns.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answer
public class KboatPattern
{
public static void main(String args[]) {
for(int i = 5; i >= 1; i--) {
for(int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
for(int i = 2; i <= 5; i++) {
for(int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
}
Output
Question 3(x)
Write a program in Java to display the following patterns.
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Answer
public class KboatPattern
{
public static void main(String args[]) {
for(int i = 1; i <= 5; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println();
}
for(int i = 4; i >= 1 ; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int k = i; k <= 5; k++) {
System.out.print(k + " ");
}
System.out.println();
}
}
}
Output
Question 4(i)
Distinguish between the following:
break statement and la
Question 6
Write a program to generate a triangle or an inverted triangle till n
terms based upon the user's choice of triangle to be displayed.
Example 1
Input:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms
5
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2
Input:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms
6
Output:
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Answer
import java.util.Scanner;
public class KboatPattern
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for a triangle");
System.out.println("Type 2 for an inverted triangle");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
System.out.print("Enter the number of terms: ");
int n = in.nextInt();
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");
}
}
}
Output