0% found this document useful (0 votes)
7 views49 pages

Looping

Chapter 9 discusses iterative constructs in Java, focusing on different types of loops such as for, while, and do-while. It covers the elements that control loops, the syntax of each loop type, and provides examples and exercises to illustrate their functionality. The chapter also explains concepts like infinite loops, nested loops, and jump statements like break and continue.

Uploaded by

Pramit bro
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)
7 views49 pages

Looping

Chapter 9 discusses iterative constructs in Java, focusing on different types of loops such as for, while, and do-while. It covers the elements that control loops, the syntax of each loop type, and provides examples and exercises to illustrate their functionality. The chapter also explains concepts like infinite loops, nested loops, and jump statements like break and continue.

Uploaded by

Pramit bro
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
You are on page 1/ 49

Chapter 9

Iterative Constructs in
Java
Tick () the correct option.
1. A type of loop that is usually used when the number of iteration is known.
a. for b. while
c. do-while d. None of these
Ans. a. for
2. A type of loop when the loop executes atleast once even if the test expression evaluates to
‘true’.
a. for b. while
c. do-while d. None of these
Ans. c. do-while
3. In while and do-while loops, a statement causes control to be transferred directly to the
conditional expression that controls the loop.
a. break b. pause
c. start d. continue
Ans. d. continue
4. By using you can force immediate termination of loop, bypassing the conditional expression and
any remaining code in the body of the loop.
a. switch b. break
c. continue d. default
Ans. b. break
5. What is an infinite loop?
a. A loop that functions infinitely well b. A loop that runs forever
c. A loop that never starts d. A loop that will never function
Ans. b. A loop that runs forever
6. What is the value of k after the following code fragment?
int k = 0;
int n = 12
while (k < n)
{

117 Computer Applications – IX (ICSE Course) Answers


k = k + 1;
}
a. 0 b. 11 c. 12 d. Unknown
Ans. c. 12
7. Which of these jump statements can skip processing remainder of code in its body for a particular
iteration?
a. break b. return
c. exit d. continue
Ans. d. continue
8. Which looping process checks the test condition at the end of the loop?
a. for b. while
c. do-while d. no looping process checks the test condition
at the end
Ans. c. do-while
9. What’s wrong? for (int k = 2, k <= 12, k++)
a. the increment should always be ++k
b. the variable must always be the letter i when using a for loop
c. there should be a semicolon at the end of the statement
d. the commas should be semicolons
Ans. d. the commas should be semicolons
10. What’s wrong? while( (i < 10) && (i > 24))
a. the logical operator && cannot be used in a test condition
b. the while loop is an exit-condition loop
c. the test condition is always false
d. the test condition is always true
Ans. c. the test condition is always false

Section A
Answer the following questions.
1. Explain the term loop with an example.
Ans. A loop is a sequence of instruction s that is continually repeated until a certain condition is
reached.
The following program illustrates the use of for loop.
public class Loops

Computer Applications – IX (ICSE Course) Answers 118


{
static void NaturalNumbers()
{
int i;
for(i=1 ; i<=10 ; i++)
{
System.out.println(i);
}
}
}
Now let us learn how the components of the loop gets executed.
Step 1:
At the beginning the initialization expression is executed i.e. i=1 which assigns 1 to i.
Step 2:
Then the test expression is checked i.e. i<=10 which checks whether the value of i is less than or
equal to 10.
Step 3:
Since the test expression is true it enters the body of the loop i.e. System.out.println(i); is
executed which prints the current value of i in the next line.
Step 4:
After executing the loop-body, the update expression i.e. i++ is executed which increments the
value of i. (The value of i becomes 2 after the execution of i++, since initially i was 1).
Step 5:
After the update expression is executed, the test-expression is again evaluated. If it is true the
sequence is repeated from step number 3, otherwise the loop terminates.
2. Name the Elements that Control a Loop.
Ans.
Every loop has elements that control its execution. These elements can be grouped under the
following in general:
1. Initialization Expression(s). Before entering a loop, the variable that controls the execution
of it is called initialization. The initialization of a variable may be inside a loop or outside it.
Whatever it may be it is executed only once before the iteration starts.
2. Test Expression. The test-expression (also called test-condition) is a condition, which decides
whether the loop body will be executed or not. Till the test expression evaluates to true, the
loop body gets executed otherwise the loop is terminated.

119 Computer Applications – IX (ICSE Course) Answers


3. Update Expression(s). The update expression(s) change the value(s) of the control variable(s).
The update expression(s) is usually made to be executed; before the next iteration.
4. The Body of the Loop. The statements that are executed repeatedly (as long as the, test
expression is true) form the body of the loop.
3. What are the three types of looping statement available in Java?
Ans. The three types of looping statements are: for, while and do-while.
4. Classify loops depending upon the priority in which a test-expression is evaluated.
Ans.
Depending upon the way a test-expression is evaluated in a loop it can be classified under the
following:
Entry controlled loop
Exit control loop
In an entry controlled loop, the test expression is evaluated before entering into a loop whereas
in an exit controlled loop, the test expression is evaluated before exiting from the loop.
5. Give the general syntax of a for-loop.
Ans. The syntax of the for loop statement is
for (initialization expression(s); test expression; update expression(s))
{
body of the loop;
}
6. Give an example to show how multiple initialization and updation are performed in a for-
loop.
Ans.
int i, j;
for(i=1, j=10 ; i<=10 ; j-- , i++)
{
System.out.println(“i=” +i+ “ ”+ “j=”+j);
}
7. What is an empty for-loop. Give an example.
Ans.
If the loop doesn’t contain a body, it is called an empty loop. For example,
for(i=1;i<=100;i++);
Notice the semicolon at the end of the parenthesis, indicating that the for loop does not have
body and therefore no statement is repeatedly executed again and again.

