Sachin Java
Sachin Java
Sachin Java
Given a square matrix M [ ] [ ] of order n. The maximum value possible for n is 10. Accept
three different characters from the keyboard and fill the array according to the instruction given
below.
Fill the upper and lower elements formed by the intersection of the diagonals by character 1.
Fill the left and right elements formed by the intersection of the diagonals by character 2.
Fill both the diagonals by character 3.
Example 1
ENTER SIZE : 4
INPUT : FIRST CHARACTER : *
SECOND CHARACTER : ?
THIRD CHARACTER : #
OUTPUT :
# * * #
? # # ?
? # # ?
# * * #
Example 2
ENTER SIZE : 5
INPUT : FIRST CHARACTER : $
SECOND CHARACTER : !
THIRD CHARACTER : @
OUTPUT :
@ $ $ $ @
! @ $ @ !
! ! @ ! !
! @ $ @ !
@ $ $ $ @
Example 3
ENTER SIZE : 65
OUTPUT : SIZE OUT OF RANGE
/**
* The class MatrixFill creates a matrix using 3 characters taken as inputs
* Upper and lower elements formed by the intersection of the diagonals are
filled by character 1.
* Left and right elements formed by the intersection of the diagonals are
filled by character 2.
* Both the diagonals are filled by character 3.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 2
*/
import java.util.*;
class MatrixFill
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of the matrix : ");
int n = sc.nextInt();
if(n<2 || n>10)
System.out.println("Size out of Range");
else
{
char A[][]=new char[n][n];
System.out.print("Enter the 1st character : ");
char c1 = sc.next().charAt(0);
System.out.print("Enter the 2nd character : ");
char c2 = sc.next().charAt(0);
System.out.print("Enter the 3rd character : ");
char c3 = sc.next().charAt(0);
Output:
Output :
% @ @ @ @ @ %
# % @ @ @ % #
# # % @ % # #
# # # % # # #
# # % @ % # #
# % @ @ @ % #
% @ @ @ @ @ %
Question: 2
The encryption of alphabets are to be done as follows:
A=1
B=2
C=3
.
.
.
Z = 26
The potential of a word is found by adding the encrypted value of the alphabets.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45
Example 1
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
Example 2
POTENTIAL : LOOK = 53
BEFORE = 51
YOU = 61
LEAP = 34
import java.util.*;
class WordPotential
{
int findPotential(String s) // Function to find potential of a word
{
s = s.toUpperCase();
int p = 0, l = s.length();
char ch;
for(int i=0; i<l; i++)
{
ch = s.charAt(i);
p = p + (ch-64); // if ch = 'A', then 'A'-64 = ASCII value of 'A'
- 64 = 65-64 = 1
}
return p;
}
printResult(w,p);
}
ob.sortPotential(words,potential);
}
}
Output:
Potential : Look = 53
before = 51
you = 61
leap = 34
Potential : The = 33
sky = 55
is = 28
the = 33
limit = 63
Evil Number : An Evil number is a positive whole number which has even number of 1s in its
binary equivalent.
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 1
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1s : 4
OUTPUT : EVIL NUMBER
Example 2
INPUT : 26
BINARY EQUIVALENT : 11010
NO. OF 1s : 3
OUTPUT : NOT AN EVIL NUMBER
/**
* The class EvilNumber inputs a number and checks whether it is an Evil
Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @ISC Computer Science Practical Specimen Paper - Question 1
*/
import java.util.*;
class EvilNumber
{
String toBinary(int n) // Function to convert a number to Binary
{
int r;
String s=""; //variable for storing the result
while(n>0)
{
r=n%2; //finding remainder by dividing the number by 2
s=dig[r]+s; //adding the remainder to the result and
reversing at the same time
n=n/2;
}
return s;
}
int x = ob.countOne(bin);
System.out.println("Number of Ones = "+x);
if(x%2==0)
System.out.println(n+" is an Evil Number.");
else
System.out.println(n+" is Not an Evil Number.");
}
}
Output:
Question:
M(N) = 1 if N = 1 [Condition 1]
Example :
Member functions:
MobiusFn() : default constructor
void input() : input value of n
int primeFac() : to check and count prime factors of n
void display() : to find and print values of Mobius function
Specify the class MobiusFn giving details of the constructors, void input(), int primeFac(),
and void display(). Also define the main function to create an object and call methods
accordingly to enable the task.
Programming Code:
/**
* The class MobiusFn inputs a number and calculates the value of Mobius
Function
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 1999
*/
import java.util.*;
class MobiusFn
{
int n;
MobiusFn()
{
n = 0;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n = sc.nextInt();
}
Output:
Enter a number : 78
Value of Mobius Function : -1
Enter a number : 12
Value of Mobius Function : 0
Enter a number : 34
Value of Mobius Function : 1
Enter a number : 17
Value of Mobius Function : -1
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 class HarshadNumber inputs a number and checks if it a Pronic Number or
not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}
Output:
Enter a number : 73
73 is not 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..
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 class HarshadNumber inputs a number and checks if it a Harshad Number
or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class HarshadNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
if(n%sum == 0)
System.out.println(n+" is a Harshad Number.");
else
System.out.println(n+" is not a Harshad Number.");
}
}
Output:
Question:
Note: Anagrams are words made up of all the characters present in the original word by re-
arranging the characters.
Example: Anagrams of the word TOP are: TOP, TPO, OPT, OTP, PTO and POT
/**
* The class Anagrams inputs a word and generates its anagrams
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class Anagrams
{
int c = 0;
Output:
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:
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.
/**
* The class FascinatingNumber inputs a number and checks if it a Fascinating
Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class FascinatingNumber
{
boolean isUnique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every digit
from '0' to '9'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
/* increasing A[5] if ch='5' as '5'-48 = 53-48=5
* (ASCII values of '0' to '9' are 48 to 57) */
}
if(flag == 1)
return false;
else
return true;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
FascinatingNumber ob = new FascinatingNumber();
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{
String s = Integer.toString(n*1) + Integer.toString(n*2) +
Integer.toString(n*3);
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
if(ob.isUnique(s))
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is not a Fascinating Number.");
}
}
}
Output:
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.
Some keith numbers are: 14 ,19, 28 , 47 , 61, 75, 197, 742, 1104, 1537
/**
* The class Keith inputs a number and checks whether it is a Keith Number or
not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class Keith
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number : "); //inputting the number
int n=Integer.parseInt(br.readLine());
int copy=n;
String s=Integer.toString(n);
int d=s.length(); //finding the number of digits (d) in the number
int arr[]=new int[n]; //array for storing the terms of the series
int i=d,sum=0;
while(sum<n) //finding the sum till it is less than the number
{
sum = 0;
for(int j=1; j<=d; j++) //loop for generating and adding the previous
'd' terms
{
sum=sum+arr[i-j];
}
arr[i]=sum; //storing the sum in the array
i++;
}
/* When the control comes out of the while loop, either the
sum is equal to the number or greater than it */
Output: