Class 9th CBSE

Java Output

Find the Output

Q.1 What is the output of the following:

Solution  :- 

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

int rows = 5;

// outer loop
for (int i = 1; i <= rows; ++i) {

// inner loop to print the numbers
for (int j = 1; j <= i; ++j) {
System.out.print(j + ” “);
}
System.out.println(“”);
}
}
}

Q.2 What is the output of the following:

Solution  :- 

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

int weeks = 3;
int days = 7;

// outer loop
for(int i = 1; i <= weeks; ++i) {
System.out.println(“Week: ” + i);

// inner loop
for(int j = 1; j <= days; ++j) {

// continue inside the inner loop
if(j % 2 != 0) {
continue;
}
System.out.println(” Days: ” + j);
}
}
}
}

OUTPUT 

Q.3 What is the output of the following:

Solution  :- 

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

int weeks = 3;
int days = 7;

// outer loop
for(int i = 1; i <= weeks; ++i) {
System.out.println(“Week: ” + i);

// inner loop
for(int j = 1; j <= days; ++j) {

// break inside the inner loop
if(i == 2) {
break;
}
System.out.println(” Days: ” + j);
}
}
}

OUTPUT 

-> Explain the following with examples

(i)BREAK

BREAK :-
In Java, the break statement is used to terminate the execution of a loop, switch, or labeled block prematurely. It is often used within loops (such as for, while, or do-while) and switch statements to exit the loop or switch statement based on certain conditions.

Example :-
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (i == 5) {
break; // exit the loop when i equals 5
}
}

(ii)CONTINUE :- 

In Java, the continue statement is used to skip the current iteration of a loop and proceed to the next iteration. It is particularly useful when you want to skip certain parts of the loop’s body based on a condition without terminating the loop entirely.

Example :-
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // skip the rest of the loop body when i equals 2
}
System.out.println(i);
}

 

⦁ Write nested for loops that will display a multiplication table. Ask for an integer from the user. Continue to re-prompt the user as long as the input is less than 1 or greater than 10. When you get valid input display a table similar to what is shown below.

Solution :- 

import java.util.Scanner;

public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;

// Prompt the user for input until a valid number is entered
do {
System.out.print(“Enter an integer between 1 and 10: “);
number = scanner.nextInt();

// Check if the number is within the valid range
if (number < 1 || number > 10) {
System.out.println(“Invalid input. Please enter a number between 1 and 10.”);
}
} while (number < 1 || number > 10);

// Display the multiplication table
System.out.println(“Multiplication Table for ” + number + “:”);
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= number; j++) {
System.out.print((j * i) + “\t”);
}
System.out.println();
}
}
}

Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable

Popular Videos

Play Video

UX for Teams

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer

Play Video

SEO & Instagram

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer