Chapter - 1.6 Repetitive Structures
Chapter - 1.6 Repetitive Structures
6 - Repetitive Structures
Practice Questions
A. Answer the following questions:
1. Explain the term loop with an example. [ICSE 2005]
Ans. Loop is a repetitive structure that allows a group of statements to be executed
repeatedly. The loop structure executes as long as the given test-expression is executed.
For example, the following for loop will execute 10 times displaying the first 10 natural
numbers.
int i;
for( i =1 ; i<=10 ; i++)
{
System.out.print(i+" ");
}
The loop starts with the initial value of 1 for ‘i' and iterates every time increasing the value
of ‘i' by 1 as long as the value of ‘i’ is less than or equal to 10. The loop body in the mean
while displays each value of ‘i’ , thus giving the output as:
1 2 3 4 5 6 7 8 9 10
6. Give an example to show how multiple initialization and updation are performed
in a for loop.
Ans. For multiple initialization and multiple updation in a for loop they are separated
by comma (,). The following example shows how multiple initialization and updation is
performed in a for loop.
9. State one similarity and one difference between while and for loops. [ICSE 2009]
Ans. Similarity: The similarity between while and for loop is that, both the loops are
entry-controlled loops..
Difference: The difference between while and for loop is that, initialization in while
loop is outside the loop structure, whereas initialization of for loop is within the loop
structure.
10. What is meant by an infinite loop? Give one example. [ICSE 2008]
Ans. Infinite loop is a loop which executes indefinitely i.e. the given test-expression is
forever evaluated to true. For example,
for(i=1;i>0;i++)
{
System.out.println(i);
}
The loop executes infinitely because the value of ‘i' is always greater than 0.
11. Give the general syntax of a while loop. How do you create infinite loops using a
while loop structure?
Ans. The general syntax of while loop is:
while (condition or test-expression)
{
//body of the loop;
}
The easiest way of creating an infinite loop using while loop is:
while(true)
{
//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 general syntax of do-while loop is:
do
{
//body of the loop;
} while (condition or test-expression)
The easiest way of creating an infinite loop using while loop is:
do
{
//body of the loop
} while(true);
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 body-of-the-loop even once (in case the test-expression is false), and the do-
while loop should be preferred when you’re sure you want to execute the body-of-the-
loop at least once.
14. What are nested loops? Give an example of a nested for loop to show its
functionality.
Ans. A loop within a loop is called a nested loop. The following example shows how
nested for loop works:
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println( );
}
The output received for the above program fragment is:
1
12
123
1234
12345
15. Name two jump statements and their use. [ICSE 2010]
Ans. The two jump statements in Java are break and continue. Whereas break
statement is used to exit from the loop or switch block, the continue statement is used to
skip the remaining statements in a loop and force the next iteration.
16. Explain the meaning of break and continue statements. [ICSE 2005, 2008]
Ans. The break statement is used to exit from the loop or switch block, the continue
statement is used to skip the remaining statements in a loop and force the next iteration.
17. Explain how break statement works in a nested loop. Also give an example to
show how continue statement works in a nested loop.
Ans. The break statement is used to exit from the nested loop in a nested loop of which it
is a content.
Example of continue statement:
for(x=1;x<=2;x++)
{
for(y=1; y<=4 ; y++)
{
if(y==3)
continue;
System.out.println(x+" \t "+y);
}
System.out.println("x="+x);
}
The output for the above code fragment would be,
1 1
1 2
1 4
x=1
2 1
2 2
2 4
x=2
As you can see whenever the value of y reaches 3 it skips the statements only within the
inner loop but the execution of the outer loop continues remaining same thus
System.out.println("x="+x); do get executed as it is actually present only within the
outer loop.
2. Analyze the following program segment and determine how many times the body
of loop will be executed (show the working). [ICSE 2009]
x=5; y=50;
while(x<=y)
{
y=y/x;
System.out.println(y);
}
Ans. Working:
x y x<=y y=y/x System.out.println(y)
5 50 true 10 10
5 10 true 2 2
5 2 false --- ---
The loop will iterate for 2 times.
3. Convert the following segment into equivalence for loop. [ICSE 2007]
int i=0;
while(i<=20)
{
System.out.print(i+" ");
i++;
}
Ans. int i;
for(i=0;i<=20;i++)
{
System.out.print(i+" ");
}
9. Write a program to find the sum of all 3-digit even natural numbers.
Ans. public class Question9
{
static void main()
{
int i,s=0;
for(i=100;i<=998;i+=2)
{
s=s+i;
}
System.out.println("Sum of all 3-digit even natural numbers="+s);
}
}
10. Write a program to find the sum of all 3 digit odd natural numbers, which are
multiples of 5.
Ans. public class Question10
{
static void main()
{
int i,s=0;
for(i=101;i<=999;i+=2)
{
if(i%5==0)
s=s+i;
}
System.out.println("Sum="+s);
}
}
11. Write a program to input an integer and find its factorial. Factorial of a number
is the product of b) Write programs to display the following series:
Ans. import java.util.*;
public class Question11
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n;
long f=1;
System.out.print("Enter an integer:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("Factorial ="+f);
}
}
12. Write a program to input an integer and print its factors.
For Example,
INPUT: INPUT:
Enter an integer: 12 Enter an integer:10
OUTPUT: OUTPUT:
Factors: 1 2 3 4 6 12 Factors: 1 2 5 10
Ans. import java.util.*;
public class Question12
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n;
System.out.print("Enter an integer:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
System.out.print(i+" ");
}
}
}
13. Write a program to input an integer and count the number of factors.
Ans. import java.util.*;
public class Question13
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0;
System.out.print("Enter an integer:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
System.out.print("Number of Factors="+c);
}
}
14. Write a program to input an integer and check whether it is a prime number or
not. (Remember, prime number has 2 factors).
Ans. import java.util.*;
public class Question14
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,c=0;
System.out.print("Enter an integer:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
System.out.print("Prime Number");
else
System.out.print("Not a Prime Number");
}
}
16. Write a program to input 10 integers and find the sum of even numbers only.
Ans. import java.util.*;
public class Question16
{
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.print("Sum of even numbers="+s);
}
}
17. 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.*;
public class Question17
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s1=0,s2=0;
System.out.println("Enter 10 integers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n>=10 && n<=99)
s1=s1+n;
else if(n>=100 && n<=999)
s2=s2+n;
}
System.out.println("Sum of 2-digit numbers="+s1);
System.out.println("Sum of 3-digit numbers="+s2);
}
}
18. Write a program to input 10 integers and display the largest integer.
Ans. import java.util.*;
public class Question18
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,l=0;
System.out.println("Enter 10 integers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
l=n;
else if(n>l)
l=n;
}
System.out.println("Largest Number="+l);
}
}
19. Write a program to input 10 integers and display the largest as well as the
smallest integer.
Ans. import java.util.*;
public class Question19
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,l=0,s=0;
System.out.println("Enter 10 integers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==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);
}
}
20. 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.*;
public class Question20
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,l=0,f=0;
System.out.println("Enter 10 integers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2==0)
{
if(f==0)
l=n;
f=1;
if(n>l)
l=n;
}
}
if(f==1)
System.out.println("Largest Even Number="+l);
else
System.out.println("No even integer found");
}
}
21. Write a program to input 10 integers and display the largest even and smallest
odd integer.
Ans. import java.util.*;
public class Question21
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,l=0,f1=0,f2=0,s=0;
System.out.println("Enter 10 integers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(n%2==0)
{
if(f1==0)
{
l=n;
f1=1;
}
if(n>l)
l=n;
}
else
{
if(f2==0)
{
s=n;
f2=1;
}
if(n<s)
s=n;
}
}
if(f1==1)
System.out.println("Largest Even Number="+l);
else if(f1==0)
System.out.println("No even integer found");
if(f2==1)
System.out.println("Smallest Odd Number="+s);
else if(f2==0)
System.out.println("No odd integer found");
}
}
22. Write a program to input 10 integers and check whether all the entered numbers
are even numbers or not.
Ans. import java.util.*;
public class Question22
{
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");
}
}
23. Write a program to input 10 integers and check whether all the entered numbers
are odd numbers or not.
Ans. import java.util.*;
public class Question23
{
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 Odd Numbers");
else
System.out.println("All are not Odd Numbers");
}
}
24. 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.*;
public class Question24
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=0,l=0;
System.out.println("Enter 10 numbers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
l=n;
if(n!=l)
f=1;
}
if(f==0)
System.out.println("All numbers are same");
else
System.out.println("All numbers are not same");
}
}
25. 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.
Ans. import java.util.*;
public class Question25
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,f=0,l=0;
System.out.println("Enter 10 numbers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
l=n;
else
{
if(n<l)
f=1;
l=n;
}
}
if(f==0)
System.out.println("All numbers are in ascending order");
else
System.out.println("All numbers are not in ascending order");
}
}
26. 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.
[ICSE-2006]
Ans. import java.util.*;
public class Question26
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s1=0,s2=0;
System.out.println("Enter a number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
s1=s1+i;
else
s2=s2+i;
}
System.out.println("Sum of even numbers="+s1);
System.out.println("Sum of odd numbers="+s2);
}
}
27. 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
v) s= 1+1/2+1/3+1/4+1/5+…+1/10
vi) s= 1/2+2/3+3/4+4/5+…+9/10
vii) s= 1-1/2+1/3-1/4+1/5-…-1/10
Ans. public class Question27
{
static void i()
{
int i,s=0;
for(i=1;i<=9;i++)
{
s=s+i*(i+1);
}
System.out.println("Sum ="+s);
}
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<=19;i+=2)
{
s=s+i-(i+1);
}
System.out.println("Sum ="+s);
}
static void v()
{
int i;
float s=0;
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<=9;i++)
{
if(i%2==0)
s=s-(float)i/(i+1);
else
s=s+(float)i/(i+1);
}
System.out.println("Sum ="+s);
}
}
28. Write a program to find the sum of 1st 10 numbers of Fibonacci series, i.e.
1,1,2,3,5, 8,13…. Lucas series is such a series which starting from 1 and 1, and
subsequent numbers are the sum of the previous two numbers.
Ans. public class Question28
{
static void main()
{
int i,a=1,b=0,c;
System.out.println("Fibonacci series =");
for(i=1;i<=10;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
29. 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 are 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. public class Question29
{
static void main()
{
int i,a=1,b=0,c;
System.out.println("Pell series =");
for(i=1;i<=15;i++)
{
c=a+2*b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
30. 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. public class Question30
{
static void main()
{
int i,a=2,b=1,c;
System.out.println("Pell series =");
System.out.print(a+" "+b+" ");
for(i=1;i<=8;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
31. 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.
Ans. import java.util.*;
public class Question31
{
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("Deficient Number");
else
System.out.println("Abundant Number");
}
}
32. 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.*;
public class Question32
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b,s1=0,s2=0;
System.out.println("Enter 2 integers:");
a=sc.nextInt();
b=sc.nextInt();
for(i=1;i<a;i++)
{
if(a%i==0)
s1+=i;
}
for(i=1;i<b;i++)
{
if(b%i==0)
s2+=i;
}
if(s1==b && s2==a)
System.out.println("Amicable Pair");
else
System.out.println("Not an amicable pair");
}
}
33. Write a program to pass an integer as argument and find the sum of its digits.
Ans. public class Question33
{
static void main(int n)
{
int d,s=0;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
System.out.println("Sum of the digits="+s);
}
}
34. Write a program to pass an integer as argument and find the sum of odd digits
and even digits separately.
Ans. public class Question34
{
static void main(int n)
{
int d,se=0,so=0;
while(n!=0)
{
d=n%10;
if(d%2==0)
se=se+d;
else
so=so+d;
n=n/10;
}
System.out.println("Sum of the even digits="+se);
System.out.println("Sum of the odd digits="+so);
}
}
35. Write a program to pass an integer as argument and find the average of its
digits.
Ans. public class Question35
{
static void main(int n)
{
int d,s=0,c=0;
float av;
while(n!=0)
{
d=n%10;
s=s+d;
c++;
n=n/10;
}
av=(float)s/c;
System.out.println("Average of its digits="+av);
}
}
36. Write a program to pass an integer as argument and print the largest as well as
smallest digit.
Ans. public class Question36
{
static void main(int n)
{
int d,s=0,l=0,f=0;
while(n!=0)
{
d=n%10;
if(f==0)
{
l=s=d;
f=1;
}
if (d>l)
l=d;
if(d<s)
s=d;
n=n/10;
}
System.out.println("Largest digit="+l);
System.out.println("Smallest digit="+s);
}
}
37. Write a program to pass an integer as argument and print the second largest
digit.
Ans. public class Question37
{
static void main(int n)
{
int d,s=0,l=0,f=0,sl=0,t=n;
while(n!=0)
{
d=n%10;
if(f==0)
{
l=d;
f=1;
}
if (d>l)
l=d;
n=n/10;
}
f=0;
while(t!=0)
{
d=t%10;
if(d!=l)
{
if(f==0)
{
sl=d;
f=1;
}
if(d>sl)
sl=d;
}
t=t/10;
}
System.out.println("Second Largest digit="+sl);
}
}
38. Write a program to input an integer and remove all the even digits from it.
Ans. import java.util.*;
public class Question38
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,d,c=0,s=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n!=0)
{
d=n%10;
if(d%2!=0)
s=s+d*(int)Math.pow(10,c++);
n=n/10;
}
System.out.println("New Number="+s);
}
}
39. 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. Same as question38.
40. Write a program to pass an integer as argument and form a new number by
having the digits arranged in ascending order.
Ans. public class Question40
{
static void main(int n)
{
int d,s=0,c=0,i,t;
for(i=9;i>=0;i--)
{
t=n;
while(t!=0)
{
d=t%10;
if(d==i)
s=s+d*(int)Math.pow(10,c++);
t=t/10;
}
}
System.out.println("Digits in ascending order="+s);
}
}
41. Write a program to input an integer and check whether all digits in it are same
or not.
Ans. import java.util.*;
public class Question41
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,n,last,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
last=n%10;
while(n!=0)
{
d=n%10;
if(d!=last)
f=1;
n=n/10;
}
if(f==0)
System.out.println("All digits are same");
else
System.out.println("All digits are not same");
}
}
42. Write a program to input an integer and check whether all digits in it are even
digits or not.
Ans. import java.util.*;
public class Question42
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,n,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n!=0)
{
d=n%10;
if(d%2!=0)
f=1;
n=n/10;
}
if(f==0)
System.out.println("All digits are even");
else
System.out.println("All digits are not even");
}
}
43. Write a program to input an integer and check whether all digits are unique or
not.
Ans. import java.util.*;
public class Question43
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d1,d2,n,t,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n!=0)
{
d1=n%10;
t=n/10;
while(t!=0)
{
d2=t%10;
if(d1==d2)
f=1;
t=t/10;
}
n=n/10;
}
if(f==0)
System.out.println("All digits are unique");
else
System.out.println("All digits are not unique");
}
}
44. Write a program to input an integer and check whether all are prime digits or
not.
Ans. import java.util.*;
public class Question44
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,n,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n!=0)
{
d=n%10;
if(d!=2 && d!=3 && d!=5 && d!=7)
f=1;
n=n/10;
}
if(f==0)
System.out.println("All digits are prime");
else
System.out.println("All digits are not prime");
}
}
45. Write a program to input an integer and check whether al digits are in
ascending order or not.
Ans. import java.util.*;
public class Question45
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d1,d2,n,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n>9)
{
d2=n%10;
d1=(n/10)%10;
if(d1>d2)
f=1;
n=n/10;
}
if(f==0)
System.out.println("All digits are in ascending order");
else
System.out.println("All digits are not in ascending order");
}
}
46. Write a program to input an integer and check whether all digits are in
desending order or not.
Ans. import java.util.*;
public class Question46
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d1,d2,n,f=0;
System.out.println("Enter an integer:");
n=sc.nextInt();
while(n>9)
{
d2=n%10;
d1=(n/10)%10;
if(d1<d2)
f=1;
n=n/10;
}
if(f==0)
System.out.println("All digits are in descending order");
else
System.out.println("All digits are not in descending order");
}
}
if((n*n)%(int)Math.pow(10,c)==n)
System.out.println("Automorphic Number");
else if((n*n*n)%(int)Math.pow(10,c)==n)
System.out.println("Trimorphic Number");
else if((3*n*n)%(int)Math.pow(10,c)==n)
System.out.println("Tri-automorphic Number");
}
}
49. Write a program to input an integer and check whether it is a happy number or
not. If you iterate the process of summing the squares of the digits of a number and
if this process terminates in 1, then the original number is called a happy number.
For example 7=> (72) =49=> (42+92) =97=>(92+72)=130 =>(12 +32+02)=10
=>(12+02)=1.
Ans. import java.util.*;
public class Question49
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,s,n;
System.out.println("Enter an integer:");
n=sc.nextInt();
do
{
s=0;
while(n!=0)
{
d=n%10;
s=s+d*d;
n=n/10;
}
n=s;
}while(n>9);
if(n==1)
System.out.println("Happy Number");
else
System.out.println("Not a Happy Number");
}
}
50. Write a program to input an integer and check whether it is a magic number or
not. If you iterate the process of summing the squares of the digits of a number and
if this process terminates in 1, then the original number is called a magic number.
For example 55=> (5+5)=10=>(1+0)=1.
Ans. import java.util.*;
public class Question50
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,s,n;
System.out.println("Enter an integer:");
n=sc.nextInt();
do
{
s=0;
while(n!=0)
{
d=n%10;
s=s+d;
n=n/10;
}
n=s;
}while(n>9);
if(n==1)
System.out.println("Magic Number");
else
System.out.println("Not a Magic Number");
}
}
51. 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.*;
public class Question51
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,s=0,n,t;
System.out.println("Enter an integer:");
n=sc.nextInt();
t=n;
while(t!=0)
{
d=t%10;
s=s+d;
t=t/10;
}
if(n%s==0)
System.out.println("Harshad Number");
else
System.out.println("Not a Harshad Number");
}
}
52. 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.*;
public class Question52
{
static void main()
{
Scanner sc=new Scanner(System.in);
int c=0,n,t,first,last;
System.out.println("Enter an integer:");
n=sc.nextInt();
t=n;
while(t!=0)
{
c++;
t=t/10;
}
first=(n*n)/(int)Math.pow(10,c);
last=(n*n)%(int)Math.pow(10,c);
if(first+last==n)
System.out.println("Kaprekar Number");
else
System.out.println("Not a Kaprekar Number");
}
}
53. Write a program to input two integers and find their Least Common Multiple
(LCM).
For Example,
INPUT:
Enter 2 integers:
12
8
OUTPUT:
LCM = 24
Ans. import java.util.*;
public class Question53
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b;
System.out.println("Enter 2 integers:");
a=sc.nextInt();
b=sc.nextInt();
for(i=a;i<=a*b;i++)
{
if(i%a==0 && i%b==0)
break;
}
System.out.println("L.C.M.="+i);
}
}
54. Write a program to input two integers and find their Highest Common
Factor(HCF).
For Example,
INPUT:
Enter 2 integers:
12
8
OUTPUT:
HCF = 4
Ans. import java.util.*;
public class Question54
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,a,b;
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)
break;
}
System.out.println("H.C.F.="+i);
}
}
56. 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.
[ICSE 2006]
Ans. import java.util.*;
public class Question56
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s1=0,s2=0;
System.out.println("Enter a number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
s1=s1+i;
else
s2=s2+i;
}
System.out.println("Sum of even numbers="+s1);
System.out.println("Sum of odd numbers="+s2);
}
}
57. Write a menu-driven class to accept a number from the user and check whether
it is a palindrome or a perfect number. [ICSE 2008]
i) Palindrome number– (a number is 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 2009]
Ans. import java.util.*;
public class Question57
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,n,ch,t,s=0,i;
System.out.println("Enter a number:");
n=sc.nextInt();
System.out.println("MENU");
System.out.println("1. to check whether it is a Palindrome Number");
System.out.println("2. to check whether it is a Perfect Number");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
t=n;
while(t!=0)
{
d=t%10;
s=s*10+d;
t=t/10;
}
if(s==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+=i;
}
if(s==n)
System.out.println("Perfect Number");
else
System.out.println("Not a Perfect Number");
break;
default:
System.out.println("Invalid Choice!");
}
}
}
58. 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. [ICSE 2009]
i) A BUZZ number is the number which either ends with 7 or 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.
Ans. import java.util.*;
public class Question58
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,n,ch,r;
System.out.println("MENU");
System.out.println("1. to check for BUZZ Number");
System.out.println("2. to find the GCD of two numbers");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter a number:");
n=sc.nextInt();
if(n%10==7 || n%7==0)
System.out.println("BUZZ Number");
else
System.out.println("Not a BUZZ Number");
break;
case 2:
System.out.println("Enter 2 numbers:");
n=sc.nextInt();
d=sc.nextInt();
do
{
r=n%d;
if(r!=0)
{
n=d;
d=r;
}
}while(r!=0);
System.out.println("GCD="+d);
break;
default:
System.out.println("Invalid Choice!");
}
}
}
59. 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 (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 two digits.
Ans. import java.util.*;
public class Question59
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,t,c=0,n,ch,r;
System.out.println("MENU");
System.out.println("1. to check for Prime Number");
System.out.println("2. to check for Automorphic Number");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println("Enter a number:");
n=sc.nextInt();
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");
break;
case 2:
System.out.println("Enter a number:");
n=sc.nextInt();
t=n;
while(t!=0)
{
c++;
t=t/10;
}
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!");
}
}
}
60. 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.*;
public class Question60
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,l=0,sl=0;
System.out.println("Enter 10 numbers:");
for(i=1;i<=10;i++)
{
n=sc.nextInt();
if(i==1)
l=n;
else
{
if(n>l)
{
sl=l;
l=n;
}
else if(sl==0)
sl=n;
else if(n>sl)
sl=n;
}
}
System.out.println("Second Largest Number="+sl);
}
}
62. Write a program to calculate and print the sum of each of the following series:
i) Sum (S) = 2 – 4 + 6 – 8 + ................ 20
ii) Sum (S) =x/2+x/5+x/8+x/11+…+x/20 where x is to be taken as input.
Ans. import java.util.*;
public class Question62
{
static void sumi()
{
int i,s=0;
for(i=2;i<=20;i+=2)
{
if(i%4==0)
s=s-i;
else
s=s+i;
}
System.out.println("Sum="+s);
}
static void sumii()
{
int i,x;
float s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of x:");
x=sc.nextInt();
for(i=2;i<=20;i+=3)
{
s=s+(float)x/i;
}
System.out.println("Sum="+s);
}
}
63. Write a program to find the sum of series, taking the value of ‘a’ and ‘n’ from
the user.
s= a/2+a/3+a/4+…+a/n
Ans. import java.util.*;
public class Question63
{
static void main()
{
int i,n,a;
float s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a and n:");
a=sc.nextInt();
n=sc.nextInt();
for(i=2;i<=n;i++)
{
s=s+(float)a/i;
}
System.out.println("Sum="+s);
}
}
64. Write a program to find the sum of series, taking the value of ‘a’ and ‘n’ from
the user.
s= a + 1/2+a + 3/4+a + 5/6+a + 7/8+... up to n terms
Ans. import java.util.*;
public class Question64
{
static void main()
{
int i,n,a,p=1;
float s=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a and n:");
a=sc.nextInt();
n=sc.nextInt();
for(i=1;i<=n;i++)
{
s=s+(float)(a+p)/(p+1);
p=p+2;
}
System.out.println("Sum="+s);
}
}
65. Write a program to find the sum of series, taking the value of ‘n’ from the user.
s= 1 + 2/2 * 3+2 + 3/3 * 4+3 + 4/4 * 5+4 + 5/5 * 6+... up to n terms
Ans. import java.util.*;
public class Question65
{
static void main()
{
int i,n;
float s=0;
Scanner sc=new Scanner(System.in);
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);
}
}
66. Write a program to compute and display the sum of the following series:
[ICSE 2007]
67. 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. [ICSE 2005]
68. Shasha Travels Pvt. Ltd. gives the following discount to its customers:
[ICSE 2010]
Write a program to input the name and ticket amount for the customer and
calculate the discount amount and net amount to be paid. Display the output in the
following format for each customer:
(Assume that there are 15 customers, first customer is given the serial no (SL. No.
1), next customer 2 ……… and so on.
69. Using nested loops try producing the following patterns on the screen:
Ans. class Question69
{
static void pattern_i()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_ii()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_iii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_iv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_v()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_vi()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
static void pattern_vii()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(i);
}
System.out.println();
}
}
static void pattern_viii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
System.out.print(i);
}
System.out.println();
}
}
static void pattern_ix()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=5;j>=i;j--)
{
System.out.print(i);
}
System.out.println();
}
}
static void pattern_x()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
static void pattern_xi()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
static void pattern_xii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i==j || i+j==6)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void pattern_xiii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((i+j)%2!=0)
System.out.print(0);
else
System.out.print(1);
}
System.out.println();
}
}
static void pattern_xiv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_xv()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i+j>=6)
System.out.print(j);
else
System.out.print(" ");
}
System.out.println();
}
}
static void pattern_xvi()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=i;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_xvii()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=1;j<=i;j++)
{
if(i==j || j==1)
System.out.print(1);
else
System.out.print(0);
}
System.out.println();
}
}
static void pattern_xviii()
{
int i,j,a,b,c;
for(i=1;i<=6;i++)
{
a=1;b=0;
for(j=1;j<=i;j++)
{
c=a+b;
System.out.print(c);
a=b;
b=c;
}
System.out.println();
}
}
static void pattern_xix()
{
int i,j;
for(i=1;i<=6;i++)
{
for(j=i;j<=5;j++)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print(j);
}
for(j=i-1;j>=1;j--)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_xx()
{
int i,j;
for(i=6;i>=1;i--)
{
for(j=i;j<6;j++)
{
System.out.print(" ");
}
for(j=i;j>=1;j--)
{
System.out.print(j);
}
for(j=2;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
static void pattern_xxi()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=i)
System.out.print(j);
else
System.out.print(" ");
}
System.out.println();
}
}
static void pattern_xxii()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if((j+i)%2==0)
System.out.print(1);
else
System.out.print(0);
}
System.out.println();
}
}
static void pattern_xxiii()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=1;j<=9;j+=2)
{
if(j>=i)
System.out.print(10-i);
else
System.out.print(" ");
}
System.out.println();
}
}
static void pattern_xxiv()
{
int i,j;
for(i=1;i<=9;i+=2)
{
for(j=1;j<=9;j+=2)
{
if(j+i>=10)
System.out.print(i);
else
System.out.print(" ");
}
System.out.println();
}
}
static void pattern_xxv()
{
/*
12345
23451
34512
45123
51234
*/
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
System.out.print(j);
}
for(j=1;j<=i-1;j++)
{
System.out.print(j);
}
System.out.println();
}
}
70. Write a program to input a number and print all its prime factors using prime
factorization method.
For Example,
INPUT: Enter an integer: 24
OUTPUT: Prime factors using prime factorisation are:
2
2
2
3
Ans. import java.util.*;
public class Question70
{
static void main()
{
int n,p=2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter an integer:");
n=sc.nextInt();
System.out.println("Prime factors using prime factorisation are:");
while(n>1)
{
if(n%p==0)
{
System.out.println(p);
n=n/p;
}
else
p++;
}
}
}
71. Write a program to input a number and check whether it is a Smith number or
not. Smith number is such a number the sum of whose digits equals the sum of the
digits of its prime factors.
Smith number is a composite number in which the sum of its digits is equal to the
sum of the digits of all its prime factors.
For example, 378 is a Smith number as the sum of the digits of 378 are: 3+7+8 = 18.
The prime factors of 378 are: 2, 3, 3, 3, 7 (sum = 2+3+3+3+7 = 18).
Similarly, 22 is a Smith number as the sum of the digits are: 2+2=4. The prime
factors of 22 are: 2 and 11 (sum = 2+(1+1) = 4
Other examples include 27, 58, 85, 94, 121, 166, 202, 265, etc.
Ans. import java.util.*;
public class Question71
{
static void main()
{
int n,d,s1=0,t,t1,s2=0,p=2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter an integer:");
n=sc.nextInt();
t=n;
while(t!=0)
{
d=t%10;
s1=s1+d;
t=t/10;
}
t=n;
while(t>1)
{
if(t%p==0)
{
t1=p;;
while(t1!=0)
{
d=t1%10;
s2=s2+d;
t1=t1/10;
}
t=t/p;
}
else
p++;
}
if(s1==s2)
System.out.println("Smith Number");
else
System.out.println("Not Smith Number");
}
}