Computer Applications – IX (ICSE Course) Answers 120


8. State one similarity and one difference between while and do while loop.
Ans. Similarity: Both the loops are generally used if the number of iterations are not known.
Difference: The while loop is an entry controlled loop and the do-while is an exit controlled
loop.
9. State one similarity and one difference between while and for loop.
Ans. Similarity: Both are entry controlled loops.
Difference: Initialisation, test expression and updation can be written at the beginning of a for-
loop, whereas in a while loop, the initialisation is written outside the loop, and the updation
inside the body of the loop.
10. What is meant by an infinite loop ? Give one example.
Ans. A loop whose test expression always results to true is an infinite loop. For example,
for(int i=1;i!=0;i++)
{
}
11. Give the general syntax of a while-loop. How do you create infinite loops using a while-loop
structure?
Ans. The syntax of a while loop is,
while (condition or test-expression)
{
body-of-the-loop;
}
12. Give the general syntax of a do-while loop. How do you create infinite loops using do-while
loop structure?
Ans. The syntax of the do-while loop is:
do
{
statement;
}while (condition);
To create an infinite you need to make the condition always result to true. For example,
do
{
System.out.println(“Hello”);;
}while (true);

121 Computer Applications – IX (ICSE Course) Answers


13. Compare loops according to its usage.
Ans. The for loop is appropriate when you know in advance how many times the loop will be executed,
i.e., the first and the last limit is already known. The other two loops while and do-while loops
are more suitable in situations when it is not known in advance when the loop will terminate.
The while should be preferred when you may not want to execute the loop-body even once (in
case the test condition is false), and the do-while loop should be preferred when you’re sure
you want to execute the loop-body at least once.
14. What are Nested-loops? Give an example of a nested for-loop to show its functionality.
Ans. A loop within another loop is called nested loop. For example,
int x , y;
for(x=1;x<=3;x++)
{
for(y=1; y<=3 ; y++)
{
System.out.println(x+ “ ” +y);
}
}
15. Name two jump statements and their use.
Ans. The ‘break’ statement is used to exit from a loop or switch block.
The ‘continue’ statement is used to skip the remaining statements in a loop and continue with
the next iteration.

B. Answer as directed:
1. Convert the following segment into an equivalent do loop.
int x,c;
for(x=10,c=20;c>10;c=c-2)
x++;
Ans.
int x,c;
x=10;
c=20;
do
{
c=c-2;
x++;
}while(c>10);

Computer Applications – IX (ICSE Course) Answers 122


2. Analyze the following program segment and determine how many times the body of loop will
be executed (show the working).
x=5; y=50;
while(x<=y)
{
y=y/x;
System.out.println(y);
}
Ans.
x y x<=y y=y/x
5 50 true 10
5 10 true 2
5 2 false -
The loop executes twice.
3. Convert the following segment into equivalence for loop.
int i=0;
while(i<=20)
{
System.out.print(i+” “);
i++;
}
Ans.
int i;
for(i=0;i<=20;i++)
{
System.out.print(i+ “ ”);
}
4. What will be the output of the following code?
int m=2;
int n=15;
for(int i = 1; i<5; i++);

123 Computer Applications – IX (ICSE Course) Answers


