CHAPTER9
CHAPTER9
1. What are loop control structures? What are the essential segments of a loop control structure?
Ans. A loop is a set of instructions that is continually repeated until a certain condition is met. The set of
instructions may be repeated any number of times, as per the requirement.
Essential parts of a looping control structure are:
(i) initialization
(ii) test – condition
(iii) update – next step
(iv) body of the loop
(ii) The continue statement skips the rest of the current iteration of the loop. It jumps back to the
beginning of the loop and continues with the next iteration, including the evaluation of loop controlling
condition.
(iii) System.exit(0) method exits current program by terminating running Java virtual machine.
(iv) The return keyword causes the control to transfer back to the method call.
Ans.
a. The condition is logically incorrect.
b. Value of x is never changed resulting into an infinite loop.
iii. while (x == y) ;
{ xx = yy;
x = y; }
Ans. a. While statement should not have a statement terminator (;).
b. Increment counters missing.
4. What is an empty statement? Explain its usefulness.
Ans. An empty statement is any statement that is purported to provide information, but in reality it
provides no information at all in the relevant conversational context. It contains a null statement i.e.,
just a semicolon (;).
5. Convert the following for loop statement into the corresponding while loop and do-while loop:
int sum = 0; for (int i = 0; i <= 50; i++) sum = sum + i;
Ans.
while loop do-while loop
6. What are the differences between while loop and do-while loop?
Ans. In while loop, control checks for the condition and then executes the loop. If condition is true the
loop is executed and if it is false the loop will not be executed.
In do while loop, the loop is executed once and then the control checks for the condition for further
execution. Even if the condition is not met, the do-while loop is always executed once.
7. How many times are the following loop bodies repeated? What is the fi nal output in each case?
i. int x = 2; ii. int y = 2;
while (x < 20) while (y < 20)
if (x % 2 == 0) if (y % 2 == 0)
System.out.println(x); System.out.println(y++);
Ans. Infinite loop. Ans. Infinite loop.
iii. int z = 2;
while (z < 20)
if ((z++) % 2 == 0)
System.out.println(z);
Ans. The loop body is executed 9 times.
Output
3
www.bhuvantechs.com
5
7
9
11
13
15
17
19
System.out.println(num);
}
System.out.println("Finished");
Ans. Output
8
6
4
0
Finished
12. Write a program using do – while loop to compute the sum of the first 500 positive odd integers.
Ans. import java.util.Scanner;
public class CountoddNumbers
{
public static void main(String[] args)
{
int num=1,sum=0;
do {
if(num % 2 != 0)
{
sum=sum+num;
www.bhuvantechs.com
14. Write a program to convert kilograms to pounds in the following tabular format
(1 kilogram is 2.2 pounds):
Ans.
import java.util.Scanner;
public class pound
{
public static void main(String[] args)
{
www.bhuvantechs.com
double pou,i;
System.out.println("KILOGRAM POUND");
for (i=1.0;i<=20.0;i=i+1)
{
pou=i*2.2;
System.out.println(i+" "+pou);
}
}
}
16. Write a program in Java to read a number and display its digits in the reverse order. For example,
if the input number is
2468, then the output should be 8642.
Enter a number: 2468
Original number: 2468
Reverse number: 8642
Ans.
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter the number");
int num=input.nextInt();
www.bhuvantechs.com
int reversed = 0;
while(num != 0)
{
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed); }
}
18. Write a program to read the number n using the Scanner class and print the Tribonacci series:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 …and so on.
[Hint: The Tribonacci series is a generalisation of the Fibonacci sequence where each term is the sum
of the three preceding terms.]
Ans. import java.util.Scanner;
public class Tribonacci {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number till u want Tribonacci series: ");
int n=sc.nextInt(); int a = 0,b = 0,c = 1;
int d = a+b+c;System.out.println("\nTribonacci Series: ");
www.bhuvantechs.com
System.out.print(a+"\t"+b+"\t"+c);
for(int i=4;i<=n;i++){
System.out.print("\t"+d);
a=b;
b=c;
c=d;
d=a+b+c; }
}
}
20. Write a program to display the following patterns as per the user’s choice.
Pattern 1 Pattern 2
II
CC
SS
EE
Ans. Import java.util.Scanner;
public class pattern
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Pattern 1 Pattern 2");
System.out.println(" I I");
System.out.println(" C C");
System.out.println(" S S");
System.out.println(" E E");
System.out.print("Enter the pattern number to view: ");
int n=sc.nextInt();
if(n==1)
{
System.out.println("Pattern 1");
System.out.println(" I ");
www.bhuvantechs.com
System.out.println(" C ");
System.out.println(" S ");
System.out.println(" E ");
}
if(n==2)
{
System.out.println(" Pattern 2");
System.out.println(" I");
System.out.println(" C");
21. Write a menu-driven program to display the pattern as the per user’s choice:
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A
For an incorrect option, an appropriate error message should be displayed. [ICSE-2018]
Ans. import java.util.Scanner;
public class pat
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("**************MENU*************");
System.out.println("Type 1 for Pattern 1 and ");
System.out.println("Type 2 for Pattern 2");
System.out.print("Enter the number: ");
int n=sc.nextInt();
char i,j;
String word="BLUE";
switch(n)
{
case 1:
for(i='E'; i>='A'; i--)
{
for(j='A'; j<=i; j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
break;
case 2:
www.bhuvantechs.com
22. Write a program to input a number and check and print whether it is a Pronic number or not.
(Pronic number is a number which is the product of two consecutive integers.)
Example: 12 = 3 × 4
20 = 4 × 5
42 = 6 × 7 [ICSE-2018]
Ans. import java.util.Scanner;
public class pronicnumber {
public static void main(String args[]){
Scanner sc = new Scanner(System.in); int flag = 0;
System.out.print("Enter a number : "); int n = sc.nextInt();
for(int i=0; i<n; i++) {
if(i*(i+1) == n) {
flag = 1;
break; }
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number."); }
}
23. Write a program to accept a number and check and display whether it is a spy number or not.
(A number is called a spy number if the sum of its digits equals the product of the digits.)
Example: Consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8.
Product of the digits = 1 * 1 * 2 * 4 = 8. [ICSE-2017]
Ans. import java.util.Scanner;
public class SpyNumber {
static boolean isSpy( int num) {
int sum=0; int product = 1; int saveNum = num;
while (num != 0) {
int digit = num % 10; sum = sum + digit;
product = product * digit; num = num / 10; }
www.bhuvantechs.com
return(sum == product) ; }
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer (-1 to exit)");int number = keyboard.nextInt();
while (number != -1){
if (isSpy(number))
System.out.println(number + " is a Spy number");
else
System.out.println(number + " is NOT a Spy number");
24. Write a program to accept a number and check and display whether it is a Niven number or not.
(Niven number is anumber which is divisible by the sum of its digits).
Example:
Consider the number 126.
Sum of its digits is 1 + 2 + 6 = 9 and 126 is divisible by 9. [ICSE-2016]
Ans. import java.util.Scanner;
public class NivenNumber {
static boolean isNiven( int num) {
int sumOfDigits=0; int temp = num;
while(temp != 0) {
int digit = temp % 10;
sumOfDigits = sumOfDigits + digit;
temp = temp / 10; }
return(num % sumOfDigits == 0) ; }
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer (0 to exit)"); int number = keyboard.nextInt();
while (number != 0) {
if (isNiven(number))
System.out.println(number + " is a Niven number");
else
System.out.println(number + " is NOT a Niven number");
System.out.println("Enter an integer (0 to exit)"); number = keyboard.nextInt(); }
System.out.println("Exiting..."); keyboard.close(); }
}
25. Using the switch statement, write a menu driven program to:
i. Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5…. The first two
Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
ii. Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, an appropriate error message should be displayed. [ICSE-2012]
Ans. import java.util.Scanner;
www.bhuvantechs.com
26. Write a menu driven program to accept a number and check and display whether (i) it is a Prime
Number or not (ii) it is an Automorphic Number or not. (Use switch-case statement).
i. Prime number: A number is said to be a prime number if it is divisible only by 1 and itself and not by
any other number. Example: 3, 5, 7, 11, 13 etc.
ii. Automorphic number: An automorphic number is the number which is contained in the last digit(s)
of its square. Example: 25 is an automorphic number as its square is 625 and 25 is present as the last
www.bhuvantechs.com
}
if(g==no)
System.out.println("Automorphic number");
else
System.out.println("Not Automorphic number");
break;
default:
System.out.println("Wrong choice");
}
27. Write a menu driven program to accept a number from the user and check whether it is a BUZZ
number or to accept any two numbers and to print the GCD of them.
i. A BUZZ number is the number which either ends with 7 or is divisible by 7.
ii. GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide
the larger number by the smaller; the remainder then divides the previous divisor. The process is
repeated till the remainder is zero. The divisor then results the GCD. [ICSE-2009]
Ans. import java.util.Scanner;
public class BUZZGCDnumber
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("1. Buzz Number");
System.out.println("2. GCD");
System.out.print("Enter your choice: ");
int choice = scan.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter a number: ");
int num = scan.nextInt();
int lastDigit = num % 10;
int remainder = num % 7;
if (lastDigit == 7 || remainder == 0)
System.out.println(num + " is a buzz number");
else
System.out.println(num + " is not a buzz number");
break;
case 2:
System.out.print("Enter two numbers: ");
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int gcd = 1;
for(int i = 1; i <= num1 && i <= num2; ++i)
{
if(num1 % i==0 && num2 % i==0)
www.bhuvantechs.com
gcd = i;
}
System.out.printf("G.C.D of %d and %d is %d", num1, num2, gcd);
break;
}
}
}
28. Write a program to read the number x using the Scanner class and compute the series:
29. Write a program in Java to compute and display factorial of numbers up to a number entered via
the Scanner class. The output should look like as shown below when 7 is input.
Enter a number: 7
1! ( =1) = 1
2! ( =1 × 2) = 2
3! ( =1 × 2 × 3) = 6
4! ( =1 × 2 × 3 × 4) = 24
5! ( =1 × 2 × 3 × 4 × 5) = 120
6! ( =1 × 2 × 3 × 4 × 5 × 6) = 720
7! ( =1 × 2 × 3 × 4 × 5 × 6 × 7) = 5040
Ans. import java.util.Scanner;
class Factorial
{
www.bhuvantechs.com
30. Write a program in Java to find the sum of the given series:
(i)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x ");
int x=in.nextInt();
System.out.println("Enter the value of n terms ");
int n=in.nextInt();
double i;
double sum=0;
for(i = 1;i <=n; i ++)
{
sum=sum+(Math.pow(x,i));
}
System.out.println("The sum is "+sum);
}
}
(ii)Ans. import java.util.Scanner;
class series
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of n terms ");
www.bhuvantechs.com
int n=in.nextInt();
double i,x=3;
double sum=0;
for(i = 1;i <=n; i ++)
{
if(i%2==0)
sum=sum-(Math.pow(x,i));
else
sum=sum+(Math.pow(x,i));
{
Scanner in = new Scanner(System.in);
int i , num, x;
double frac, sum=0;
System.out.println("Enter the numerator");
x = in.nextInt();
for(i = 1; i <= 20; i ++)
{
frac = x/fact(i);
double sum=0;
for(i = 0; i <= 20; i ++)
{
sum=sum+(Math.pow(2,i)-1);
}
System.out.println("The sum is "+sum);
}
}
x. Ans. import java.util.Scanner;
31. Write a menu driven class to accept a number from the user and check whether it is Palindrome or
a Perfect number.
i Palindrome number: A number is a Palindrome which when read in reverse order is same as read in
the right order.
Example: 11, 101, 151, etc.
ii. Perfect number: A number is called Perfect if it is equal to the sum of its factors other than the
number itself.
Example: 6 = 1+2+3 [ICSE-2008]
Ans. import java.io.*;
import java.util.Scanner;
class PALINDROME_OR_PERFECT
{
public static void main(String args[])
{
int run,num,ch;
System.out.println("ENTER YOUR CHOICE :");
System.out.println("1 FOR PALINDROME ");
System.out.println("2 FOR PERFECT NUMBER ");
Scanner in = new Scanner(System.in);
ch= in.nextInt();
if(ch==1)
{
System.out.println("ENTER A NUMBER FOR FINDING WHETHER IT IS A PALINDROME");
num=in.nextInt();
www.bhuvantechs.com
int d;
int rev=0;
int num1=num;
do
{
d=num%10;
rev=(rev*10)+d;
num=num/10;
}while(num!=0);
32. Write a program to print the sum of negative numbers, sum of positive even numbers and sum of
positive odd numbers from a list of n numbers entered by the user. The program terminates when the
user enters a zero. [ICSE-2005]
Ans. import java.util.*;
public class Sums
{
void Sums()
{
int nn = 0, ne = 0, no = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter nos (Press 0 to stop): ");
int n;
www.bhuvantechs.com
do {
n = sc.nextInt();
if (n < 0) {
nn = nn + n;
} else if (n > 0) {
if (n % 2 == 0) {
ne = ne + n;
} else {
no = no + n;
www.bhuvantechs.com