Nalanda Public School
Mehdipatnam
NOTES
UNIT- IX NESTED LOOPS Sub: Computer Applications
Class-X Teacher: Bushra Sultana
I. 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
II. 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.
III. 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++) {
System.out.println(j);
if (i == 3)
break outer;
}
}
System.out.println("Outside outer loop");
Question 4
Give two differences between 'Break' and 'Continue'.
break continue
It is used to unconditionally jump out of the loop It is used to unconditionally jump to the next iteration
of the loop, skipping the remaining statements of the
current iteration.
It is used in switch-case and loops It is only used in 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
}
..
..
}
IV. UNSOLVED 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 java.util.*;
public class SeriesA
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}
(b) S = a + (a/2!) + (a/3!) + (a/4!) + ....... + (a/n!)
import java.util.*;
public class SeriesB
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}
(c) S = a - (a/2!) + (a/3!) - (a/4!) + ....... to n
import java.util.Scanner;
public class SeriesC
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}
(d) S = (a/2!) - (a/3!) + (a/4!) - (a/5!) + ....... + (a/10!)
import java.util.*;
public class SeriesD
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
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;
}
System.out.println("Sum=" + sum);
}
}
(e) S = (2/a) + (3/a2) + (5/a3) + (7/a4) + ....... to n
import java.util.Scanner;
public class SeriesE
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a: ");
int a = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
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 / Math.pow(a, i);
lastPrime = j;
break;
}
}
}
System.out.println("Sum=" + sum);
}
}
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 java.util.*;
public class TwinPrime
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
boolean isAPrime = true;
for (int i = 2; i <= a / 2; i++) {
if (a % i == 0) {
isAPrime = false;
break;
}
if (isAPrime && Math.abs(a - b) == 2) {
boolean isBPrime = true;
for (int i = 2; i <= b / 2; i++) {
if (b % i == 0) {
isBPrime = false;
break;
}
if (isBPrime)
System.out.println(a + " and " + b + " are twin prime");
else
System.out.println(a + " and " + b + " are not twin prime");
}
else
System.out.println(a + " and " + b + " are not twin prime");
}
}
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 NoZero
{
public static void main(String args[]) {
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) {
System.out.print(i + " ");
count++;
}
//This will print 10 numbers per line
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
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 PrimePalindrome
{
public static void main(String args[]) {
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) {
System.out.print(i + " ");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
}
}
}
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 java.util.*;
public class StudentMarks
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int studentCount = in.nextInt();
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++) {
System.out.println("Enter details of student " + (i + 1));
System.out.print("Name: ");
in.nextLine();
names[i] = in.nextLine();
System.out.print("Marks in English: ");
engMarks[i] = in.nextInt();
System.out.print("Marks in Science: ");
sciMarks[i] = in.nextInt();
System.out.print("Marks in Maths: ");
mathsMarks[i] = in.nextInt();
avgMarks[i] = (engMarks[i] + sciMarks[i] + mathsMarks[i]) / 3.0;
totalMarks += avgMarks[i];
}
System.out.println();
for (int i = 0; i < studentCount; i++) {
System.out.println("Details of student " + (i + 1));
System.out.println("Name: " + names[i]);
System.out.println("English: " + engMarks[i]);
System.out.println("Science: " + sciMarks[i]);
System.out.println("Maths: " + mathsMarks[i]);
System.out.println("Average: " + avgMarks[i]);
}
double classAvg = totalMarks / studentCount;
System.out.println("\nAverage of all students is " + classAvg);
}
}
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 java.util.Scanner;
public class DigitSort
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number having 3 or more digits: ");
int OrgNum = in.nextInt();
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++) {
System.out.print(i + ", ");
}
}
System.out.println();
}
}
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 java.util.*;
public class MagicNum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
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)
System.out.println(num + " is Magic Number");
else
System.out.println(num + " is not Magic Number");
}
}
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 java.util.*;
public class MultipleHarshad
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number to check: ");
int num = in.nextInt();
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)
System.out.println(num + " is Multiple Harshad Number");
else
System.out.println(num + " is not Multiple Harshad Number");
}
}
Question 9
Write the programs to display the following patterns:
(a)
1
31
531
7531
97531
public class PatternA
{
public static void main(String args[]) {
for (int i = 1; i < 10; i = i + 2) {
for (int j = i; j > 0; j = j - 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
(b)
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15
public class PatternB
{
public static void main(String args[]) {
int a = 1;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
}
}
(c)
15 14 13 12 11
10 9 8 7
654
32
1
public class PatternC
{
public static void main(String args[]) {
int a = 15;
for (int i = 5; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(a-- + "\t");
}
System.out.println();
}
}
}
(d)
1
10
101
1010
10101
public class PatternD
{
public static void main(String args[]) {
int a = 1, b = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
if (j % 2 == 0)
System.out.print(b + " ");
else
System.out.print(a + " ");
}
System.out.println();
}
}
}
(e)
55555
4444
333
22
1
public class PatternE
{
public static void main(String args[]) {
for (int i = 0; i < 5; i++) {
for (int j = i; j > 0; j--) {
System.out.print(" ");
}
for (int k = 5 - i; k > 0; k--) {
System.out.print((5 - i) + " ");
}
System.out.println();
}
}
}
(f)
12345
22345
33345
44445
55555
public class PatternF
{
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 = i; k <= 5; k++)
System.out.print(k + " ");
System.out.println();
}
}
}
(g)
*
*#
*#*
*#*#
*#*#*
public class PatternG
{
public static void main(String args[]) {
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();
}
}
}
(h)
54321
5432
543
54
5
public class PatternH
{
public static void main(String args[]) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
(i)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class PatternI
{
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();
}
}
}
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 java.util.Scanner;
public class PatternTriangle
{
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");
}
}
}
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 java.util.* ;
public class FloydPattern
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
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();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}