Sie sind auf Seite 1von 91

Question:

Write a Program in Java to input a number and check whether it is a Pronic Number or
Heteromecic Number or not.
Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number,
is a number which is the product of two consecutive integers, that is, n (n + 1).
The
first
few
pronic
numbers
are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462
etc.
Programming Code:
1 /**
2 * The class HarshadNumber inputs a number and checks if it a Pronic Number
or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class PronicNumber
8 {
9
public static void main(String args[])
1
{
Scanner sc = new Scanner(System.in);
0
11
System.out.print("Enter a number : ");
1
int n = sc.nextInt();
2
int flag = 0;
1
3
for(int i=1; i<=n; i++)
1
{
if(i*(i+1) == n)
4
{
1
flag = 1;
5
break;
1
}
}
6
1
if(flag == 1)
7
System.out.println(n+" is a Pronic Number.");
1
else
8
System.out.println(n+" is not a Pronic Number.");
1
}
9 }
2
0
2

1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
Alternate Programming Code:
1 /**
2 * The class HarshadNumber inputs a number and checks if it a Pronic Number
or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class PronicNumber
8 {
9
public static void main(String args[])
1
{
Scanner sc = new Scanner(System.in);
0
11
System.out.print("Enter a number : ");
1
int n = sc.nextInt();
2
int k = (int)(Math.sqrt(n));
1
if(k*(k+1)==n)
System.out.println(n+" is a Pronic Number.");
3
else
1
System.out.println(n+" is not a Pronic Number.");
4
}
1 }
5

1
6
1
7
1
8
1
9
2
0
2
1
2
2
Output:
Enter a number : 110
110 is a Pronic Number.
Enter a number : 73
73 is not a Pronic Number.
Enter a number : 342
342 is a Pronic Number.
Enter a number : 15
15 is not a Pronic Number.

Question:
Write a Program in Java to input a number and check whether it is a Harshad Number or Niven
Number or not..
Harshad Number : In recreational mathematics, a Harshad number (or Niven number), is an
integer (in base 10) that is divisible by the sum of its digits.
Lets understand the concept of Harshad Number through the following example:

The number 18 is a Harshad number in base 10, because the sum of the digits 1 and 8 is 9
(1 + 8 = 9), and 18 is divisible by 9 (since 18 % 9 = 0)

The number 1729 is a Harshad number in base 10, because the sum of the digits 1 ,7, 2
and 9 is 19 (1 + 7 + 2 + 9 = 19), and 1729 is divisible by 19 (1729 = 19 * 91)

The number 19 is not a Harshad number in base 10, because the sum of the digits 1 and 9
is 10 (1 + 9 = 10), and 19 is not divisible by 10 (since 19 % 10 = 9)

The
first
few
Harshad
numbers
in
base
10
are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42, 45, 48, 50, 54, 60, 63, 70, 72, 80,
81, 84, 90, 100, 102, 108, 110, 111, 112, 114, 117, 120, 126, 132, 133, 135, 140, 144, 150, 152,
153, 156, 162, 171, 180, 190, 192, 195, 198, 200 etc.
Programming Code:
1 /**
class HarshadNumber inputs a number and checks if it a Harshad Number
2 *orThe
not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class HarshadNumber
8 {
9
public static void main(String args[])
1
{
Scanner sc = new Scanner(System.in);
0
11
System.out.print("Enter a number : ");
1
int n = sc.nextInt();
2
int c = n, d, sum = 0;
1
3
//finding sum of digits
1
while(c>0)
{
4
d = c%10;
1
sum = sum + d;
5
c = c/10;
1
}
6
if(n%sum == 0)
1
System.out.println(n+" is a Harshad Number.");
7
else
1
System.out.println(n+" is not a Harshad Number.");
8
}
1 }
9
2
0
2
1
2
2
2
3

2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
Output:
Enter a number : 195
195 is a Harshad Number.
Enter a number : 194
194 is not a Harshad Number.
Enter a number : 190
190 is a Harshad Number.
Enter a number : 111
111 is a Harshad Number.

Question:

Write a Program in Java to input a number and check whether it is a Fascinating Number or
not..
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property.
The property is such that, when the number is multiplied by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless
of the number of zeroes.
Lets understand the concept of Fascinating Number through the following example:
Consider
192
192
192 x 3 = 576

the
x
x

number
1
2

=
=

192,
192
384

Concatenating the results : 192384576


It could be observed that 192384576 consists of all digits from 1 to 9 exactly once. Hence, it
could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.
Programming Code:
1 /**
2 * The class FascinatingNumber inputs a number and checks if it a Fascinating
Number or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class FascinatingNumber
8 {
9
boolean isUnique(String q)
1
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit
0
from
'0'
to '9'
11
int i, flag = 0;
1
char ch;
2
for(i=0; i<q.length(); i++)
{
1
ch = q.charAt(i);
3
A[ch-48]++;
1

/* increasing A[5] if ch='5' as '5'-48 = 53-48=5


4
* (ASCII values of '0' to '9' are 48 to 57) */
1
}
5
1
for(i=1; i<10; i++)
6
{
//checking if every digit from '1' to '9' are present exactly
1
once
or
not
7
if(A[i]!=1)
1
{
8
flag = 1; //flag is set to 1 if frequency is not 1
1
break;
}
9
}
2
0
if(flag == 1)
2
return false;
1
else
return true;
2
}
2
public static void main(String args[])
2
{
3
Scanner sc = new Scanner(System.in);
2
FascinatingNumber ob = new FascinatingNumber();
4
System.out.print("Enter a number : ");
2
int n = sc.nextInt();
5
String p = Integer.toString(n); //converting the number to String
2
6
if(p.length()<3)
2
System.out.println("Number should be of atleast 3 digits.");
7
2
else
{
8
String s = Integer.toString(n*1) + Integer.toString(n*2) +
2
Integer.toString(n*3);
9
/* Joining the first, second and third multiple of the number
3
* by converting them to Strings and concatenating them*/
if(ob.isUnique(s))
0
System.out.println(n+" is a Fascinating Number.");
3
else
1
System.out.println(n+" is not a Fascinating Number.");
3
}
2
}
3 }
3
3
4
3
5
3
6
3

7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6

0
6
1
Output:
Enter a number : 273
273 is a Fascinating Number.
Enter a number : 853
853 is not a Fascinating Number.
Enter a number : 95
Number should be of atleast 3 digits.

Question:

Write a Program in Java to input a number and check whether it is a Disarium Number or not.
Note: A number will be called DISARIUM if sum of its digits powered with their respective
position is equal to the original number.
For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)
Programming Code:
1 /**
2 * The class Disarium inputs a number and checks whether
* it is a Disarium number or not
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Disarium
8
{
9
public static void main(String[] args)throws IOException
1
{
BufferedReader br=new BufferedReader (new
0
InputStreamReader(System.in));
11
System.out.print("Enter a number : ");
1
int n = Integer.parseInt(br.readLine());
2
int copy = n, d = 0, sum = 0;
String s = Integer.toString(n); //converting the number into a
1
3 String
1 no.of digitsint len = s.length(); //finding the length of the number i.e.
4
1
while(copy>0)
5
{
d = copy % 10; //extracting the last digit
1
sum = sum + (int)Math.pow(d,len);
6
len--;
1
copy = copy / 10;
7
}
1
8
if(sum == n)
System.out.println(n+" is a Disarium Number.");
1
else
9
System.out.println(n+" is not a Disarium Number.");
2
}
0
}
2

1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
Output:
Enter a Number : 135
135 is a Disarium Number.
Enter a Number : 219
219 is not a Disarium Number.
Enter a Number : 89
89 is a Disarium Number.
Question:
Write a Program in Java to input a number and check whether it is a Keith Number or not.
Note:A Keith Number is an integer N with d digits with the following property:
If a Fibonacci-like sequence (in which each term in the sequence is the sum of the d previous
terms) is formed, with the first d terms being the decimal digits of the number N, then N itself
occurs
as
a
term
in
the
sequence.

For
1,

example,
9,

197
7,

is

a
17,

Keith number since


33,
57,

it generates the
107,
197,

sequence
..

Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104, 1537
Solution:
1 /**
2 * The class Keith inputs a number and checks whether it is a Keith Number or
not
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Keith
8 {
9 public static void main(String args[])throws IOException
1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
0
System.out.print("Enter the number : "); //inputting the number
11
int n=Integer.parseInt(br.readLine());
1
2
int copy=n;
1
String s=Integer.toString(n);
int d=s.length(); //finding the number of digits (d) in the number
3
int arr[]=new int[n]; //array for storing the terms of the series
1
4
for(int i=d-1; i>=0; i--)
1
{
5
arr[i]=copy%10; //storing the digits of the number in the array
1
copy=copy/10;
6
}
1
7
int i=d,sum=0;
1
while(sum<n) //finding the sum till it is less than the number
8
{
1
sum = 0;
9
for(int j=1; j<=d; j++) //loop for generating and adding the
previous
'd' terms
2
{
0
sum=sum+arr[i-j];
2
}
1
arr[i]=sum; //storing the sum in the array
2
i++;
}
2
2
/* When the control comes out of the while loop, either the
3
sum is equal to the number or greater than it */
2
4
if(sum==n) //if sum is equal to the number, then it is a Keith number

2
5
2
6 }
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7

System.out.println("The number is a Keith Number");


else
System.out.println("The number is a not a Keith Number");

4
8
Output:
Enter the number : 197
The number is a Keith Number
Enter the number : 14
The number is a Keith Number
Enter the number : 53
The number is a not a Keith Number

Question:
Write a Program in Java to input a number in Decimal number system and convert it into its
equivalent number in the Octal number system.
Note: Octal Number system is a number system which can represent a number in any other
number system in terms of digits ranging from 0 to 7 only. This number system consists of only
eight basic digits i.e. 0, 1, 2, 3, 4, 5, 6 and 7.
For Example: 25 in the Decimal number system can be represented as 31 in the Octal number
system.

Figure Illustrating
Decimal to Octal Number System Conversion
Solution:
1 /**
2 * The class Dec2Oct inputs a Decimal number and converts it into its
equivalent Octal number
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Dec2Oct
8 {
9
public static void main(String args[])throws IOException
1
{
BufferedReader br=new BufferedReader (new
0
InputStreamReader(System.in));
11
System.out.print("Enter a decimal number : ");
1
int n=Integer.parseInt(br.readLine());
2
1
int r;
String s=""; //variable for storing the result
3
1
//array storing the digits (as characters) in the octal number
4
system
1
char dig[]={'0','1','2','3','4','5','6','7'};
5
1
while(n>0)
6
{
r=n%8; //finding remainder by dividing the number by 8
1
s=dig[r]+s; //adding the remainder to the result and
7
reversing at the same time
1
n=n/8;
8
}
1
System.out.println("Output = "+s);
}
9
2 }
0

2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
Output:
Enter a decimal number : 25
Output = 31
Enter a decimal number : 465
Output = 721
[Note: Dont forget to share these resources and links from our website in your social
networking sites with your friends and followers.]

Question:
Write a Program in Java to input a number and check whether it is a Unique Number or not.
Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For
example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.
Solution:
1 /**
2 * The class UniqueNumber inputs a number and checks whether it is a Unique
Number or not
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class UniqueNumber
8 {
9 public static void main(String args[])throws IOException
1
{
BufferedReader br=new BufferedReader(new
0
InputStreamReader(System.in));
11
System.out.print("Enter any number : ");
1
int n=Integer.parseInt(br.readLine());
2
1
String s=Integer.toString(n); //converting the number into String
3 form
int l=s.length();
1
int flag=0;
4
1
/* loop for checking whether there are repeated digits */
5
for(int i=0;i<l-1;i++)
1
{
for(int j=i+1;j<l;j++)
6
{
1
if(s.charAt(i)==s.charAt(j)) //if any digits match, then we
7 know it is not a Unique Number
1
{
8
flag=1;
break;
1
}
9
}
2
}

0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
}
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8

if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");

Output:
Enter any number : 3121
**** The Number is Not a Unique Number ****
Enter any number : 5243
**** The Number is a Unique Number ****

Question:
Write a Program in Java to input a number and check whether it is a Duck Number or not.
Note: A Duck number is a number which has zeroes present in it, but there should be no zero
present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers
whereas 08237, 04309 are not.
Solution:
1 /**
2 * The class Duck_No inputs a number and checks whether it is a Duck Number
or not

3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
*/
5
6 import java.io.*;
7 class Duck_No
8 {
public static void main(String args[])throws IOException
9
{
1
BufferedReader br=new BufferedReader(new
0 InputStreamReader(System.in));
11
System.out.print("Enter any number : ");
String n=br.readLine(); //inputting the number and storing it in a
1
String
2
1
int l=n.length(); //finding the length (number of digit) of the
3 number
1
int c=0; //variable for counting number of zero digits
char ch;
4
1
for(int i=1;i<l;i++)
5
{
1
ch=n.charAt(i); //extracting each digit and checking whether it
6 is a '0' or not
1
if(ch=='0')
c++;
7
}
1
8
char f=n.charAt(0); //taking out the first digit of the inputted
1 number
9
2
if(c>0 && f!='0')
0
System.out.println("It is a duck number");
else
2
System.out.println("It is not a duck number");
1
}
2 }
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9

3
0
3
1
3
2
3
3
3
4
Output:
Enter any number : 4013
It is a duck number
Enter any number : 0134
It is not a duck number
Enter any number : 67300987
It is a duck number
[Note: Dont forget to share these resources and links from our website in your social
networking sites with your friends and followers.]

Question:
An Emirp number is a number which is prime backwards and forwards. Example: 13 and 31 are
both
prime
numbers.
Thus,
13
is
an
Emirp
number.
Design a class Emirp to check if a given number is Emirp number or not. Some of the members
of the class are given below:
Class name : Emirp
Data
n
:
rev
:
stores
f : stores the divisor

members/instance
stores
the
reverse

the
of

the

variables:
number
number

Member
functions:
Emirp(int
nn)
:
to
assign
n
=
nn,
rev
=
0
and
f
=
2
int isprime(int x) : check if the number is prime using the recursive technique and return 1 if
prime
otherwise
return
0
void isEmirp() : reverse the given number and check if both the original number and the reverse
number are prime, by invoking the function isprime(int) and display the result with an
appropriate message.
Specify the class Emirp giving details of the constructor(int), int isprime(int) and void
isEmirp(). Define the main() function to create an object and call the methods to check for
Emirp number.
Programming Code:
1
2
3
4