m++; –n;
System.out.println(“m=” +m);
System.out.println(“n=”+n);
Ans.
m=3
n=15
5. Give the output of the following program:
class Output1
{
static void find()
{
int n=1234, d ,s=0;
while(n!=0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
System.out.println(s);
}
}
Ans.
4321
6. Give the output of the following program:
class Output2
{
static void find()
{
int n=1234, d ,s=0,c=0;
while(n!=0)

Computer Applications – IX (ICSE Course) Answers 124


{
d=n%10;
s=s+d*(int)Math.pow(10,c++);
n=n/10;
}
System.out.println(s);
}
}
Ans. 1234

SECTION B

Write programs for the following.


1. Write a program to find the sum of 1st 10 odd natural numbers.
Ans.
class Q1
{
static void main()
{
int i,s=0;
for(i=1;i<=19;i+=2)
{
s=s+i;
}
System.out.println(s);
}
}
2. Write a program to find the sum of 1st 10 even natural numbers.
Ans.
class Q2
{
static void main()
{
int i,s=0;
for(i=2;i<=20;i+=2)

125 Computer Applications – IX (ICSE Course) Answers


{
s=s+i;
}
System.out.println(s);
}
}
3. Write a program to find the sum of all 3-digit even natural numbers.
Ans.
class Q3
{
static void main()
{
int i,s=0;
for(i=100;i<=998;i+=2)
{
s=s+i;
}
System.out.println(s);
}
}
4. Write a program to find the sum of all 3 digit odd natural numbers, which are multiples of 5.
Ans.
class Q4
{
static void main()
{
int i,s=0;
for(i=101;i<=999;i+=2)
{
if (i%5==0)
s=s+i;
}
System.out.println(s);
}
}

Computer Applications – IX (ICSE Course) Answers 126


5. Write a program to input an integer and find its factorial. Factorial of a number is the product
of all natural numbers till that number. For example factorial of 5 is 120 since 1×2×3×4×5=120.
Ans.
import java.util.*;
class Q5
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,f=1,n;
System.out.println(“Enter a number:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println(“Factorial=”+f);
}
}
6. Write a program to input an integer and print its factors.
For Example,
INPUT: Enter an integer:12
OUTPUT: Factors: 1 2 3 4 6 12
Ans.
import java.util.*;
class Q6
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n;
System.out.println(“Enter an integer:”);
n=sc.nextInt();
System.out.println(“Factors:”);
for(i=1;i<=n;i++)
{

127 Computer Applications – IX (ICSE Course) Answers


if (n%i==0)
System.out.print(i+“ ”);
}
}
}
7. Write a program to input an integer and count the number of factors.
Ans.
import java.util.*;
class Q7
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0;
System.out.println(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
System.out.println(“No. of Factors:”+c);
}
}
8. Write a program to input an integer and check whether it is a prime number or not.
Ans.
import java.util.*;
class Q8
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0;
System.out.println(“Enter an integer:”);
n=sc.nextInt();

Computer Applications – IX (ICSE Course) Answers 128


for(i=1;i<=n;i++)
{
if (n%i==0)
c++;
}
if(c==2)
System.out.println(“Prime number”);
else
System.out.println(“Not a Prime number”);
}
}
9. Write a program to input 10 integers and find their sum.
Ans.
import java.util.*;
class Q9
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
s=s+n;
}
System.out.println(“Sum of the numbers=”+s);
}
}
10. Write a program to input 10 integers and find the sum of even numbers only.
Ans.
import java.util.*;
class Q10
{

129 Computer Applications – IX (ICSE Course) Answers


static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2==0)
s=s+n;
}
System.out.println(“Sum of even numbers=”+s);
}
}
11. Write a program to input 10 integers and find the sum of two digit as well as three digit
numbers separately.
Ans.
import java.util.*;
class Q11
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s2=0,s3=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n>=10 && n<=99)
s2=s2+n;
if(n>=100 && n<=999)
s3=s3+n;
}
System.out.println(“Sum of 2 digit numbers=”+s2);
System.out.println(“Sum of 3 digit numbers=”+s3);
}
}

Computer Applications – IX (ICSE Course) Answers 130


12. Write a program to input 10 integers and display the largest integer.
Ans.
import java.util.*;
class Q12
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0,l=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
c++;
if(c==1)
l=n;
if(n>l)
l=n;
}
System.out.println(“Largest number=”+l);
}
}
13 Write a program to input 10 integers and display the largest as well as the smallest integer.
Ans.
import java.util.*;
class Q13
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0,l=0,s=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{

131 Computer Applications – IX (ICSE Course) Answers


n=sc.nextInt();
c++;
if(c==1)
s=l=n;
if(n>l)
l=n;
if(n<s)
s=n;
}
System.out.println(“Largest number=”+l);
System.out.println(“Smallest number=”+s);
}
}
14. Write a program to input 10 integers and display the largest even integer. In case there is no
even integer, it should print “No even integer found”.
Ans.
import java.util.*;
class Q14
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0,l=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2==0)
{
c++;
if(c==1)
l=n;
if(n>l)
l=n;
}

Computer Applications – IX (ICSE Course) Answers 132


}
if(c>0)
System.out.println(“Largest even number=”+l);
else
System.out.println(“No even integer found”);
}
}
15. Write a program to input 10 integers and display the largest even and smallest odd integer.
Ans.
import java.util.*;
class Q15
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0,l=0,p=0,s=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2==0)
{
c++;
if(c==1)
l=n;
if(n>l)
l=n;
}
else
{
p++;
if(p==1)
s=n;
if(n<s)
s=n;
}

133 Computer Applications – IX (ICSE Course) Answers


}
if(c>0)
System.out.println(“Largest even number=”+l);
else
System.out.println(“No even integer found”);
if(p>0)
System.out.println(“Smallest odd number=”+s);
else
System.out.println(“No odd integer found”);
}
}
16. Write a program to input 10 integers and check whether all the entered numbers are even
numbers or not.
Ans.
import java.util.*;
class Q16
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2!=0)
f=1;
}
if(f==0)
System.out.println(“All are even numbers”);
else
System.out.println(“All are not even numbers”);
}
}

Computer Applications – IX (ICSE Course) Answers 134


