0% found this document useful (0 votes)
8 views36 pages

Nested Loop

The document discusses nested loops in programming, including true/false statements and fill-in-the-blank questions about their functionality. It provides examples of Java programs that utilize nested loops to compute series, check for twin primes, display numbers without zeros, and calculate student averages, among other tasks. Additionally, it covers concepts like 'break' and 'continue' statements in nested loops, along with sample code for various programming exercises.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views36 pages

Nested Loop

The document discusses nested loops in programming, including true/false statements and fill-in-the-blank questions about their functionality. It provides examples of Java programs that utilize nested loops to compute series, check for twin primes, display numbers without zeros, and calculate student averages, among other tasks. Additionally, it covers concepts like 'break' and 'continue' statements in nested loops, along with sample code for various programming exercises.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 1 - Unit 9

Nested Loop

Write whether the following statements are True/False

Question 1

Nested loop contains a single loop.


False

Question 2

When break statement is applied, it terminates the loop.


True

Question 3

The outer loop follows next iteration when iterations of inner loop is over.
True

Question 4

Nested loop means the using of two or more loops in a program.


False

Question 5

Labelled break statement allows the next iteration of the loop from any place of looping
structure.
False

Question 6

In a nested loop, break and continue can be used simultaneously.


False

Fill in the blanks

Question 1
Labelled break is used to terminate an outer loop.

Question 2

A loop within another loop is called nested loop.

Question 3

Termination of inner loop takes place before outer loop.

Question 4

continue statement will repeat a loop for the next iteration after ignoring some statements of the
loop.

Question 5

break statement terminates a switch-case statement as well as a loop.

Answer the following questions

Question 1

What is a nested loop?

When a loop is contained inside another loop it is termed as nested loops

Question 2

Write down the syntax of a nested for loop.

Below is the syntax of nested loop:

for (<initial value>; <test condition>; <update value>) {

for (<initial value>; <test condition>; <update value>) {

executable statement(s)

}
Question 3

What action will you take to terminate an outer loop from the block of an inner loop?

An outer loop can be terminated from the block of an inner loop by using Labelled break
statement as shown in the below example:

outer: for (int i = 1; i <= 5; i++) {


for (int j = 1; j <= 10; j++) {
[Link](j);
if (i == 3)
break outer;
}
}
[Link]("Outside outer loop");

Question 4

Give two differences between 'Break' and 'Continue'.

break continue

It is used to unconditionally jump to the next iteration of the


It is used to unconditionally
loop, skipping the remaining statements of the current
jump out of the loop
iteration.

It is used in switch-case and


It is only used in loops.
loops

Question 5

What is significance of 'break outer' and 'continue outer' in a nested loop?

'break outer' will terminate the loop that is labelled as outer in a nested loop and transfer
the program control to the statement just after the loop labelled as outer.
'continue outer' will skip the remaining statements of the nested loop and start the next
iteration of the loop that is labelled as outer.

Question 6

Write down the constructs (syntax) of:

(a) Nested do-while loop


do {
//statements of outer do-while loop
..
..
do {
//statements of inner do-while loop
} while (<condition>);
..
..
} while (<condition>);
(b) Nested while loop

while (<condition>) {
//statements of outer while loop
..
..
while (<condition>) {
//statements of inner while loop
}
..
..
}
Solutions to Unsolved Java Programs

Question 1

Write programs to find the sum of the following series:

(a) S = 1 + (3/2!) + (5/3!) + (7/4!) + ....... to n

import [Link];

public class KboatSeries


{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter n: ");
int n = [Link]();
double sum = 0.0;
for (int i = 1, j = 1; i <= n; i++, j = j + 2) {
double f = 1;
for (int k = 1; k <= i; k++) {
f *= k;
}
sum += j / f;
}
[Link]("Sum=" + sum);
}
}

Output

(b) S = a + (a/2!) + (a/3!) + (a/4!) + ....... + (a/n!)

import [Link];

public class KboatSeries


{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;

for (int i = 1; i <= n; i++) {


double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
sum += a / f;
}
[Link]("Sum=" + sum);

}
}

Output

(c) S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n

import [Link];

public class KboatSeries


{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;

for (int i = 1; i <= n; i++) {


double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
if (i % 2 == 0)
sum -= a / f;
else
sum += a / f;
}
[Link]("Sum=" + sum);

}
}

Output

(d) S = (a/2!) - (a/3!) + (a/4!) - (a/5!) + ....... + (a/10!)

import [Link];
public class KboatSeries
{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
double sum = 0;

for (int i = 2; i <= 10; i++) {


double f = 1;
for (int j = 1; j <= i; j++) {
f *= j;
}
if (i % 2 == 0)
sum += a / f;
else
sum -= a / f;
}
[Link]("Sum=" + sum);

}
}

Output
(e) S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ....... to n

import [Link];

public class KboatSeries