/**
* The class Emirp inputs a number and checks whether it is an Emirp number
or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java

5 * @Question Year : ISC Theory 2013 Question 8


6 */
7
import java.util.*;
8 class Emirp
9 {
int n, rev, f;
1
Emirp(int nn) //parameterised constructor
0
{
11
n=nn;
1
rev=0;
2
f=2;
}
1
3
/* Note: The function isPrime(...) will check whether 'n' is prime or
1
not
4
* It will not check whether 'x' is prime or not.
1
* The variable 'x' is just a counter to increment the recursion process
5
*/
int
isprime(int x)
1
{
6
if(x<=n)
1
{
7
if(n%x!=0)
{
1
isprime(x+1);
8
}
1
}
9
if(x==n)
2
return 1;
else
0
return 0;
2
}
1
2
void isEmirp()
2
{
2
int copy=n, d;
while(copy>0) // code for reversing a number
3
{
2
d=copy%10;
4
rev=rev*10+d;
2
copy=copy/10;
}
5
2
int a=isprime(f); //checking whether the Original number 'n' is
6
Prime or not
2
n = rev; //saving reverse in 'n' so that function isprime() checks
7 it to be prime
2
f=2; //resetting the value of f for checking whether the reverse
8 number is Prime or not
int b=isprime(f); //checking whether the Reverse number is Prime or
2
not
9
3
if(a==1 && b==1) //If both Original and Reverse are Prime, then it
0 is an Emirp number

System.out.println("It is an Emirp Number");


3
else
1
System.out.println("It is Not an Emirp Number");
3
}
2
3
public static void main(String args[])throws Exception
{
3
Scanner sc = new Scanner(System.in);
3
4 number System.out.print("Enter any number : "); //inputting the original
3
int n=sc.nextInt();
5
Emirp ob=new Emirp(n);
ob.isEmirp();
3
}
6
}
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3

5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
Output:
Enter
any
13 is an Emirp Number

number

13

Enter
any
41 is Not an Emirp Number

number

41

Question:
Consider
the
sequence
of
natural
numbers.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
.
Removing
every
second
number
produces
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 .

the

sequences

Removing
every
third
number
1, 3, 7, 9, 13, 15, 19, 21, 25 .

the

sequences

produces

This process continues indefinitely by removing the fourth, fifthand so on, till after a fixed
number of steps, certain natural numbers remain indefinitely. These are known as Lucky
Numbers.
Write a program to generate and print lucky numbers less than a given number N.
SAMPLE INPUT : N = 10
OUTPUT : The Lucky numbers less than 10 are:

SAMPLE INPUT : N = 25
OUTPUT : The Lucky numbers less than 25 are:

13

19

Solution:
This is the 1st method, and it uses only one single dimensional array to generate the Lucky
Numbers.
1 /**
2 * The class LuckyNumbers_1 generates the Lucky Numbers upto a given limit
* @author : www.javaforschool.com
3 * @Program Type : BlueJ Program - Java
4 * @Question Year : ISC 2001
5 */
6
7 import java.io.*;
class LuckyNumbers_1
8 {
9 public static void main(String args[])throws IOException
10 {
11 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
12 System.out.print("Enter the Number of Elements : ");
int n=Integer.parseInt(br.readLine());
13
14 int a[]=new int[n];
15 int c=n;
16
17 for(int i=0;i<n;i++)
18 {
19 a[i]=i+1;
}
20
21 int del=1;
22 System.out.println("nLucky Number Operation :n");
23
24 while(del<n)
25 {
for(int i=del; i<n; i+=del)
26 {
27 for(int j=i; j<n-1; j++)
28 {
29 a[j]=a[j+1];
30 }
n--;
31 }
32 del++;
33
34 for(int i=0; i<n; i++)
35 {
");
36 System.out.print(a[i]+"
}
37 System.out.println();
38 } //end of while
39
40 System.out.print("nHence, the Lucky Numbers Less than "+c+" are : ");
41 for(int i=0; i<n; i++)
{

42
43
44
45
46 System.out.print(a[i]+"
47 }
48 }
49 }
50
51
52
53

");

Output:
Enter the Number of Elements : 25
Lucky Number Operation :
1
1
1
1
1

3
3
3
3
3

5
7
7
7
7

7 9 11 13 15 17 19 21 23 25
9 13 15 19 21 25
13 15 19 25
13 19 25
13 19

Hence, the Lucky Numbers Less than 25 are : 1 3 7 13 19

Question:

A happy number is a number in which the eventual sum of the square of the digits of the number
is equal to 1.
Example:

Hence, 28 is a happy number.


Example:
Hence, 12 is not a happy number.
Design a class happy to check if a given number is a happy number. Some of the members of the
class are given below:
Class name : Happy
Data members/instance variable :
n : stores the number

Member functions :
Happy(
)
:
constructor
to
assign
0
to
n
void getnum(int nn) : to assign the parameter value to the number n = nn
int sum_sq_digits(int x) : returns the sum of the square of the digits of the number x, using the
recursive
technique
void ishappy( ) : checks if the given number is a happy number by calling the function
sum_sq_digits(int) and displays an appropriate message
Specify the class Happy giving details of the constructor( ), void getnum(int), int
sum_sq_digits(int) and void ishappy( ). Also define a main( ) function to create an object and
call the methods to check for happy number.
Solution:
1 /**
2 * The class Happy checks if a given number is a happy number or not.
* @author : www.javaforschool.com
3 * @Program Type : BlueJ Program - Java
4 * @Question Year : ISC 2012 Question 10
5 */
6
7 import java.io.*;
class Happy
8 {
9 static BufferedReader br=new BufferedReader(new
1 InputStreamReader(System.in));
0 int n;
11
1 Happy()
{
2
n=0;
1
}
3
1 void getnum(int nn)
4 {
1 }n=nn;
5
1 int sum_sq_digits(int x)
6 {
if(x==0)
1
return 0;
7
else
1
{
8
int d=x%10;
1
return (d*d+ sum_sq_digits(x/10));
}
9
2 }
0
void ishappy()
2 {

1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4

int a=sum_sq_digits(n);
while(a>9)
{
a=sum_sq_digits(a);
}
if(a==1)
System.out.print(n+" is a Happy Number");
else
System.out.print(n+" is Not a Happy Number");
}
public static void main(String args[])throws IOException
{
Happy ob=new Happy();
System.out.print("Enter any number: ");
int b=Integer.parseInt(br.readLine());
ob.getnum(b);
ob.ishappy();
}
}

4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
Output:
Enter
28 is a Happy Number

any

number:

28

Enter
12 is not a Happy Number

any

number:

12

Enter
68 is a Happy Number

any

number:

68

Question:
Write a Program in Java to input a number and check whether it is a Kaprekar number or not.
Note: A positive whole number n that has d number of digits is squared and split into two
pieces, a right-hand piece that has d digits and a left-hand piece that has remaining d or d-1
digits.
If the sum of the two pieces is equal to the number, then n is a Kaprekar number. The first few
Kaprekar numbers are: 9, 45, 297 ..
Example 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Example 2: 45
452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.
Example 3: 297
2972 = 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88
Sum = 209 + 88 = 297, i.e. equal to the number. Hence, 297 is a Kaprekar number.

Solution:
1 /**
2 * The class Kaprekar inputs a number and checks whether it is a Kaprekar
Number or not
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Kaprekar
8 {
9
public static void main(String args[]) throws IOException
1
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
0
System.out.print("Enter a Number : ");
11
int n = Integer.parseInt(br.readLine()); //Inputting the number
1
2
int sq = n*n; //finding the square of the number
1
String s = Integer.toString(sq); //converting the square into a String
3
if(sq<=9)
1
s = "0"+s; //Adding a zero in the beginning if the square is of
4
single digit
1
5
int l = s.length(); //finding the length (i.e. no. of digits in the
1 square).
6
int mid = l/2; //finding the middle point
1
String left=s.substring(0,mid); //extracting the left digits from the
7
square
1
String right=s.substring(mid); //extracting the right digits from the
8 square
1
9
int x = Integer.parseInt(left); //converting the left String into
2 Integer
int y = Integer.parseInt(right); //converting the right String into
0
Integer
2
1
//if sum of left and right numbers is equal to the original number then
2 it is a Kaprekar number
2
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
2
else
3
System.out.println(n+" is Not a Kaprekar Number");
2
}
4 }
2
5
2
6
2
7

2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
Output:
Enter
a
297 is a Kaprekar Number

Question:

Number

297

Write a program to declare a square matrix A[][] of order (M x M) where M must be greater
than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the
following tasks on the matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange
them
in
the
matrix.
(b)
Calculate
the
sum
of
both
the
diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Test your program with the sample data and some random data:
Example 1
INPUT :M = 4
9
8
15
7

2
13
6
12

1
8
3
23

5
4
11
8

OUTPUT:
ORIGINAL MATRIX
9
8
15
7

2
13
6
12

1
8
3
23

5
4
11
8

REARRANGED MATRIX
9
8
15
7

2
3
8
12

1
6
13
23

5
4
11
8

DIAGONAL ELEMENTS
9
7

3
8

6
13

5
8

SUM OF THE DIAGONAL ELEMENTS = 59


Programming Code:
1

/**

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

* The class SortNonBoundary_ISC2016 inputs a square matrix and


* sorts the non-boundary elements in ascending order
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2016 Question 2
*/
import java.util.*;
class SortNonBoundary_ISC2016
{
int A[][],B[],m,n;
void input() //Function for taking all the necessary inputs
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
m=sc.nextInt();
if(m<4 || m>10)
System.out.println("Invalid Range");
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n]; //Array to store Non-Boundary Elements

System.out.println("Enter the elements of the Matrix : ");


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a value : ");
A[i][j]=sc.nextInt();
}
}

}
/* The below function stores Non-Boundary elements
* from array A[][] to array B[] if s = 1
* else stores the Non-Boundary elements in array A[][] from array B[]
*/
void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}

}
48
}
49
}
50
51
void sortArray() //Function for sorting Non-Boundary elements stored in
52 array B[]
{
53
int c = 0;
54
for(int i=0; i<n-1; i++)
55
{
56
for(int j=i+1; j<n; j++)
57
{
if(B[i]>B[j])
58
{
59
c = B[i];
60
B[i] = B[j];
61
B[j] = c;
}
62
}
63
}
64
}
65
66
void printArray() //Function for printing the array A[][]
67
{
for(int i=0;i<m;i++)
68
{
69
for(int j=0;j<m;j++)
70
{
71
System.out.print(A[i][j]+"\t");
72
}
System.out.println();
73
}
74
}
75
76
void printDiagonal() //Function for printing the diagonal elements and
77 their sum
{
78
int sum = 0;
79
for(int i=0;i<m;i++)
80
{
81
for(int j=0;j<m;j++)
82
{
if(i==j || (i+j)==m-1)
83
{
84
System.out.print(A[i][j]+"\t");
85
sum = sum + A[i][j];
86
}
else
87
System.out.print("\t");
88
}
89
System.out.println();
90
}
91
System.out.println("Sum of the Diagonal Elements : "+sum);
}
92
93
public static void main(String args[])

