Chapter 8
Chapter 8
******************************************************************************
Chapter 8
******************************************************************************
******************************************************************************
1. When the statements are repeated sequentially a number of times in a program, the construct is
known as:
a) iteration b) sequence
c) selection d) none
a) for b) while
c) do-while d) if-else
3. Which of the following loop does not execute even once if the condition is false in the beginning?
a) do-while b) while
a) loop b) continue
c) switch d) break
6. Which of the following loop checks the condition first and then execution begins?
a) do-while b) do
8. How many times the loop, for(i=1; ;i++), will execute, if there is no statement to terminate the
loop?
a) 1 b) 0
c) infinite d) none
for(i=10; i<10;i++)
Statement
For how many times will the given loop statement be executed:
a) never b) 1 time
c) 10 times d) infinite
Ans.
1. a) iteration
2. c) do-while
3. options b,c and d do not execute even once if the condition if false but option a will execute
atleast once even if the condition is false.
4. b) for(i=3;i<=30;i=i+3)
5. c) switch
6. c) while loop
7. b) ten times
8. c) infinite
9. c) switch
10. a) never
1. What do you understand by iterative process? How can it be resolved by using loop?
Ans. Iterative process is a process in which a set of instructions are performed repeatedly till the
condition is satisfied. The programming constructs used for this purpose are known as loops. Once
the control enters the loop, it executes the statements within the loop repeatedly till the condition
becomes false. When the condition becomes false, the control skips and moves to execute the rest
of the program.
A ‘for’ loop usually has a loop variable. A ‘for’ loop has three parts. They are as follows:
{
System.out.print(“Test message”);
System.out.print(p);
System.out.println();
The above loop is executed for with an initial value p=1 and ‘Test message1’is printed.
Then the body of the loop is executed again when p=2 and ‘Test message2’ is printed.
Later the loop is executed once again when p=3 and ‘Test message3’ is printed.
Finally, when p=4, the condition becomes false and the control breaks out of the loop. [That is, the
body of the loop is not executed for p=4.]
for loop
while loop
do while loop
Ans. A loop must have a condition which results in a boolean value and a loop variable with an initial
value and an update value. Sometime Loops like while(true) which do not involve loop variables
must have a condition within the body of the loop followed by a break statement to terminate the
loop.
In this looping structure, a condition is checked at the beginning of the loop module. If the given
condition is true then the control will enter the loop for execution. The process is repeated under
the condition is false. Since the testing of the condition takes place at the entry point of the loop, it is
termed as ‘Entry Controlled Loop’. Example : for loop, while loop.
while(<condition>)
In this structure, the condition is tested at the end of the looping construct. If the condition is true,
then the control is sent back to the beginning of the loop for the next iteration. As soon as the
condition is false, the control exits the loop. Since the condition is tested at the exit point of the
loop, it is said to be an ‘Exit Controlled Loop’. In this type of loop, even if the condition is false for the
first time, the control will enter the loop at least once. Example: do-while loop.
do
}while(condition);
Ans.
do while do { }while(<condition>);
a) break statement
break
The break statement is used for unusual termination of a block.
This means that as soon as the break statement is executed, the control will come out of the block
by ignoring the rest of the statements of the block and the remaining iterations of the loop.
Sometimes the desired result may be obtained even before the loop has finished all the iterations. In
such a situation, we can use break to terminate the loop.
The break statement is also used in a switch-case block to terminate a case and to avoid ‘fall
through’.
Example
if(i= =5)
break;
System.out.println( );
The above snippet prints the numbers from 1 to 4. When i=5 the break statement is executed and
hence the control comes out of the loop.
continue
When the continue statement is executed, it forces the control to skip the rest of the statements in
the current iteration and continue with the next iteration of the loop.
Example:
if(i= =5)
continue;
System.out.println(i);
The above snippet prints all the numbers from 1 to 10 except 5 because of the continue statement.
Ans. While writing a program, the programmer always has the flexibility to convert one type of loop
to another type of loop based on the programmer’s convenience and requirement.
9. What are the different ways to inter-convert the loops? Name them.
Ans. The various inter-conversion of loops that can be done are given below:
a) Finite loop
b) Delay loop
c) Infinite loop
d) Null loop
Ans.
a) Finite loop : When the statements run for a fixed number of times then the iteration is called as a
‘finite loop’. It can be further categorised as continuous loop and step loop.
Example:
int k=1;
while(k<=5)
System.out.println(k);
k++;
b) Delay loop or Null loop : A loop that does not include any statement to be repeated, is known as
Null loop. Basically, a null loop is used to create a delay in the execution. This can be done to pause
the control (or create a time delay) as needed by the user during programming. The body of the
delay loop does not contain any meaningful operation to perform.
Example:
c) Infinite loop: A loop which is executed endlessly is known as ‘Infinite Loop’. This type of loop never
comes to an end. Such ‘Infinite Loops’ are created when there are missing parameters . It may be a
missing condition or it may be a condition which never becomes false.
Example 1:
for(m=1; ;m<=10; )
In the above loop, since the update expression is missing, the condition always evaluates to true and
hence results in an ‘Infinite Loop’.
Example 2:
while(true)
In the above loop, the condition is always true. If there is no break statement in the body of the
loop, this loop becomes an infinite loop.
Example 3:
In the above loop, due to an incorrect logic the loop is executed endlessly. In this case, suppose if the
update expression was j++ instead of
for loop:
The for loop is a finite loop which can be used when the number of iterations is fixed.
The initialization of the loop variable and the update expression can be done in the ‘for statement’
itself.
while loop:
The while loop can be used when the number of iterations is fixed as well as unfixed. During an
unfixed iteration, the user may not be aware of the number of iterations during the process.
The initialization and the update expression cannot be given in the while statement. The initialization
must be done before the control enters the loop module and the update expression must be given in
the body of the loop.
while loop:
A while loop is an entry-controlled loop in which the condition is checked before the control starts
executing the body of the loop.
If the condition is false, the statements within the body of the loop are not executed.
Example:
int p=20;
while(p >0)
{
p = p/2;
System.out.println(p);
The above loop is executed for two iterations i.e. p=20 and p=10
do-while loop:
A do-while loop is an exit-controlled loop in which the condition is checked after executing the body
of the loop.
Even if the condition is false the statements in the body of the loop are executed at least once.
Example:
int p=20;
do
p = p/2;
System.out.println(p);
} while(p >100);
The above loop is executed once for p=20 although the condition p>100 is not satisfied. This is
because the condition was checked at the exit point of the loop.
12. State one difference and one similarity between while and do-while loop?
Difference:
A while loop is a entry-controlled loop in which the condition is checked before the control starts
executing the body of the loop. If the condition is false, the statements within the body of the loop
are not executed.
A do-while loop is an exit-controlled loop in which the condition is checked after executing the body
of the loop. So, even if the condition is false the statements in the body of the loop are executed at
least once.
Similarity:
Both the loops check a logical condition and only when the condition is false, the loop is terminated.
13. State one similarity and one difference between while and for loop.
Similarity : Both the ‘for’ loop as well as the ‘while ‘ loop are entry controlled or entry restricted
loops because the condition is checked before entering the loop. Only when the condition is true,
the loop is executed. Suppose if the condition is false, the control is transferred to the statement
after the looping construct.
Difference : The for loop is a finite loop which can be used when the number of iterations are fixed
The while loop can be used in both cases i.e. whether the number of iterations are known
or unknown (fixed or unfixed iterations).
14. Give two differences between Step loop and Continuous loop.
When the statements run for a fixed number of times then the iteration is called as a ‘infinite loop’.
It can be further categorised into continuous loop and step loop.
Step loop:
It is a loop in which the loop variable or the control variable is updated (increased or decreased) by a
given value, other than one after each iteration.
Example:
Continous loop:
It is a loop in which the loop variable or the control variable is updated (increased or decreased) by
one after each iteration.
Example:
1.
class dk1
int i;
for(i= -1;i<10; i++)
System.out.println(++ i);
}}}
Ans:
Since the print statement also has increment operation (prefix increment operation) the value of ‘i’
is incremented twice. There the output is as follows:
10
2.
class dk2
{
public static void main(String args[])
k *=i;
System.out.println(k);
}}
Ans:
Though the i=2 in the beginning during the condition, its value is incremented by 1. So the loop runs
for 3<6, 4<6, 5<6 and therefore the value of k=3*4*5=60. Therefore the output is 60.
3.
class dk3
int m=2,n=15;
for(int i=1;i<=5;i++)
m++;
–n;
System.out.println(“m=” +m);
System.out.println(“n=”+n);
}}}
Ans:
The initial value of m=2 and n=15. During every iteration the value of ‘m’ is increased by 1 and
printed. Likewise, the value of ‘n’ is decreased and then printed. Since there are 5 iterations (i=1 to
5) , ‘m’ value gets printed as m=3,m=4,m=5,m=6,m=7. Likewise, ‘n’ value gets printed as n=14, n=13,
n=12, n=11, n=10. Hence the output is as follows:
m=3
n=14
m=4
n=13
m=5
n=12
m=6
n=11
m=7
n=10
4. Determine how many times the body of the loop will be executed.
class dk4
while( x<=y)
y=y/x;
System.out.println(y);
}}}
Ans. The loop is executed twice.
First iteration
Second iteration
Third iteration
5<=2 condition is false. So the control doesn’t enter the body of the loop and the loop terminates.
10
1. Using do while:
class Test
{
int x,c;
for(x=10,c=20; c>=10;c=c-2)
x++;
System.out.println(x);
}}}
Ans.
class Test
do
{
x++;
System.out.println(x);
c=c-2;
}while (c>=10);
}}
2. Using do while:
class Pattern
int i,j;
for(i=5;i>=1;i –)
System.out.print(i);
}
System.out.println( );
}}
Ans.
class Pattern
int i=5,j;
do
System.out.print(i);
i–;
}while(i>=1);
System.out.println( );
}}
3. Using do while
class Number
int i,n=191,c=0;
for(i=1;i<=n;i++)
if(n%i==0)
c=c+1;
if(c==2)
System.out.println(“Prime”);
else
System.out.println(“Not Prime”);
}}
Ans.
class Number
int i=1,n=191,c=0;
do
if(n%i==0)
c=c+1;
i++;
}while(i<=n);
if(c==2)
System.out.println(“Prime”);
else
System.out.println(“Not Prime”);
}}
import java.io.*;
class Sample
int n,r;
System.out.println(“Enter a number”);
n=Integer.parseInt(in.readLine( ));
do
{
r=n%10;
n=n/10;
System.out.println(r );
while(n!=0);
}}
Ans.
import java.io.*;
class Sample
int n,r;
System.out.println(“Enter a number”);
n=Integer.parseInt(in.readLine( ));
while(n!=0)
r=n%10;
n=n/10;
System.out.println(r );
}}
1) Write the programs in Java to display the first ten terms of the following series:
1, 4, 9, 16………………
1, 2, 4, 7, 11,……………
3, 6, 9, 12………………
4, 8, 16, 32……………..
0, 7, 26…………………
1, 9, 25, 49……………..
0, 3, 8, 15………………
Solution:
a)
//1,4,9,16……
class Q1
for(int i=1;i<=10;i++)
System.out.println(i*i);
}}
b)
//1,2,4,7,11…….
class Q1
{int x=1;
for(int i=1;i<=10;i++ )
{System.out.println(x);
x=x+i;
}}
c)
//3,6,9,12…..
class Q1
for(int i=1;i<=10;i++ )
System.out.println(i*3);
}}
d)
//4,8,16,32
class Q1
for(int p=1;p<=10;p++ )
{i=i*2;
System.out.println(i); }
}}
e)
//1.5,3.0,4.5,6.0……
class Q1
for(int j=1;j<=10;j++)
System.out.println(j*1.5);
}}
f)
//0,7,26…..
class Q1
{static void series6()
for(int p=1;p<=10;p++ )
System.out.println((int)Math.pow(p,3)-1);
}}
g)
//1,9,25,49
class Q1
for(int p=1;p<=19;p=p+2)
System.out.println(p*p);
}}
h)
//4,16,36,64….
class Q1
for(int p=2;p<=20;p=p+2)
System.out.println(p*p);
}}
i)
//0,3,8,15…
class Q1
for(int p=1;p<=10;p++)
System.out.println(p*p-1);
}}
j)
//24,99,224,399,………..
class Q1
for(int p=5;p<=50;p=p+5 )
System.out.println(p*p-1);
}}
k)
//2,5,10,17,…..
class Q1
for(int p=1;p<=10;p++)
System.out.println(p*p+1);
}}
2) Write a program to input any 50 numbers (including positive and negative). Perform the following
tasks:
a) Count the positive numbers
Solution:
Import java.util.*;
class Q2
int x, p=0,n=0,sumpos=0,sumneg=0,count=0;
while(++count<=50)
x=sc.nextInt();
if(x>0)
p++;
sumpos+=x;
else if(x<0)
n++;
sumneg+=x;
}
3) Write a program to calculate the sum of all odd numbers and even numbers between a range of
numbers from m to n (both inclusive) where m<n. Input m and n (where m<n).
Solution:
Import java.util.*;
class Q3
int m,n,even=0,odd=0;
m=sn.nextInt();
n=sn.nextInt();
while(m<=n)
if(m%2==0)
even+=m;
else
odd+=m;
m++;
}
System.out.println(“Sum of even numbers=”+even);
4) Write a program to enter any 50 numbers and check whether they are divisible by 5 or not. If
divisible then perform the following tasks:
Solution:
import java.util.*;
class Q4
int i=0,n,count=0;
String s=””;
System.out.println(“Enter 50 numbers”);
while(++i<=50)
n=ob.nextInt();
if(n%5==0)
if(n%10==5)
s=s+”\t”+n;
else if(n%10==0)
count++;
5) Write a program to display all the numbers between m and n input from the keyboard (where
m<n, m>0, n>0) check and print the numbers that are perfect square. E.g. 25, 36,49……..are said to
be perfect square numbers.
Solution:
import java.util.*;
class Q5
int m,n;
String s=””;
m=ob.nextInt();
n=ob.nextInt();
while(m<n)
if(m==(int)Math.sqrt(m)*(int)Math.sqrt(m))
s=s+”\n”+m;
m++;
}
6) Write a program to display all the ‘Buzz Numbers’ between p and q (where p<q). A ‘Buzz Number’
is the number which ends with 7 or is divisible by 7.
Solution:
import java.util.*;
class Q6
int p,q;
String s=””;
p=ob.nextInt();
q=ob.nextInt();
while(p<q)
if(p%10==7 || p%7==0)
s=s+”\n”+p;
p++;
7) Write a program to input marks in English, Maths and Science of 40 students who have passed
ICSE Examination 2014. Now, perform the following tasks:
a) Number of students, who have secured 95% or more in all the subjects.
b) Number of students, who have secured 90% or more in English, Maths and Science.
Solution:
import java.util.*;
class Q7
int n=0,eng,math,sci,x=0,y=0;
while(++n<=40)
eng=sn.nextInt();
math=sn.nextInt();
sci=sn.nextInt();
x++;
y++;
1+4+9+……………………………..400
1+ 1/3 + 1/5……………………..1/19
2 – 4 + 6 – 8 +……………………-20
(1*2) +(2*3)+……………………(19*20)
Solution:
//1+4+9+……400
class Q8
int sum=0;
for(int j=1;j<=20;j++)
sum+=j*j;
import java.util.*;
class Q8
double sum=0;
for(int p=1;p<=20;p++)
sum+=1.0/p;
import java.util.*;
class Q8
double sum=0;
for(int p=1;p<=19;p=p+2)
sum+=1.0/p;
import java.util.*;
class Q9
double term,sum=0;
for(int p=1;p<=19;p++)
term=p/(p+1.0);
sum+=term;
//2-4+6-8+…….-20
import java.util.*;
class Q8
for(int i=2;i<=20;i=i+2)
if(i%4==0)
sum-=i;
else
sum+=i;
//(1*2) +(2*3)+……..(19*20)
import java.util.*;
class Q8
int sum=0;
for(int p=1;p<=19;p++)
sum+=p*(p+1);
9) Write a program to input a number and count the number of digits. The program checks whether
the number contains odd number of digits or even number of digits.
import java.util.*;
class Q9 {
System.out.println(“Enter a number”);
int n,y=0;
n=sn.nextInt();
while(n>0)
n=n/10;
System.out.println(“Number of digits=”+y);
if(y%2==0)
}}
10) Write a program to input a number and display the new number after reversing the digits of the
original number. The program also displays the absolute difference the original number and the
reversed number.
Solution:
import java.util.*;
class Q10 {
System.out.println(“Enter a number”);
int n,n1,n2,y=0,rev=0;
double d;
n=sn.nextInt();
n1=n2=n;;
while(n>0)
{y++;
n=n/10;
while(n1>0)
{ d=n1%10;
d=d*Math.pow(10,–y);
rev+=d;
n1=n1/10;
11) The Greatest Common Divisor (GCD) of two integers is calculated by the continued division
method. Divide the larger number by the smaller, the remainder then divides the previous divisor.
The process repeats unless the remainder reaches zero. The last divisor results in GCD.
[ICSE 2009]
Solution:
import java.util.*;
class Q11 {
int a,b,a1,b1,c=0;
a1=sn.nextInt();
b1=sn.nextInt();
a=Math.max(a1,b1);
b=Math.min(a1,b1);
while(true)
System.out.println(“***New iteration”);//OPTIONAL
c=a%b;
a=b;
System.out.println(“a=”+a);//OPTIONAL
b=c;
System.out.println(“b=”+b);//OPTIONAL
if(a%b==0)break;
System.out.println(“HCF=”+c);
}}
12) Write a program in Java to find the sum of the given series:
S=a2 + a2/2 + a2/3 + …………….+ a2/10
S= a1 + a2 + a3 + ………………….+ an
Solution:
a)
class Q12a {
{double term,sum=0;
for(int i=1;i<=10;i++)
{term=a*a/i;
sum+=term;
System.out.println(“Sum=”+sum);
}}
// S= a1 + a2/2 + a3/3 + …………….+ a10/10
class Q12b {
{double term,sum=0;
for(int i=1;i<=10;i++)
{term=Math.pow(a,i)/i;
sum+=term;
System.out.println(“Sum=”+sum);
}}
class Q12c {
{double term,sum=0;
for(int i=2;i<=20;i++)
{term=a*i;
sum+=term;
System.out.println(“Sum=”+sum);
}}
// S= a1 + a2 + a3 + ………………….+ an
import java.util.*;
class Q12d {
int a,n;
double sum=0;
a=sn.nextInt();
n=sn.nextInt();
for(int i=1;i<=n;i++)
sum+=Math.pow(a,i);
System.out.println(“Sum=”+sum);
}}
import java.util.*;
class Q12e {
int a,n;
double sum=0,term;
a=sn.nextInt();
n=sn.nextInt();
for(int i=1;i<=n;i++)
{term=Math.pow(i,i)/Math.pow(a,(i-1));
sum+=term;
System.out.println(“Sum=”+sum);
}}
import java.util.*;
class Q12f {
int a,n;
double sum=0,term;
a=sn.nextInt();
for(int i=1,x=1;x<=n;i=i+2,x++)
{term=Math.pow(i,2)/Math.pow(a,x);
sum+=term;
System.out.println(“Sum=”+sum);
}}
import java.util.*;
class Q12g {
int a,n;
double sum=0;
n=sn.nextInt();
for(int i=1;i<=n;i++)
sum+=1.0/Math.pow(a,i);
System.out.println(“Sum=”+sum);
}}
import java.util.*;
class Q12h {
int x;
double sum=0;
x=sn.nextInt();
for(double i=2.0;i<=20;i=i+3)
sum+=x/i;
System.out.println(“Sum=”+sum);
}}
13) In order to reach the top of pole, a monkey in his first attempt reaches to a height of 5 feet and
in the subsequent jumps, he slips down by 2% of the height attained in the previous jump. The
process repeats and finally the monkey reaches the top of the pole. Write a program to input the
height of the pole. Write a program to input height of the pole. Calculate and display the number of
attempts the monkey makes to reach the top of the pole.
Solution:
import java.util.*;
class Q13
double ht,x,y,cnt=0,sum=0;
ht=sn.nextDouble();
x=5;
while(sum<ht)
{y=98/100.0 * x;
sum+=y;
System.out.println(sum); //OPTIONAL
x=y;
cnt++;
System.out.println(“\n\n”+sum); //OPTIONAL
}}
14) Write a program to input Principal (P), Rate (R ) and Time (T).Calculate and display the amount,
which is compounded annually for each year by using the formula:
p=p+si;
[Hint : The amount after each year is the Principal for the next year.]
Solution:
import java.util.*;
class Q14 {
double p,r,t,si,amt=0;
p=sn.nextDouble();
r=sn.nextDouble();
t=sn.nextDouble();
for(int i=1;i<=t;i++)
{si=p*r*t/100.0;
amt=p+si;
}
System.out.println(“The amount after “+t+”years =”+amt);
}}
15) Write a menu driven program to input two positive numbers m and n (when m>n) and perform
the following tasks:
c) Find the quotient and remainder of two numbers without using ‘/’ and ‘%’ operator.
[Hint: The last value obtained after each subtraction is the remainder and the number of iterations
results in quotient.]
5-2=3
Solution:
import java.util.*;
class Q15 {
{
Scanner sn=new Scanner(System.in);
int m,n,p=0,sum=0,cnt=0,i;
System.out.println(“MENU”);
int choice=sn.nextInt();
m=sn.nextInt();
n=sn.nextInt();
switch(choice)
{
case 1: //Addition by repeated increment
sum=m;
for(i=1;i<=n;i++)
sum++;
System.out.println(“Sum=”+sum);
break;
for(i=1;i<=n;i++)
p+=m;
System.out.println(“Product=”+p);
break;
for(i=m;i>0;i=i-n)
{if(i-n<0)
break;
cnt++;
System.out.println(“Quotient=”+cnt);
System.out.println(“Remainder=”+i);
break;
} //end of if statement
else
}}
16) Write a Menu Driven class to accept a number from the user and check whether it is a
Palindrome or a Perfect number.
a) Palindrome number – (A number is a Palindrome which when read in reverse order is the same as
in the right order.)
b) 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
Solution:
import java.util.*;
class Q16
int n,n1,d,rev=0,sum=0,var;
System.out.println(“**MENU**”);
var=sn.nextInt();
n=sn.nextInt();
n1=n;
switch(var)
{case 1: //PALINDROME NUMBER
while(n>0)
d=n%10;
rev=rev*10+d;
n=n/10;
if(n1==rev)
else
break;
for(int j=1;j<n1;j++)
if(n1%j==0)
sum+=j;
if(n1==sum)
else
break;
default:System.out.println(“Invalid choice”);
}}
17) Write a Menu Driven program to accept a number from the user and check whether it is a Prime
number or an Automorphic number.
b) 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 in the last digit(s) of its
square.
Solution:
import java.util.*;
class Q17
int var,n,cnt=0,p;
boolean flag=true;
System.out.println(“**MENU**”);
System.out.println(“1.Prime number”);
System.out.println(“2.Automorphic number”);
var=sn.nextInt();
n=sn.nextInt();
switch(var)
for(int i=1;i<=n/2;i++)
{if(n%i==0)
cnt++;
break;
p=n*n;
while(n>0)
if(p%10 !=n%10)
{flag=false;
break;
else
{n=n/10;
p=p/10;
break;
default:System.out.println(“Invalid choice”);
}}
18) Write a menu driven program to perform the following tasks by using the ‘switch case’
statement. [ICSE 2011]
Solution:
import java.util.*;
class Q18 {
int n,j=1;
double sum=0.0;
System.out.println(“***MENU***”);
switch(ob.nextInt())
for(int i=1;i<=n;i++)
System.out.print(i*i-1+” ,”);
break;
n=ob.nextInt();
for(int i=1;i<=n;i++)
{sum+=j/(j+1.0);
j=i+2;
break;
}}}
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.
Solution:
import java.util.*;
class Q19 {
int n,sum=0,a=0,b=0,c=1;
System.out.println(“***MENU***”);
switch(ob.nextInt())
{a=b;
b=c;
c=a+b;
break;
n=ob.nextInt();
while(n>0)
{sum+=n%10;
n=n/10;
}
break;
}}
20) A special two-digit number is such that when the sum of its digits is added to the product of its
digits, the result is equal to the original two-digit number.
Sum of digits=5 + 9 = 14
Product of digits = 5* 9 = 45
Sum of the sum of the digits and product of digits = 14+ 45= 59
Write a program to accept a two-digit number. Add the sum of its digits to the products of its digits.
If the value is equal to the number input, then display the message “Special 2 digit number”
otherwise display the message “Not a special two digit number”.
Solution:
import java.util.*;
class Q20 {
int n,ld,rd,sum=0,pdt;
n=sn.nextInt();
rd=n%10;
ld=n/10;
sum=ld+rd;
pdt=ld*rd;
if(n==(sum+pdt))
else
}}
b) Find and display the factorial of a number input by the user (the factorial of a non-negative
integer n, denoted by n! is the product of all integers less than or equal to n.
import java.util.*;
class Q21 {
int n,fact=1;
System.out.println(“***MENU***”);
System.out.println(“1.Factors of a number”);
System.out.println(“2.Factorial of a number”);
switch(ob.nextInt())
System.out.println(“Enter a number”);
n=ob.nextInt();
{ if(n%i==0)
System.out.print(i+”,”);
break;
n=ob.nextInt();
for(int j=1;j<=n;j++)
fact=fact*j;
break;
}}
22) Write a program to input a number. Check and display whether it is a Niven number or not. ( A
number is said to be Niven which is divisible by the sum of its digits)
Solution:
import java.util.*;
class Q22 {
{
Scanner sn=new Scanner(System.in);
int sum=0,num,n,d;
n=sn.nextInt();
num=n;
while(n>0)
{d=n%10;
sum+=d;
n=n/10;
if(num%sum==0)
else
}}
23) Write a program to accept a number and check whether it is a ‘Spy number’ or not. ( A number is
Spy if the sum of its digits equals the product of its digits)
Solution:
import java.util.*;
class Q23 {
int sum=0,pdt=1,n,d;
n=sn.nextInt();
while(n>0)
{d=n%10;
sum+=d;
pdt*=d;
n=n/10;
if(sum==pdt)
else
}}
24) Using switch statement, write a menu driven program for the following:
1,11,111,1111,11111
For an incorrect option, an appropriate error message should be displayed. [ICSE 2017]
Solution:
import java.util.*;
class Q24 {
int x,s;
double term,sum=0;
System.out.println(“***MENU***”);
switch(ob.nextInt())
x=2;
for(int i=1;i<=20;i++)
{term=Math.pow(x,i);
if(i%2==1)
sum+=term;
else
sum-=term;
break;
s=0;
for(int j=1;j<=5;j++)
{s=s*10+1;
System.out.print(s+”,”);
if(s==0)
s++;
break;
default: System.out.println(“Invalid entry”);
}}}
*************