{
public void computeSum() {
Scanner in = new Scanner([Link]);
[Link]("Enter a: ");
int a = [Link]();
[Link]("Enter n: ");
int n = [Link]();
double sum = 0;
int lastPrime = 1;

for (int i = 1; i <= n; i++) {


for (int j = lastPrime + 1; j <= Integer.MAX_VALUE; j++) {
boolean isPrime = true;
for (int k = 2; k <= j / 2; k++) {
if (j % k == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
sum += j / [Link](a, i);
lastPrime = j;
break;
}
}

}
[Link]("Sum=" + sum);
}
}

Output

Question 2

Write a program to input two numbers and check whether they are twin prime numbers or not.
Hint: Twin prime numbers are the prime numbers whose difference is 2.
For example: (5,7), (11,13), ....... and so on.

import [Link];

public class KboatTwinPrime


{
public void twinPrimeCheck() {

Scanner in = new Scanner([Link]);

[Link]("Enter first number: ");


int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

boolean isAPrime = true;

for (int i = 2; i <= a / 2; i++) {

if (a % i == 0) {
isAPrime = false;
break;
}

if (isAPrime && [Link](a - b) == 2) {

boolean isBPrime = true;

for (int i = 2; i <= b / 2; i++) {

if (b % i == 0) {
isBPrime = false;
break;
}

if (isBPrime)
[Link](a + " and " + b + " are twin
prime");
else
[Link](a + " and " + b + " are not twin
prime");
}
else
[Link](a + " and " + b + " are not twin
prime");
}
}

Output

Question 3

Write a program to display all the numbers between 100 and 200 which don't contain zeros at
any position.
For example: 111, 112, 113, ....... , 199

public class KboatNoZero


{
public void display() {

int count = 0;
for (int i = 100; i <= 200; i++) {

boolean isNoZero = true;


int t = i;
while (t > 0) {
if (t % 10 == 0) {
isNoZero = false;
break;
}

t /= 10;
}

if (isNoZero) {
[Link](i + " ");
count++;
}

//This will print 10 numbers per line


if (count == 10) {
[Link]();
count = 0;
}
}

}
}

Output
Question 4

Write a program to display all prime palindrome numbers between 10 and 1000.
[Hint: A number which is prime as well a palindrome is said to be 'Prime Palindrome'
number.]
For example: 11, 101, 131, 151,

public class KboatPrimePalindrome


{
public void displayPrimePalindrome() {

int count = 0;

for (int i = 10; i <= 1000; i++) {

int num = i, revNum = 0;


while (num != 0) {
int digit = num % 10;
num /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == i) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {

if (i % j == 0) {
isPrime = false;
break;
}

}
if (isPrime) {
[Link](i + " ");
count++;
if (count == 10) {
[Link]();
count = 0;
}
}
}
}
}
}

Output
Question 5

In an entrance examination, students have been appeared in English, Maths and Science papers.
Write a program to calculate and display average marks obtained by all the students. Take
number of students appeared and marks obtained in all three subjects by every student along with
the name as inputs.
Display the name, marks obtained in three subjects and the average of all the students.

import [Link];

public class KboatStudentMarks


{
public void studentMarks() {
Scanner in = new Scanner([Link]);
[Link]("Enter number of students: ");
int studentCount = [Link]();

String names[] = new String[studentCount];


int engMarks[] = new int[studentCount];
int sciMarks[] = new int[studentCount];
int mathsMarks[] = new int[studentCount];
double avgMarks[] = new double[studentCount];
double totalMarks = 0.0;

for (int i = 0; i < studentCount; i++) {


[Link]("Enter details of student " + (i + 1));
[Link]("Name: ");
[Link]();
names[i] = [Link]();
[Link]("Marks in English: ");
engMarks[i] = [Link]();
[Link]("Marks in Science: ");
sciMarks[i] = [Link]();
[Link]("Marks in Maths: ");
mathsMarks[i] = [Link]();
avgMarks[i] = (engMarks[i] + sciMarks[i] + mathsMarks[i])
/ 3.0;
totalMarks += avgMarks[i];
}

[Link]();

for (int i = 0; i < studentCount; i++) {


[Link]("Details of student " + (i + 1));
[Link]("Name: " + names[i]);
[Link]("English: " + engMarks[i]);
[Link]("Science: " + sciMarks[i]);
[Link]("Maths: " + mathsMarks[i]);
[Link]("Average: " + avgMarks[i]);
}

double classAvg = totalMarks / studentCount;


[Link]("\nAverage of all students is " +
classAvg);
}
}

Output
Question 6

Write a program in Java to enter a number containing three digits or more. Arrange the digits of
the entered number in ascending order and display the result.
Sample Input: Enter a number 4972
Sample Output: 2, 4, 7, 9

import [Link];

public class KboatDigitSort


{
public void sortDigits() {
Scanner in = new Scanner([Link]);
[Link]("Enter a number having 3 or more digits: ");
int OrgNum = [Link]();

for (int i = 0; i <= 9; i++) {


int num = OrgNum;
int c = 0;
while (num != 0) {
if (num % 10 == i)
c++;
num /= 10;
}
for (int j = 1; j <= c; j++) {
[Link](i + ", ");
}
}

[Link]();
}
}

Output
Question 7

Write a program to input a number and check whether it is 'Magic Number' or not. Display the
message accordingly.
A number is said to be a magic number if the eventual sum of digits of the number is one.
Sample Input : 55
Then, 5 + 5 = 10, 1 + 0 = 1
Sample Output: Hence, 55 is a Magic Number.
Similarly, 289 is a Magic Number.

import [Link];

public class KboatMagicNum


{

public static void main(String args[]) {

Scanner in = new Scanner([Link]);


[Link]("Enter number to check: ");
int num = [Link]();
int n = num;

while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}

if (n == 1)
[Link](num + " is Magic Number");
else
[Link](num + " is not Magic Number");

}
}

Output

Question 8

A number is said to be Multiple Harshad number, when divided by the sum of its digits,
produces another 'Harshad Number'. Write a program to input a number and check whether it is a
Multiple Harshad Number or not.
(When a number is divisible by the sum of its digit, it is called 'Harshad Number').
Sample Input: 6804
Hint: 6804 ⇒ 6+8+0+4 = 18 ⇒ 6804/18 = 378
378 ⇒ 3+7+8= 18 ⇒ 378/18 = 21
21 ⇒ 2+1 = 3 ⇒ 21/3 = 7
Sample Output: Multiple Harshad Number
import [Link];

public class KboatMultipleHarshad


{
public void checkMultipleHarshad() {

Scanner in = new Scanner([Link]);


[Link]("Enter number to check: ");
int num = [Link]();
int dividend = num;
int divisor;
int count = 0;

while (dividend > 1) {


divisor=0;
int t = dividend;
while (t > 0) {
int d = t % 10;
divisor += d;
t /= 10;
}

if (dividend % divisor == 0 && divisor != 1) {


dividend = dividend / divisor;
count++;
}
else {
break;
}
}

if (dividend == 1 && count > 1)


[Link](num + " is Multiple Harshad Number");
else
[Link](num + " is not Multiple Harshad
Number");
}
}

Output
Question 9

Write the programs to display the following patterns:

(a)

1
31
531
7531
97531

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
[Link](j + " ");
}
[Link]();
}
}
}

Output
(b)

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

public class KboatPattern


{
public void displayPattern() {
int a = 1;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
[Link](a++ + "\t");
}
[Link]();
}
}
}