17. Write a program to input 10 integers and check whether all the entered numbers are same or
not.
For Example,
INPUT: Enter 10 numbers: 10 12 13 234 45 34 67 78 76 12
OUTPUT: All numbers are not same.
INPUT: Enter 10 numbers: 12 12 12 12 12 12 12 12 12 12
OUTPUT: All numbers are same.
Ans.
import java.util.*;
class Q17
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=0,m=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
m=n;
if(n!=m)
f=1;
}
if(f==0)
System.out.println(“All numbers are same”);
else
System.out.println(“All numbers are not same”);
}
}
18. Write a program to input 10 integers and check whether the entered numbers are in ascending
order or not.
For Example,
INPUT: Enter 10 numbers: 10 12 13 25 45 55 67 78 106 122
OUTPUT: The numbers are in ascending order.
INPUT: Enter 10 numbers: 25 34 56 67 12 32 43 21 23 111
OUTPUT: The numbers are not in ascending order.

135 Computer Applications – IX (ICSE Course) Answers


Ans.
import java.util.*;
class Q18
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=0,p=0;
System.out.println(“Enter 10 integers:”);
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i>1)
{
if(n<p)
f=1;
}
p=n;
}
if(f==0)
System.out.println(“The numbers are in ascending order”);
else
System.out.println(“The numbers are not in ascending order”);
}
}
19 Write a program to calculate and print the sum of odd numbers and the sum of even numbers
for the first n natural numbers. The integer n is to be entered by the user.
Ans.
import java.util.*;
class Q19
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,se=0,so=0;

Computer Applications – IX (ICSE Course) Answers 136


System.out.println(“Enter a number:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%2==0)
se+=n;
else
so+=n;
}
System.out.println(“Sum of even numbers=”+se);
System.out.println(“Sum of odd numbers=”+so);
}
}
20. Write programs to find the sum of each of the following series:
i. s=1*2+2*3+3*4+…+9*10
ii. s=1*2+3*4+5*6+…+9*10
iii. s=1*3+2*4+3*5+…+9*11
iv. s=1-2+3-4+5-6+…+19-20
1 1 1 1 1
v. s = 1 + + + + + ... +
2 3 4 5 10
1 2 3 4 9
vi. s = + + + + ... +
2 3 4 5 10
1 1 1 1 1
vii. s = 1 − + − + + ... −
2 3 4 5 10
Ans.
class Q20
{
static void i()
{
int i,s=0;
for(i=1;i<=9;i++)
{
s=s+i*(i+1);
}
System.out.println(“Sum =”+s);

137 Computer Applications – IX (ICSE Course) Answers


}
static void ii()
{
int i,s=0;
for(i=1;i<=9;i+=2)
{
s=s+i*(i+1);
}
System.out.println(“Sum =”+s);
}
static void iii()
{
int i,s=0;
for(i=1;i<=9;i++)
{
s=s+i*(i+2);
}
System.out.println(“Sum =”+s);
}
static void iv()
{
int i,s=0;
for(i=1;i<=20;i++)
{
if(i%2==0)
s=s-i;
else
s=s+i;
}
System.out.println(“Sum =”+s);
}
static void v()
{
int i;
float s=0;

Computer Applications – IX (ICSE Course) Answers 138


for(i=1;i<=10;i++)
{
s=s+(float)1/i;
}
System.out.println(“Sum =”+s);
}
static void vi()
{
int i;
float s=0;
for(i=1;i<=9;i++)
{
s=s+(float)i/(i+1);
}
System.out.println(“Sum =”+s);
}
static void vii()
{
int i;
float s=0;
for(i=1;i<=10;i++)
{
if(i==2 || i==10)
s=s-(float)i/(i+1);
else
s=s+(float)i/(i+1);
}
System.out.println(“Sum =”+s);
}
}
21. Write a program to input two integers say x and n and find the sum of the following series:
x2 x3 x 4 x 5 x6 x7 xn
i. x+ + + + + + + ... +
2 3 4 5 6 7 n
2 3 4 5 6 n
x x x x x x x
ii. + + + + + + ... +
2 3 4 5 6 7 n+1
x 3
x 4
x 5
x 6
x 7
x n+1
iii. x + + + + + + ... +
2

2 3 4 5 6 n

139 Computer Applications – IX (ICSE Course) Answers


x3 x4 x5 x6 x7 x n +1
vi. x + + + + + + ... +
2

2 3 4 5 6 n
x 2
x 3
x 4
x 5
x 6
x 7
xn
v. x − + + + + + − ... ±
2 3 4 5 6 7 n
Ans.
import java.util.*;
class Q21
{
static void i()
{
int i,x,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the values of x and n:”);
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i)/i;;
}
System.out.println(“Sum =”+s);
}
static void ii()
{
int i,x,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the values of x and n:”);
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i)/(i+1);;
}
System.out.println(“Sum =”+s);

Computer Applications – IX (ICSE Course) Answers 140


}
static void iii()
{
int i,x,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the values of x and n:”);
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i+1)/i;;
}
System.out.println(“Sum =”+s);
}
static void iv()
{
int i,x,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the values of x and n:”);
x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i+1)/i;;
}
System.out.println(“Sum =”+s);
}
static void v()
{
int i,x,n;
double s=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the values of x and n:”);

141 Computer Applications – IX (ICSE Course) Answers


x=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+Math.pow(x,i)/i;;
}
System.out.println(“Sum =”+s);
}
}
22. Write a program to find the sum of 1st 10 numbers of Fibonacci series i.e. 1,1,2,3,5,8,13….
Fibonacci series is such a series which starting from 1 and 1, and subsequent numbers are the
sum of the previous two numbers.
Ans.
class Q22
{
static void main()
{
int i,f=1,s=0,sum=0,t;
for(i=1;i<=10;i++)
{
t=f+s;
sum+=t;
f=s;
s=t;
}
System.out.print(“Sum=”+sum);
}
}
23. Write a program to print the first 15 numbers of the Pell series. Pell series is such a series
which starts from 1 and 2 , and subsequent numbers is the sum of twice the previous number
and the number previous to the previous number. Pell series: 1, 2, 5, 12, 29, 70, 169, 408, 985,
2378, 5741, 13860, …
Ans.
class Q23
{

Computer Applications – IX (ICSE Course) Answers 142


static void main()
{
int i,f=1,s=0,t;
for(i=1;i<=15;i++)
{
t=f+s*2;
System.out.print(t);
f=s;
s=t;
}
}
}
24. Write a program to find the sum of 1st 10 numbers of Lucas series i.e. 2,1,3,4,7,11,18,…. Lucas
series is such a series which starting from 2 and 1, and subsequent numbers are the sum of the
previous two numbers.
Ans.
class Q24
{
static void main()
{
int i,f=2,s=1,t,sum=0;
sum=f+s;
for(i=1;i<=8;i++)
{
t=f+s;
sum+=t;
f=s;
s=t;
}
System.out.println(“Sum=”+sum);
}
}
25. Write a program to input an integer and check whether it is perfect, abundant or deficient
number. If the sum of the factors excluding itself is equal to that number it is perfect, if
greater than that number it is abundant and if less than that number it is deficient number.

143 Computer Applications – IX (ICSE Course) Answers


Ans.
import java.util.*;
class Q25
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s=0;
System.out.println(“Enter an integer:”);
n=sc.nextInt();
for(i=1;i<n;i++)
{
if(n%i==0)
s+=i;
}
if(s==n)
System.out.println(“Perfect Number”);
else if(s>n)
System.out.println(“Abundant Number”);
else
System.out.println(“Deficient Number”);
}
}
26. Write a program to input two integers and check whether it forms an amicable pair or not. An
amicable pair is such that, the sum of the factors excluding itself of one number is the other
number and sum of the factors excluding itself of the other number is this number.
Example, (220, 284) . Since sum of factors excluding itself of:
220= 1+2+4+5+10+11+20+22+ 44+55+110=284
284= 1+ 2+4+71+142=220.
Ans.
import java.util.*;
class Q26
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,sa=0,sb=0;

Computer Applications – IX (ICSE Course) Answers 144


System.out.println(“Enter 2 integers:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=1;i<a;i++)
{
if(a%i==0)
sa+=i;
}
for(i=1;i<b;i++)
{
if(b%i==0)
sb+=i;
}
if(sa==b && sb==a)
System.out.println(“Amicable pair”);
else
System.out.println(“Not Amicable pair”);
}
}
27. Write a program to pass an integer as argument and find the sum of its digits.
Ans.
class Q27
{
static void main(int a)
{
int i,d,s=0;
for(i=a;i>0;i=i/10)
{
d=i%10;
s=s+d;
}
System.out.println(“Sum=”+s);
}
}

145 Computer Applications – IX (ICSE Course) Answers


28. Write a program to pass an integer as argument and find the sum of odd digits and even digits
separately.
Ans.
class Q28
{
static void main(int a)
{
int i,d,se=0,so=0;
for(i=a;i>0;i=i/10)
{
d=i%10;
if(d%2==0)
se=se+d;
else
so=so+d;
}
System.out.println(“Sum of even digits=”+se);
System.out.println(“Sum of odd digits=”+so);
}
}
29. Write a program to pass an integer as argument and find the average of its digits.
Ans.
class Q29
{
static void main(int a)
{
int i,d,s=0,c=0;
float avg;
for(i=a;i>0;i=i/10)
{
d=i%10;
s+=d;
c++;
}
avg=(float)s/c;
System.out.println(“Average=”+avg);
}
}

Computer Applications – IX (ICSE Course) Answers 146


30. Write a program to pass an integer as argument and print the largest as well as smallest digit.
Ans.
class Q30
{
static void main(int a)
{
int i,d,l=0,s=0,c=0;
for(i=a;i>0;i=i/10)
{
d=i%10;
c++;
if(c==1)
l=s=d;
if(d>l)
l=d;
if(d<s)
s=d;
}
System.out.println(“Largest=”+l);
System.out.println(“Smallest=”+s);
}
}
31. Write a program to input an integer and remove all the even digits from it.
For Example,
INPUT: Enter an integer: 1234
OUPUT: 13
Ans.
class Q31
{
static void main(int a)
{
int i,d,s=0,c=0;
for(i=a;i>0;i=i/10)
{
d=i%10;
if(d%2!=0)

147 Computer Applications – IX (ICSE Course) Answers


s=s+d*(int)Math.pow(10,c++);
}
System.out.println(“New number=”+s);
}
}
32. Write a program to pass an integer as argument and check whether all digits in it are even
digit or not.
Ans.
class Q32
{
static void main(int a)
{
int i,d,f=0;
for(i=a;i>0;i=i/10)
{
d=i%10;
if(d%2!=0)
f=1;
}
if(f==0)
System.out.println(“All are even digits”);
else
System.out.println(“All are not even digits”);
}
}
33. Write a program to pass an integer as argument and check whether the digits are in ascending
order or not.
Ans.
class Q33
{
static void main(int a)
{
int i,d,d1,f=0;
for(i=a;i>9;i=i/10)
{

Computer Applications – IX (ICSE Course) Answers 148


d=i%10;
d1=(i/10)%10;
if(d<d1)
f=1;
}
if(f==0)
System.out.println(“Digits are in ascending order”);
else
System.out.println(“Digits are not in ascending order”);
}
}
34. Write a program to pass an integer as argument and check whether it is Armstrong number
or not. Numbers whose sum of the cube of its digit is equal to the number itself is called
Armstrong numbers. Example 153=13+53 +33.
Ans.
class Q34
{
static void main(int a)
{
int i,d,s=0;
for(i=a;i>9;i=i/10)
{
d=i%10;
s=s+d*d*d;
}
if(s==a)
System.out.println(“Armstrong Number”);
else
System.out.println(“Not an Armstrong Number”);
}
}
35. Write a program to input an integer and check whether it is an automorphic, trimorphic or
tri-automorphic number or not. A number n is said to be automorphic, if its square ends in
n. For instance 5 is automorphic, because 52= 25, which ends in 5, 25 is automorphic, because
252=625, which ends in 25. A number n is called trimorphic if n3 ends in n. For example 493, =
117649, is trimorphic. A number n is called tri-automorphic if 3n2 ends in n; for example 667
is tri-automorphic because 3 × 6672, = 1334667, ends in 667.

149 Computer Applications – IX (ICSE Course) Answers


Ans.
import java.util.*;
class Q35
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,d,a,c=0;
System.out.println(“Enter an integer:”);
a=sc.nextInt();
for(i=a;i>0;i=i/10)
{
c++;
}
if((a*a)%(int)Math.pow(10,c)==a)
System.out.println(“Automorphic Number”);
else if((a*a*a)%(int)Math.pow(10,c)==a)
System.out.println(“Trimorphic Number”);
else if((3*a*a)%(int)Math.pow(10,c)==a)
System.out.println(“Tri-Automorphic Number”);
else
System.out.println(“Not any type of Automorphic Number”);
}
}
36. Write a program to input an integer and check whether it is Harshad or Niven number or
not. A number is said to be harshad if it is divisible by the sum of the digits of that number,
example 126 and 1729.
Ans.
import java.util.*;
class Q36
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,d,a,s=0;
System.out.println(“Enter an integer:”);
a=sc.nextInt();
for(i=a;i>0;i=i/10)

Computer Applications – IX (ICSE Course) Answers 150


{
d=i%10;
s+=d;
}
if(a%s==0)
System.out.println(“Harshad Number”);
else
System.out.println(“Not a Harshad Number”);
}
}
37. Write a program to input a number and check whether it is a Kaprekar number or not. Take
a positive whole number n that has d number of digits. Take the square n and separate the
result into two pieces: a right-hand piece that has d digits and a left-hand piece that has either
d or d-1 digits. Add these two pieces together. If the result is n, then n is a Kaprekar number.
Examples are 9 (92 = 81, 8 + 1 = 9), 45 (452 = 2025, 20 + 25 = 45), and 297 (2972 = 88209, 88 +
209 = 297).
Ans.
import java.util.*;
class Q37
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,d=0,a,s=0,f,l;
System.out.println(“Enter an integer:”);
a=sc.nextInt();
for(i=a;i>0;i=i/10)
{
d++;
}
s=a*a;
f=s/(int)Math.pow(10,d);
l=s%(int)Math.pow(10,d);
if(f+l==a)
System.out.println(“Kaprekar Number”);
else
System.out.println(“Not a Kaprekar Number”);
}
}

151 Computer Applications – IX (ICSE Course) Answers


38. Write a program to input two integers and find their Least Common Multiple(L.C.M).
For Example,
INPUT: Enter 2 integers:
12
8
OUTPUT: L.C.M. = 24
Ans.
import java.util.*;
class Q38
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,l=0;
System.out.println(“Enter 2 integer:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=a;i<=a*b;i++)
{
if(i%a==0 && i%b==0)
{
l=i;
break;
}
}
System.out.println(“L.C.M.=”+l);
}
}
39. Write a program to input two integers and find their Highest Common Factor(H.C.F).
For Example,
INPUT: Enter 2 integers:
12
8
OUTPUT: H.C.F. = 4
Ans.
import java.util.*;
class Q39
{

Computer Applications – IX (ICSE Course) Answers 152


static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,h=0;
System.out.println(“Enter 2 integers:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=a;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
h=i;
break;
}
}
System.out.println(“H.C.F.=”+h);
}
}
40. 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 same
as read in the right order) Example: 11, 101, 151, etc.
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
Ans.
import java.util.*;
class Q40
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,r=0,d,ch,s=0;
System.out.println(“Enter a number:”);
n=sc.nextInt();
System.out.println(“MENU”);