{
94
SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();
95
ob.input();
96
System.out.println("*********************");
97
System.out.println("The original matrix:");
System.out.println("*********************");
98
ob.printArray(); //Printing the original array
99
ob.convert(1); //Storing Non-Boundary elements to a 1-D array
10
ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal
0 Elements)
10
ob.convert(2); //Storing the sorted Non-Boundary elements back to
1 original 2-D array
10
System.out.println("*********************");
2
System.out.println("The Rearranged matrix:");
10
System.out.println("*********************");
3
ob.printArray(); //Printing the rearranged array
System.out.println("*********************");
10
System.out.println("The Diagonal Elements:");
4
System.out.println("*********************");
10
ob.printDiagonal(); //Printing the diagonal elements and their sum
5
}
10 }
6
10
7
10
8
10
9
110
111
112
113
114
115
116
117
118
119
12
0
12
1
12
2
12
3
12
4

12
5
12
6
12
7
12
8
12
9
13
0
13
1
13
2
Output:
Enter the size of the square matrix : 4
Enter the elements of the Matrix :
Enter a value : 9
Enter a value : 2
Enter a value : 1
Enter a value : 5
Enter a value : 8
Enter a value : 13
Enter a value : 8
Enter a value : 4
Enter a value : 15
Enter a value : 6
Enter a value : 3
Enter a value : 11
Enter a value : 7
Enter a value : 12
Enter a value : 23
Enter a value : 8
*********************
The original matrix:
*********************
9
2
1
5
8
13 8
4
15 6
3
11
7
12 23 8
*********************
The Rearranged matrix:
*********************
9
2
1
5
8
3
6
4
15 8
13 11
7
12 23 8
*********************

The Diagonal Elements:


*********************
9
5
3
6
8
13
7
8
Sum of the Diagonal Elements : 59

Question:
A class Admission contain the admission numbers of 100 students. Some of the data members/
member functions are given below:
Class name: Admission
Data member/instance variable:
Adno[ ]: Integer array to store admission numbers
Member functions/methods:
Admission():
constructur
to
initialize
the
array
elements
void fillArray(): to accept the element of the array in ascending order
int binSearch(int l, int u, int v): to search for a particular admission number(v) using binary
search and recursive technique and return 1 if found otherwise returns -1
Specify the class Admission giving details of the constructor, void fillArrray() and int
binSearch(int, int, int). Define the main() function to create an object and call the functions
accordingly to enable task.

Programming Code:
1 /**
2 * The class Admission searches for an Admission number using Binary Search
* @author : www.guideforschool.com
3 * @Program Type : BlueJ Program - Java
4 * @Question Year : ISC 2015 (Theory) Question 8
5 */
6
7 import java.util.*;
class Admission
8 {
9
int Adno[]=new int[100];
1
static Scanner sc = new Scanner(System.in);
0
Admission() // Default constructor
11
{
1
for(int i=0; i<100; i++)
2
{
1
Adno[i]=0;
}
3
}
1
4
void fillArray()throws Exception // Function to accept elements in
1
ascending order
5
{
1
for(int i=0; i<100; i++)
{
6
System.out.print("Enter Admission no of student "+(i+1)+": ");
1
Adno[i] = sc.nextInt();
7
}
1
8
/*Sorting the array in ascending order */
1
9
int temp=0;
for(int i=0; i<99; i++)
2
{
0
for(int j=i+1; j<100; j++)
2
{
1
if(Adno[i]>Adno[j])
2
{
temp = Adno[i];
2
Adno[i] = Adno[j];
2
Adno[j] = temp;
3
}
2
}
}
4
}
2
5
int binSearch(int l, int u, int v) // Recursive function implementing
2
binary search
6
{
2
int mid = (l + u)/2;
if(u < l) // condition if the search is unsuccessful
7
{

2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4 }
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0

return -1;
}
if(v==Adno[mid]) // condition if the search is successful
{
return 1;
}
else if(v>Adno[mid])
{
return binSearch(mid+1,u,v);
}
else
{
return binSearch(l,mid-1,v);
}
}
public static void main(String args[])throws Exception
{
Admission ob = new Admission();
System.out.println("Enter Admission number in ascending order");
ob.fillArray();
System.out.print("Enter an Admission number to search : ");
int v = sc.nextInt();
int f = ob.binSearch(0,99,v);
System.out.println("*****************************");
if(f == 1)
{
System.out.println("Admission Number found");
}
else
{
System.out.println("Admission Number Not found");
}
}

5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3

7
4
7
5
7
6
7
7
7
8
7
9
8
0
8
1
8
2
8
3
8
4
8
5
8
6
Output:
Note: The output has been taken for 5 elements in the array
Enter
Admission
Enter
Admission
Enter
Admission
Enter
Admission
Enter
Admission
Enter
Admission
Enter
an
Admission
*****************************
Admission Number found

number
no
of
no
of
no
of
no
of
no
of
number

in

ascending

student
student
student
student
student
to
search

1:
2:
3:
4:
5:
:

order
205
310
670
887
952
887

Question:
Write a program to declare a square matrix A[ ][ ] of order n. Allow the user to input positive
integers into this matrix. Perform the following tasks on the matrix:
(i)
Output
the
original
matrix.
(ii) Find the SADDLE POINT for the matrix. If the matrix has no saddle point, output the
message NO SADDLE POINT.
[Note: A saddle point is an element of the matrix such that it is the minimum element for the
row to which it belongs and the maximum element for the column to which it belongs. Saddle
point for a given matrix is always unique.]
Example:
4
7
513

In

the
5
8

Matrix
6
9

Saddle point = 7 because it is the minimum element of row 2 and maximum element of column 1
This question came in Question 3 of ISC 2003 Practical Examination.
Programming Code:
1 /**
2 * The class SaddlePoint inputs a matrix of n*n size and finds its
* saddle point if any
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 * @Question Year : ISC 2003 Question 3 (Practical)
6 */
7
import java.io.*;
8 class SaddlePoint
9 {
1
public static void main(String args[])throws IOException
{
0
BufferedReader br=new BufferedReader(new
11
InputStreamReader(System.in));
1
System.out.print("Enter the order of the matrix : ");
2
int n=Integer.parseInt(br.readLine());

1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5

int A[][]=new int[n][n];


System.out.println("Inputting the elements in the matrix");
System.out.println("******************************"); // Ignore
these. They are just for styling
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter Element at ["+i+"]["+j+"] : ");
A[i][j]=Integer.parseInt(br.readLine());
}
}
/* Printing the Original Matrix */
System.out.println("******************************");
System.out.println("The Original Matrix is");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}

int max, min, x, f=0;


for(int i=0;i<n;i++)
{
/* Finding the minimum element of a row */
min = A[i][0]; // Initializing min with first element of every
row

x = 0;
for(int j=0;j<n;j++)
{
if(A[i][j]<min)
{
min = A[i][j];
x = j; // Saving the column position of the minimum
element of the row
}
}
/* Finding the maximum element in the column
* corresponding to the minimum element of row */
max = A[0][x]; // Initializing max with first element of that
column

for(int k=0;k<n;k++)
{
if(A[k][x]>max)
{
max = A[k][x];
}
}

3
/* If the minimum of a row is same as maximum of the
6
corresponding
column,
3
then, we have that element as the Saddle point */
7
if(max==min)
3
{
System.out.println("********************");
8
System.out.println("Saddle point = "+max);
3
System.out.println("********************");
9
f=1;
4
}
0
}
4
if(f==0)
1
{
4
System.out.println("********************");
2
System.out.println("No saddle point");
4
System.out.println("********************");
3
}
}
4
}
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8

5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7
5
7
6
7
7
7
8
7
9
8
0
8
1

8
2
8
3
8
4
8
5
8
6
8
7
Output:

Question:
A class Mixer has been defined to merge two sorted integer arrays in ascending order. Some of
the members of the class are given below:
Class name

Mixer

Data members/instance variables:


int arr[]
int n

:
:

to store the elements of an array


to store the size of the array

Member functions:
Mixer( int nn)
:
constructor to assign n = nn
void accept()
:
to accept the elements of the array in ascending order
without any duplicates
Mixer mix( Mixer A)
:
to merge the current object array elements with the
parameterized array elements and return the resultant object
void display()
:
to display the elements of the array
Specify the class Mixer, giving details of the constructor(int), void accept(), Mixer
mix(Mixer) and void display(). Define the main() function to create an object and call the
function accordingly to enable the task.
Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

/**
* The class Mixer merges the array of the current object with the array of
the
* object passed as parameter
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC 2014 Question 8 (Theory)
*/
import java.io.*;
class Mixer
{
int arr[];
int n;
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Mixer(int nn)
{
n = nn;
arr = new int[n];
}
void accept()throws IOException
{
System.out.println("\n* Input the Array *\n");
for(int i=0; i<n; i++)
{
System.out.print("Enter Element ["+(i+1)+"] : ");
arr[i] = Integer.parseInt(br.readLine());
}

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

System.out.println();

Mixer mix(Mixer A)
{
int size = this.arr.length + A.arr.length; //size of resulting
array
/* 'this' keyword refers to the current object, i.e. the object
which calls mix() function */
Mixer B = new Mixer(size); //object which will store the result of
merging
int x = 0;
/* Merging the array of current object with array of parameter
object */
for(int i=0; i<size; i++)
{
if(i<A.arr.length)
B.arr[i] = A.arr[i];
else
{
B.arr[i] = this.arr[x];
x++;
}
}
/* Sorting the result*/
int temp=0;
for(int i=0; i<size-1; i++)
{
for(int j=i+1; j<size; j++)
{
if(B.arr[i]>B.arr[j])
{
temp = B.arr[i];
B.arr[i] = B.arr[j];
B.arr[j] = temp;
}
}
}
return B;
}
void display()
{
for(int i=0; i<n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[])throws IOException

73
74
75
76
77
78
79
{
80
System.out.print("Enter size of the 1st array : ");
81
int p = Integer.parseInt(br.readLine());
82
Mixer obj1 = new Mixer(p);
83
obj1.accept();
84
85
System.out.print("Enter size of the 2nd array : ");
int q = Integer.parseInt(br.readLine());
86
Mixer obj2 = new Mixer(q);
87
obj2.accept();
88
89
Mixer obj3 = obj2.mix(obj1); //obj2 is the current object which is
90 referred by 'this' keyword above
91
92
System.out.print("The 1st Array is : ");
obj1.display();
93
94
System.out.print("The 2nd Array is : ");
95
obj2.display();
96
97
System.out.print("The Merged Array is : ");
98
obj3.display();
99
}
10 }
0
10
1
10
2
10
3
Output:
Enter size of the 1st array : 5
* Input the Array *
Enter Element [1] : 11
Enter Element [2] : 13
Enter Element [3] : 17

Enter Element [4] : 24


Enter Element [5] : 33
Enter size of the 2nd array : 3
* Input the Array *
Enter Element [1] : 5
Enter Element [2] : 19
Enter Element [3] : 30
The 1st Array is : 11 13 17 24 33
The 2nd Array is : 5 19 30
The Merged Array is : 5 11 13 17 19 24 30 33

Question:

Write a Program in Java to fill a square matrix of size n*n in a spiral fashion (from the inside)
with natural numbers from 1 to n*n, taking n as input.

For example: if n = 5, then n*n = 25, hence the array will be filled as given below.

Note: Dont be confused as to how the filling will start from the center. Just treat the above
program as (see the difference in the direction of the arrows):

So, you see, this question is nothing but the same circular matrix which we did earlier (See: Java
Program to print Circular (Spiral) Matrix). The only change is that here the filling of numbers is
from n*n to 1 every time decreasing by one. In the earlier program it was from 1 to n*n every
time increasing by one.
So slight changes in our earlier program will result into this new spiral matrix. The changes will
be:

Start k from n*n and not 1

Change the condition in while loop to while(k >= 1)

Decrease the value of k every time as k- -

Solution:
1 /**
class Circular_Matrix2 creates a Square Matrix of size n*n and fills
2 *itThe
in a circular fashion
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Circular_Matrix2
8
{
9
public static void main(String args[])throws IOException
1
{
BufferedReader br=new BufferedReader(new
0
InputStreamReader(System.in));
11
System.out.print("Enter the number of elements : ");
1
int n=Integer.parseInt(br.readLine());
2
1
int A[][]=new int[n][n];
int k=n*n, c1=0, c2=n-1, r1=0, r2=n-1;
3
1
while(k>=1)
4
{
1
for(int i=c1;i<=c2;i++)
5
{
1
A[r1][i]=k--;
}
6
1
for(int j=r1+1;j<=r2;j++)
7
{
1
A[j][c2]=k--;
8
}
1
9
for(int i=c2-1;i>=c1;i--)
{
2
A[r2][i]=k--;
0
}
2
1
for(int j=r2-1;j>=r1+1;j--)
2
{
A[j][c1]=k--;
2
}
2
3
c1++;
2
c2--;
4
r1++;
2
r2--;
}
5
2
/* Printing the Circular matrix */
6
System.out.println("The Circular Matrix is:");

2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9

for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}

5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
Output:

Question:
Write a Program in Java to input a 2-D array of size m*n and print its boundary (border)
elements.
For example:

Programming Code:
1 /**
2 * The class Boundary_Element accesses and prints the boundary elements of a
2D array
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.io.*;
class Boundary_Element
8 {
9
public static void main(String args[])throws IOException
1
{
int i,j,m,n;
0
BufferedReader br=new BufferedReader(new
11
InputStreamReader(System.in));
1
2
System.out.print("Enter the no. of rows: "); //Inputting the number
1 of rows
m=Integer.parseInt(br.readLine());
3
System.out.print("Enter the no. of columns: "); //Inputting the
1
number of columns
4
n=Integer.parseInt(br.readLine());
1
5
int A[][]=new int[m][n]; //Creating the array
1
6
/* Inputting the array */
for(i=0;i<m;i++)
1
{
7
for(j=0;j<n;j++)
1
{

System.out.print("Enter the elements: ");


8
A[i][j]=Integer.parseInt(br.readLine());
1
}
9
}
2
0
System.out.println("The Boundary Elements are:");
for(i=0;i<m;i++)
2
{
1
for(j=0;j<n;j++)
2
{
2
if(i==0 || j==0 || i == m-1 || j == n-1) //condition for
2 accessing boundary elements
System.out.print(A[i][j]+"\t");
3
else
2
System.out.print(" \t");
4
}
2
System.out.println();
}
5
}
2
}
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4

1
4
2
4
3
4
4
4
5
Note: If you are asked to input a square matrix of size n*n then just input the value of n and
replace m and n in the above program with n.
Output:

Question:
Write a Program in Java to input a number and check whether it is an Evil Number or not.
Evil Number : An Evil number is a positive whole number which has even number of 1s in its
binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1s.
A few evil numbers are 3, 5, 6, 9.
Design a program to accept a positive whole number and find the binary equivalent of the
number and count the number of 1s in it and display whether it is a Evil number or not with an
appropriate message. Output the result in format given below:
Example
INPUT
BINARY
NO.
OF
OUTPUT : EVIL NUMBER

1
15
1111
4

2
26
11010
3

:
EQUIVALENT

Example
INPUT
BINARY
EQUIVALENT
NO.
OF
OUTPUT : NOT AN EVIL NUMBER

:
1s

:
:
1s

Programming Code:
1 /**
2 * The class EvilNumber inputs a number and checks whether it is an Evil
Number or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 * @ISC Computer Science Practical Specimen Paper - Question 1
6 */
import java.util.*;
7 class EvilNumber
8 {
9
String toBinary(int n) // Function to convert a number to Binary
{
1
int r;
0
String s=""; //variable for storing the result
11
1
char dig[]={'0','1'}; //array storing the digits (as characters) in
2 a binary number system
1
3
while(n>0)
{
1
r=n%2; //finding remainder by dividing the number by 2
4
s=dig[r]+s; //adding the remainder to the result and

1 reversing at the same time


n=n/2;
5
}
1
return s;
6
}
1
int countOne(String s) // Function to count no of 1's in binary number
7
{
1
int c = 0, l = s.length();
8
char ch;
1
for(int i=0; i<l; i++)
9
{
ch=s.charAt(i);
2
if(ch=='1')
0
{
2
c++;
1
}
}
2
return c;
2
}
2
3
public static void main(String args[])
2
{
4
EvilNumber ob = new EvilNumber();
Scanner sc = new Scanner(System.in);
2
5
System.out.print("Enter a positive number : ");
2
int n = sc.nextInt();
6
2
String bin = ob.toBinary(n);
7
System.out.println("Binary Equivalent = "+bin);
2
8
int x = ob.countOne(bin);
System.out.println("Number of Ones = "+x);
2
9
if(x%2==0)
3
System.out.println(n+" is an Evil Number.");
0
else
3
System.out.println(n+" is Not an Evil Number.");
1
}
3 }
2
3
3
3
4
3
5
3
6
3
7

3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0

Output:
Enter a positive number : 26
Binary Equivalent = 11010
Number of Ones = 3
26 is Not an Evil Number.
Enter a positive number : 420
Binary Equivalent = 110100100
Number of Ones = 4
420 is an Evil Number.
Enter a positive number : 659
Binary Equivalent = 1010010011
Number of Ones = 5
659 is Not an Evil Number.

Question:
A super class Record has been defined to store the names and ranks of 50 students. Define a sub
class Rank to find the highest rank along with the name. The details of both classes are given
below:
Class name : Record
Data
Members
name[
]
:
to
rnk[ ] : to store the ranks of students

/
store

the

instance
names

Member
Record()
:
constructor
to
initialize
void
readvalues()
:
to
store
names
void display() : displays the names and the corresponding ranks

of

data
and

variables:
students
functions:
members
ranks

Class name : Rank


Data
Members
/
index : integer to store the index of the topmost rank

instance

variables:

Member
functions
Rank() : constructor to invoke the base class constructor and to initialize index to 0.
void highest() : finds the index location of the topmost rank and stores it in index without
sorting
the
array
6
void display() : displays the name and ranks along with the name having the topmost rank.
Specify the class Record giving details of the constructor(), void readvalues(), void display().
Using the concept of inheritance, specify the class Rank giving details of constructor(), void
highest() and void display().
The main function and algorithm need not be written.
[Note: We will be writing the main() function also in this program so as to familiarize the
students on how to run programs based on the concept of inheritance.]
Programming Code:
1
2
3

/**
* The superclass Record stores the stores the name and ranks of 50 students
and
* the subclass Rank finds the highest rank along with the name

4 * @author : www.javaforschool.com
5 * @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2011 Question 11
6 */
7
8 import java.io.*;
9 class Record //superclass
{
1
BufferedReader br=new BufferedReader(new
0 InputStreamReader(System.in));
11
String name[];
1
int rnk[];
2
Record()
1
{
3
name = new String[50];
1
rnk = new int[50];
4
}
1
5
void readvalues()throws IOException
{
1
System.out.println("*** Inputting The Names And Ranks ***");
6
for(int i=0;i<50;i++)
1
{
7
System.out.print("Enter name of student "+(i+1)+" : ");
name[i]=br.readLine();
1
System.out.print("Enter his rank : ");
8
rnk[i]=Integer.parseInt(br.readLine());
1
}
9
}
2
0
void display()
{
2
System.out.println("NamettRank");
1
System.out.println("-------tt-------"); //this is just for
2 styling the output. You can skip it !
2
for(int i=0;i<50;i++)
2
{
System.out.println(name[i]+"tt"+rnk[i]);
3
}
2
}
4
} //end of superclass Record
2
5 class Rank extends Record //subclass
{
2
int index;
6
2
Rank()
7
{
2
super(); //invoking the constructor of superclass
8
index = 0;
}
2
9
void highest()
3
{

0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5

int min = rnk[0];


for(int i=0;i<50;i++)
{
if(rnk[i]<min)
{
min = rnk[i];
index = i;
}
}

void display()
{
super.display(); //calling the superclass function display()
highest(); //calling the function highest() for finding index of
topmost rank
System.out.println("nTop most rank = "+rnk[index]);
System.out.println("Student with topmost rank = "+name[index]);
}
} //end of subclass Rank
/* In your exams you don't need to write the below given code
We are writing it so as to familiarize the students on how to run
programs
based on the concept of inheritance.*/
public class Question11_ISC2011 //Class which will contain the main() method
and execute the program
{
public static void main(String args[])throws IOException
{
Rank ob=new Rank(); //creating object of subclass
ob.readvalues(); //calling radvalues() function of superclass to
input the names and ranks
System.out.println("*** Output ***");
ob.display(); //calling display() function of subclass
}
}

3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7
5
7

6
7
7
7
8
7
9
8
0
8
1
8
2
8
3
8
4
8
5
8
6
8
7
8
8
8
9
9
0
Output:
[Note: We have shown output of the same program with only 5 students and not 50. But the
above program works likewise for 50 students]
*** Inputting The Names And Ranks ***
Enter name of student 1 : Aamir
Enter his rank : 5
Enter name of student 2 : Zakir
Enter his rank : 2
Enter name of student 3 : Saalim
Enter his rank : 7
Enter name of student 4 : Samir
Enter his rank : 3
Enter name of student 5 : Saif
Enter his rank : 6

*** Output ***


Name
Rank

Aamir
5
Zakir
2
Saalim
7
Samir
3
Saif
6
Top most rank = 2
Student with topmost rank = Zakir

Question:
A super class Detail has been defined to store the details of a customer. Define a subclass Bill to
compute the monthly telephone charge of the customer as per the chart given below:
Number Of Calls
1 100
101 200

201 300
Above 300
Rate
Only Rental charge
60 paisa per call + rental charge
80 paisa per call + rental charge
1 rupee per call + rental charge
The details of both the classes are given below:
Class Name : Detail
Data members / Instance variables:
name : to store the name of the customer.
address : to store the address of the customer.
telno : to store the phone number of the customer.
rent : to store the monthly rental charge
Member functions:
Detail() : parameterized constructor to assign values to data members.
void show() : to display the detail of the customer.
Class Name : Bill
Data members / Instance variables:
n : to store the number of calls.
amt : to store the amount to be paid by the customer.
Member functions:
Bill() : parameterized constructor to assign values to data members of both classes and to
initialize amt = 0.0.
void cal() : calculates the monthly telephone charge as per the charge given above.
void show() : to display the detail of the customer and amount to be paid.
Specify the class Detail giving details of the constructor( ) and void show(). Using the concept
of inheritance, specify the class Bill giving details of the constructor( ), void cal() and void
show().
The main function and algorithm need not be written.
[Note: We will be writing the main() function also in this program so as to familiarize the
students on how to run programs based on the concept of inheritance.]
Programming Code:
1 /**

* The superclass Detail stores the details of a customer and the subclass

2 Bill calculates the telephone bill


3 * @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
4 * @Question Year : ISC Theory 2012 Question 12
5 */
6
7 import java.io.*;
8 class Detail //superclass
{
9
String name, address;
1
long telno;
0
double rent;
11
Detail(String n1, String a1, long t1, double r1)
1
{
2
name = n1;
1
address = a1;
3
telno = t1;
rent = r1;
1
}
4
1
void show()
5
{
1
System.out.println("Name of customer = "+name);
6
System.out.println("Address = "+address);
System.out.println("Telephone Number = "+telno);
1
System.out.println("Monthly Rental = Rs. "+rent);
7
}
1
} //end of superclass Detail
8
1 class Bill extends Detail //subclass
9
{
int n;
2
double amt;
0
2
Bill(String n1, String a1, long t1, double r1, int c)
1
{
2
super(n1,a1,t1,r1); //initializing data members of superclass by
2 calling its constructor
n = c;
2
amt = 0.0;
3
}
2
4
void cal()
2
{
if(n>=1 && n<=100)
5
amt = rent;
2
else if(n>=101 && n<=200)
6
amt = 0.6*n + rent;
2
else if(n>=201 && n<=300)
7
amt = 0.8*n + rent;
else
2
amt = 1*n + rent;
8
}
2

9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5

void show()
{
super.show(); //calling the superclass function show()
System.out.println("No. of calls = "+n);
System.out.println("Amount to be paid = Rs. "+amt);
}
} //end of subclass Bill
/* In your exams you don't need to write the below given code
We are writing it so as to familiarize the students on how to run
programs based on the concept of inheritance.*/
public class Question12_ISC2012 //Class which will contain the main() method
and execute the program
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the name : ");
String n1=br.readLine();
System.out.print("Enter the address : ");
String a1=br.readLine();
System.out.print("Enter the telephone number : ");
long t1=Long.parseLong(br.readLine());
System.out.print("Enter the monthly rental : ");
double r1=Double.parseDouble(br.readLine());
System.out.print("Enter the number of calls : ");
int c=Integer.parseInt(br.readLine());

}
}

Bill ob=new Bill(n1,a1,t1,r1,c); //creating object of subclass


System.out.println("*** Output ***");
ob.cal();
ob.show(); //calling show() function of subclass

2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7

5
7
6
7
7
7
8
7
9
8
0
8
1
8
2
8
3
8
4
8
5
8
6
8
7
8
8
8
9
Output:
Enter the name : Java For School
Enter the address : 123, Sample Street
Enter the telephone number : 1234567890
Enter the monthly rental : 180
Enter the number of calls : 270
*** Output ***
Name of customer = Java For School
Address = 123, Sample Street
Telephone Number = 1234567890
Monthly Rental = Rs. 180.0
No. of calls = 270
Amount to be paid = Rs. 396.0

