[Go to site: main page, start]

0% found this document useful (0 votes)
64 views17 pages

Java Nested Loops Exercises

The document contains multiple Java programming exercises that involve generating various patterns and series using loops. Each question includes a specific output format, the corresponding Java code to achieve that output, and examples of the expected results. The exercises cover a range of topics including nested loops, conditionals, and user input handling.

Uploaded by

prad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views17 pages

Java Nested Loops Exercises

The document contains multiple Java programming exercises that involve generating various patterns and series using loops. Each question includes a specific output format, the corresponding Java code to achieve that output, and examples of the expected results. The exercises cover a range of topics including nested loops, conditionals, and user input handling.

Uploaded by

prad
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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)
[Link]("# ");
else
[Link]("* ");
}
[Link]();
}
}
}
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++) {
[Link]('5');
}
[Link](' ');
}
}
}

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++) {
[Link](a++ + "\t");
}
[Link]();
}
}
}
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)
[Link](i + " ");
else
[Link](ch + " ");
}
[Link]();
}
}
}

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)
[Link](ch2 + " ");
else
[Link](ch1 + " ");
}
[Link]();
}
}
}

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++) {
[Link](i);
}
for (int k = 1; k <= 5 - i; k++) {
[Link]('*');
}
[Link]();
}
}
}

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--)
[Link](ch + " ");
[Link](i);
[Link]();
}
}
}

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++) {
[Link]((char)j);
}
[Link]();
}
}
}

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--)
[Link]( j + " ");
[Link]();
}
}
}

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 = [Link]();
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
[Link]([Link](j) + " ");
}
[Link]();
}
}
}

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++)
[Link](j + " ");
[Link]();
}
for(int i = 2; i <= 5; i++) {
for(int j = 1; j <= i; j++)
[Link](j + " ");
[Link]();
}
}
}

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++) {
[Link](" ");
}
for (int k = i; k <= 5; k++) {
[Link](k + " ");
}
[Link]();
}
for(int i = 4; i >= 1 ; i--) {
for (int j = 1; j < i; j++) {
[Link](" ");
}
for (int k = i; k <= 5; k++) {
[Link](k + " ");
}
[Link]();
}
}
}
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 [Link];

public class KboatPattern


{
public static void main(String args[]) {

Scanner in = new Scanner([Link]);


[Link]("Type 1 for a triangle");
[Link]("Type 2 for an inverted triangle");

[Link]("Enter your choice: ");


int ch = [Link]();

[Link]("Enter the number of terms: ");


int n = [Link]();

switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](i + " ");
}
[Link]();
}
break;

case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
[Link](i + " ");
}
[Link]();
}
break;

default:
[Link]("Incorrect Choice");
}
}
}

Output

Common questions

Powered by AI

The program uses nested loops to print a series of the digit '5'. The outer loop iterates over the range from 1 to a given number (e.g. 6), and the inner loop prints the digit '5' i times, where i is the current iteration number of the outer loop. This results in patterns such as 5, 55, 555, etc.

In Java, to creatively use the string 'JAVA' in pattern creation, divide the printing into line-wise character extraction while iterating through indices. For each line, loop from the initial position to the current index, increasing by each letter, forming a triangular display, like J, JA, JAV, and JAVA. This can be controlled through nested loops using standard string length extraction methods.

The pattern involves both numbers and the asterisk '*' character, where numbers increase from 1 line by line and '*' fills the remaining characters up to a fixed column width. The outer loop controls the row count, and an inner loop prints the current row number followed by '*' such that the total count equals a fixed number (e.g., 5). This generates mixed content rows with numbers aligned starting from the left.

A mirrored number pyramid pattern can be achieved by employing two sequential for loop structures within a single main function. The first loop sequence creates the upward progression of a number pyramid by iterating and printing numbers from 1 to 5, and the second loop sequence mirrors it downwards by reducing the count back to 1. Careful control of loop indices and printing controls the symmetrical output.

The Java program generates a pattern where it alternates between printing '*' and '#' based on whether the column index (j) is even or odd. If j is even, it prints '#'; otherwise, it prints '*'. This is achieved using a nested loop that iterates from 1 to a given limit (e.g., 5), incrementing the range with each outer loop iteration.

Conditional logic in loops, such as using if-else statements, can create varied output patterns by determining the character or number to print based on loop counter properties, like odd or even checks. Nested loops extend patterns row by row, and conditional statements inside them decide the specific character based on the current index value, creating visually structured outputs.

By leveraging ASCII values, a Java program can iterate over numeric values, convert them to characters, and print these as part of a pattern. For example, starting from 'A' (ASCII 65) to 'E' (ASCII 69), nested loops can decrement the endpoint progressively for each line, using `System.out.print((char)j);` within loops to convert and print letters sequentially, forming decreasing patterns of letters.

To generate an inverted descending sequence of numbers, a nested loop approach is used. The outer loop runs from a starting number down to 1. For each iteration of the outer loop, the inner loop runs from the current outer loop index down to 1, printing each number in the sequence on the same line. The program continues until the outer loop completes its iteration, resulting in a triangular pattern of descending numbers.

To modify a Java program to print an inverted pyramid pattern using characters, adjust the loop to start from a higher number (such as 5) and decrement with each iteration. Use nested loops where the outer loop determines the number of lines and the inner loop prints spaces followed by the chosen character, reducing the number of characters printed with each descending line iteration. This will invert the pyramid into a triangular pattern.

In the Java program, user input determines whether a standard triangle or an inverted triangle is generated. The user enters a choice (1 or 2) and a number of terms, which guides the program's switch-case structure to either iterate from 1 to n for a triangle or n to 1 for an inverted triangle, printing the number repeatedly in each line corresponding to the iteration level.

You might also like