153 Computer Applications – IX (ICSE Course) Answers


System.out.println(“1.Palindrome Number”);
System.out.println(“2.Perfect Number”);
System.out.println(“Enter your choice:”);
ch=sc.nextInt();
switch(ch)
{
case 1:
for(i=n;i>0;i/=10)
{
d=i%10;
r=r*10+d;
}
if(r==n)
System.out.println(“Palindrome Number”);
else
System.out.println(“Not a Palindrome Number”);
break;
case 2:
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
System.out.println(“Perfect Number”);
else
System.out.println(“Not a Perfect Number”);
break;
default:
System.out.println(“Invalid choice”);
}
}
}

Computer Applications – IX (ICSE Course) Answers 154


41. Write a menu driven program to accept a number from the user and check whether it is
‘BUZZ’ number or to accept any two numbers and print the ‘GCD’ of them.
a. A BUZZ number is the number which either ends with 7 or divisible by 7.
b. 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.
Ans. import java.util.*;
class Q41
{
static void main()
{
Scanner sc=new Scanner(System.in);
int ch,n,r=0,a,b;
System.out.println(“MENU”);
System.out.println(“1.Buzz Number”);
System.out.println(“2.GCD”);
System.out.println(“Enter your choice:”);
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println(“Enter a number:”);
n=sc.nextInt();
if(n%7==0 || n%10==7)
System.out.println(“Buzz Number”);
else
System.out.println(“Not a Buzz Number”);
break;
case 2:
System.out.println(“Enter 2 numbers:”);
a=sc.nextInt();
b=sc.nextInt();
for(r=a%b;r>0;r=a%b)
{
a=b;
b=r;

155 Computer Applications – IX (ICSE Course) Answers


}
System.out.println(“GCD=”+b);
break;
default:
System.out.println(“Invalid choice”);
}
}
42. Write a menu driven program to accept a number and check and display whether it is a Prime
Number or not OR an Automorphic Number or not.
a. 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.
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 as the last two
digits.
Ans.
import java.util.*;
class Q42
{
static void main()
{
Scanner sc=new Scanner(System.in);
int ch,i,c=0,n;
System.out.println(“Enter a number:”);
n=sc.nextInt();
System.out.println(“MENU”);
System.out.println(“1.Prime Number”);
System.out.println(“2.Automorphic Number”);
System.out.println(“Enter your choice:”);
ch=sc.nextInt();
switch(ch)
{
case 1:
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;

Computer Applications – IX (ICSE Course) Answers 156


}
if(c==2)
System.out.println(“Prime Number”);
else
System.out.println(“Not a Prime Number”);
break;
case 2:
for(i=n;i>0;i=i/10)
{
c++;
}
if((n*n)%(int)Math.pow(10,c)==n)
System.out.println(“Automorphic Number”);
else
System.out.println(“Not an Automorphic Number”);
break;
default:
System.out.println(“Invalid choice”);
}
}
}
43. Write a program to input 10 integers and print the second largest number. Assume that there
is at least one second largest number in the given set of integers.
For Example,
INPUT: Enter 10 integers:
12 35 46 22 34 56 78 89 23 21
OUTPUT: Second Largest Integer: 78
Ans.
import java.util.*;
class Q43
{
static void main()
{
Scanner sc=new Scanner(System.in);
int l=0,sl=0,i,c=0,n;
System.out.println(“Enter 10 numbers:”);

157 Computer Applications – IX (ICSE Course) Answers


for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
l=n;
else if(n>l)
{
sl=l;
l=n;
}
else
{
if(l!=0)
{
if(sl==0)
sl=n;
if(n>sl)
sl=n;
}
}
}
System.out.println(“Second Largest=”+sl);
}
}
44. Write a program to find the sum of the following series:
S=1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)+…+(1+2+3+4+…+10)
(Please note that no nested loop is to be used)
Ans.
class Q44
{
static void main()
{
int i,s=0,p=0;
for(i=1;i<=10;i++)
{
p=p+i;

Computer Applications – IX (ICSE Course) Answers 158


s=s+p;
}
System.out.println(“Sum=”+s);
}
}
45. 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 numbers (N) entered by the user. The list terminates
when the user enters a zero.
Ans.
import java.util.*;
class Q45
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,se=0,so=0;
for(;true;)
{
System.out.println(“Enter a number:”);
n=sc.nextInt();
if(n==0)
break;
if(n%2==0 && n>0)
se+=n;
else if(n%2==1 && n>0)
so+=n;
}
System.out.println(“Sum of positive even no.s=”+se);
System.out.println(“Sum of positive odd no.s=”+so);
}
}
46. A game of dice is to be simulated for two players, each player gets a chance to throw his dice,
and the value is added to his points, this process continues alternately until for the player
whose added points equals to 20 and is declared the winner. Write a program to simulate this
process using the random( ) function.

159 Computer Applications – IX (ICSE Course) Answers