Question:
A super class Perimeter has been defined to calculate the perimeter of a parallelogram. Define a
subclass Area to compute the area of the parallelogram by using the required data members of
the super class. The details are given below:
Class name : Perimeter
Data members/instance variables:
a : to store the length in decimal
b : to store the breadth in decimal
Member functions:
Perimeter( ) : parameterized constructor to assign values to data members
double Calculate( ) : calculate and return the perimeter of a parallelogram as 2 * (length +
breadth)
void show() : to display the data members along with the perimeter of the parallelogram
Class name : Area

Data
members/instance
h:
to
store
the
area : to store the area of the parallelogram

height

in

variables:
decimal

Member functions:
Area( ) : parameterized constructor to assign values to data members of both the classes
void doarea( ) : compute the area as (breadth * height)
void show() : display the data members of both classes along with the area and perimeter of the
parallelogram.
Specify the class Perimeter giving details of the constructor( ), void Calculate( ) and void
show( ). Using the concept of inheritance, specify the class Area giving details of
the constructor( ), void doarea( ) and void show( ).
The main function and algorithm need not be written.
[Note: We will be writing the main() function also in this program so as to familiarize the
students on how to run programs based on the concept of inheritance.]
Programming Code:
1 /**
2 * The superclass Perimeter calculate the perimeter of a parallelogram and
* the subclass Area calculates the area of a parallelogram
3 * @author : www.javaforschool.com
4 * @Program Type : BlueJ Program - Java
5 * @Question Year : ISC Theory 2013 Question 11
6 */
7
import java.io.*;
8 class Perimeter //superclass
9
{
1
double a,b;
Perimeter(double x, double y)
0
{
11
a=x;
1
b=y;
2
}
double Calculate()
1
{
3
double p=2*(a+b);
1
return p;
4
}
1
void show()
{
5
System.out.println("Length = "+a);
1
System.out.println("Breadth = "+b);
6
System.out.println("Perimeter = "+Calculate()); //printing
1 perimeter by calling Calculate()
7
}
}
//end
of superclass Perimeter
1

8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4

class Area extends Perimeter //subclass


{
double h, area;
Area(double x, double y, double z)
{
super(x,y); //initializing data members of superclass by calling
its constructor
h=z;
}
void doarea()
{
area=b*h;
}
void show()
{
super.show(); //calling the superclass function show()
System.out.println("Height = "+h);
System.out.println("Area = "+area);
}
} //end of subclass Area
/* In your exams you don't need to write the below given code
We are writing it so as to familiarize the students on how to run
programs based on the concept of inheritance.*/
public class Question11_ISC2013 //Class which will contain the main() method
and execute the program
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter The Length : ");
double x=Double.parseDouble(br.readLine());
System.out.print("Enter The Breadth : ");
double y=Double.parseDouble(br.readLine());
System.out.print("Enter The Height : ");
double z=Double.parseDouble(br.readLine());
Area ob=new Area(x,y,z); //creating object of subclass
System.out.println("*** Output ***");
ob.doarea();
ob.show(); //calling show() function of subclass
}

1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6

4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
Output:
Enter The Length : 5
Enter The Breadth : 7
Enter The Height : 9
*** Output ***
Length = 5.0
Breadth = 7.0
Perimeter = 24.0
Height = 9.0
Area = 63.0

Question:
Write a Program in Java to input a number and check whether it is a Fascinating Number or
not..

Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property.
The property is such that, when the number is multiplied by 2 and 3, and both these products are
concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless
of the number of zeroes.
Lets understand the concept of Fascinating Number through the following example:
Consider
192
192
192 x 3 = 576

the
x
x

number
1
2

=
=

192,
192
384

Concatenating the results : 192384576


It could be observed that 192384576 consists of all digits from 1 to 9 exactly once. Hence, it
could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.
Programming Code:
1 /**
2 * The class FascinatingNumber inputs a number and checks if it a Fascinating
Number or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class FascinatingNumber
8 {
9
boolean isUnique(String q)
1
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit
0
from
'0'
to '9'
11
int i, flag = 0;
1
char ch;
2
for(i=0; i<q.length(); i++)
{
1
ch = q.charAt(i);
3
A[ch-48]++;
1
/* increasing A[5] if ch='5' as '5'-48 = 53-48=5
4
* (ASCII values of '0' to '9' are 48 to 57) */
1
}
5
for(i=1; i<10; i++)
1
{
6
//checking if every digit from '1' to '9' are present exactly
1 once or not
7
if(A[i]!=1)
1
{
flag = 1; //flag is set to 1 if frequency is not 1

break;
8
}
1
}
9
2
if(flag == 1)
0
return false;
else
2
return true;
1
}
2
public static void main(String args[])
2
{
2
Scanner sc = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
3
2
System.out.print("Enter a number : ");
4
int n = sc.nextInt();
2
String p = Integer.toString(n); //converting the number to String
5
2
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
6
2
else
7
{
2
String s = Integer.toString(n*1) + Integer.toString(n*2) +
8 Integer.toString(n*3);
2
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
9
if(ob.isUnique(s))
3
System.out.println(n+" is a Fascinating Number.");
0
else
3
System.out.println(n+" is not a Fascinating Number.");
1
}
}
3
}
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4

1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
Output:
Enter a number : 273
273 is a Fascinating Number.

Enter a number : 853


853 is not a Fascinating Number.
Enter a number : 95
Number should be of atleast 3 digits.

Question:

Write a Program in Java to input a 2-D square matrix and check whether it is a Lower Triangular
Matrix or not.
Lower Triangular Matrix : A Lower Triangular matrix is a square matrix in which all the
entries above the main diagonal (
) are zero. The entries below or on the main diagonal

themselves may or may not be zero.


Example:

Solution:
1 /**
The class LowerTriangularMatrix inputs a Matrix and checks whether it is a
2 *Lower
Triangular Matrix or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 */
6
7 import java.util.*;
class LowerTriangularMatrix
8 {
9
public static void main(String args[])throws Exception
1
{
Scanner sc=new Scanner(System.in);
0
System.out.print("Enter the size of the matrix : ");
11
int m=sc.nextInt();
1
int A[][]=new int[m][m];
2
1
/* Inputting the matrix */
for(int i=0;i<m;i++)
3
{
1
for(int j=0;j<m;j++)
4
{
1
System.out.print("Enter an element : ");
5
A[i][j]=sc.nextInt();
}
1
}
6
1
/* Printing the matrix */
7
System.out.println("*************************");
1
System.out.println("The Matrix is : ");
8
for(int i=0;i<m;i++)
{
1
for(int j=0;j<m;j++)

9
2
0
2
1
2
2
2
3
2
4
2
5
zero
2
6
2
7
2
8
2
9
3
0
}
3
}
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4

System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");
int p=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
/* Checking that the matrix is Lower Triangular or not */
if(A[i][j]!=0) // All elements above the diagonal must be
{
}
}

p=1;
break;

if(p==0)
System.out.println("The matrix is Lower Triangular");
else
System.out.println("The matrix is not Lower Triangular");

2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
Output:
Enter the size of the matrix : 4
Enter an element : 5
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 3

Enter an element : 1
Enter an element : 0
Enter an element : 0
Enter an element : 4
Enter an element : 9
Enter an element : 4
Enter an element : 0
Enter an element : 6
Enter an element : 8
Enter an element : 7
Enter an element : 2
*************************
The Matrix is :
5000
3100
4940
6872
*************************
The matrix is Lower Triangular

Das könnte Ihnen auch gefallen