Output
(c)

15 14 13 12 11
10 9 8 7
654
32
1

public class KboatPattern


{
public void displayPattern() {
int a = 15;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
[Link](a-- + "\t");
}
[Link]();
}
}
}

Output
(d)

1
10
101
1010
10101

public class KboatPattern


{
public void displayPattern() {
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
[Link](b + " ");
else
[Link](a + " ");
}
[Link]();
}
}
}

Output
(e)

55555
4444
333
22
1

public class KboatPattern


{
public void displayPattern() {
for (int i = 0; i < 5; i++) {

for (int j = i; j > 0; j--) {


[Link](" ");
}

for (int k = 5 - i; k > 0; k--) {


[Link]((5 - i) + " ");
}

[Link]();
}
}
}

Output
(f)

12345
22345
33345
44445
55555

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {

for (int j = 1; j < i; j++)


[Link](i + " ");

for (int k = i; k <= 5; k++)


[Link](k + " ");

[Link]();
}
}
}

Output
(g)

*
*#
*#*
*#*#
*#*#*

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
[Link]("# ");
else
[Link]("* ");
}
[Link]();
}
}
}

Output
(h)

54321
5432
543
54
5

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
[Link](j + " ");
}
[Link]();
}
}
}

Output
(i)

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

public class KboatPattern


{
public void displayPattern() {
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
[Link](a++ + "\t");
}
[Link]();
}
}
}

Output
Question 10

Write a program to generate a triangle or an inverted triangle till n terms based upon the user's
choice.

Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
1
22
333
4444
55555

Example 2:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 2
Enter the number of terms 6
Sample Output:
666666
55555
4444
333
22
1

import [Link];

public class KboatPattern


{
public void choosePattern() {

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
Question 11

Using the switch statement, write a menu driven program for the following:

(a) To print the Floyd's triangle:


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

(b) To display the following pattern:


I
IC
ICS
ICSE

For an incorrect option, an appropriate error message should be displayed.

import [Link];

public class KboatPattern


{
public void choosePattern() {
Scanner in = new Scanner([Link]);
[Link]("Type 1 for Floyd's triangle");
[Link]("Type 2 for an ICSE pattern");

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


int ch = [Link]();

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

case 2:
String s = "ICSE";
for (int i = 0; i < [Link](); i++) {
for (int j = 0; j <= i; j++) {
[Link]([Link](j) + " ");
}
[Link]();
}
break;
default:
[Link]("Incorrect Choice");
}
}
}

Output

You might also like