Ans.
class Q46
{
static void main()
{
int i,r1,r2,s1=0,s2=0;
for(i=1;s1<20 && s2<20;i++)
{
r1=1+(int)(Math.random()*20);
s1+=r1;
if(s1<20)
{
r2=1+(int)(Math.random()*20);
s2+=r2;
}
}
if(s1>s2)
System.out.println(“Winner is player 1”);
else
System.out.println(“Winner is player 2”);
}
}
47. Write a program to calculate and print the values of:-

x2 + y2
Z=
x+ y
Where, x ranges from 0 to 50 and y is to be taken as input.
Ans.
import java.util.*;
class Q47
{
static void main()
{
Scanner sc=new Scanner(System.in);
int x,y,z;

Computer Applications – IX (ICSE Course) Answers 160


for(x=0;x<=50;x++)
{
System.out.println(“Enter the value of y:”);
y=sc.nextInt();
z=(x*x+y*y)/(x+y);
System.out.println(“z=”+z);
}
}
}
48. Write a program to calculate and print the sum of each of the following series:
a. Sum (S) = 2 – 4 + 6 – 8 + ………. -20
x x x x x
b. Sum (S) = + + + + ... +
2 5 8 11 20
Ans.
import java.util.*;
class Q48
{
static void a()
{
int s=0,i;
for(i=1;i<=10;i++)
{
if(i%2==0)
s=s-2*i;
lse
s=s+2*i;
}
System.out.println(“Sum=”+s);
}
static void b()
{
Scanner sc=new Scanner(System.in);
int x,i;
float s=0;
System.out.println(“Enter the value of x:”);
x=sc.nextInt();
for(i=2;i<=20;i+=3)

161 Computer Applications – IX (ICSE Course) Answers


{
s=s+(float)x/i;
}
System.out.println(“Sum=”+s);
}
}
49. Write a program to find the sum of series, taking the value of ‘a’ and ‘n’ from the user.
a a a a
S = + + + ... +
2 3 4 n
Ans.
import java.util.*;
class Q49
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,n,i;
float s=0;
System.out.println(“Enter the value of a:”);
a=sc.nextInt();
System.out.println(“Enter the value of n:”);
n=sc.nextInt();
for(i=2;i<=n;i++)
{
s=s+(float)a/i;
}
System.out.println(“Sum=”+s);
}
}
50. Write a program to find the sum of series, taking the value of ‘a’ and ‘n’ from the user.
a + 1 a + 3 a + 5 a + 7 …upto n terms
s= + + +
2 4 6 8
Ans.
import java.util.*;
class Q50
{
static void main()
{

Computer Applications – IX (ICSE Course) Answers 162


Scanner sc=new Scanner(System.in);
int a,n,i,c=1;
float s=0;
System.out.println(“Enter the value of a:”);
a=sc.nextInt();
System.out.println(“Enter the value of n:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+(float)(a+c)/(c+1);
c+=2;
}
System.out.println(“Sum=”+s);
}
}
51. Write a program to find the sum of series, taking the value of ‘n’ from the user.
1 + 2 2 + 3 3 + 4 4 + 5 …upto n terms
s= + + +
2* 3 3* 4 4 * 5 5* 6
Ans.
import java.util.*;
class Q51
{
static void main()
{
Scanner sc=new Scanner(System.in);
int n,i;
float s=0;
System.out.println(“Enter the value of n:”);
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+(float)(i+(i+1))/((i+1)*(i+2));
}
System.out.println(“Sum=”+s);
}
}

163 Computer Applications – IX (ICSE Course) Answers


52. An interesting method of multiplication of integers (not very large numbers) is as follows.
The method can be illustrated by the following example. If the numbers 17 and 19 are to be
multiplied, they are put at the top of two columns as shown below:
17 19
The numbers at the left hand side is successively divided by 2 (integer division) while the
other is successively multiplied by 2. The results are written one below the other in their
respective columns. The process is repeated till the column containing the division results
reached 1. At this stage all the numbers in the right hand column are struck off where numbers
corresponding to it on the left hand column is even.
17 19
8 (even) 38
4 (even) 76
2 (even) 152
1 304
Now the remaining numbers on the right hand side are added: 19+304=323 which is equal to
the product of 17 and 19.
Write a program to input two 2-digit numbers and find their product using the above technique.
Ans.
import java.util.*;
class Q52
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,i,s=0;
System.out.println(“Enter 2 numbers:”);
a=sc.nextInt();
b=sc.nextInt();
for(i=a;i>=1;i=i/2)
{
if (i%2==1)
s=s+b;
b=b*2;
}
System.out.println(“Product=”+s);
}
}

Computer Applications – IX (ICSE Course) Answers 164


53. Write a program to input a number and print all its prime factors using prime factorization.
For Example,
INPUT: Enter an integer: 24
OUTPUT: Prime Factors using Prime Factorisation are:
2
2
2
3
Ans.
import java.util.*;
class Q53
{
static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,f=2;
System.out.println(“Enter a number:”);
n=sc.nextInt();
for(i=n;i>1;)
{
if(i%f==0)
{
System.out.println(f);
i=i/f;
}
else
f++;
}
}
}

165 Computer Applications – IX (ICSE Course) Answers

You might also like