Computer Project
Question 1
Write a menu driven program using switch case construct that converts a decimal number into its
equivalent Binary, Octal, Hexadecimal.
Assume an integer input.
Code:
/**
* Menu driven program to convert a decimal number to
* its binary, octal, or hexadecimal equivalents
* based on the user's input
*/
import java.util.*; //imports all classes of the utility package
class Convert //class declaration
int rem;
void binary(int n) //conversion to binary
String bno="";
char bchars[]={'0','1'}; //stores binary digits
while(n>0)
rem=n%2; //stores remainder
bno=bchars[rem]+bno;
n=n/2;
}
System.out.println("Binary equivalent = "+bno);
} //end of method
void octal(int n) //conversion to octal
String ono = "";
char ochars[]={'0','1','2','3','4','5','6','7'}; //stores octal digits
while(n>0)
rem=n%8; //stores remainder
ono=ochars[rem]+ono;
n=n/8;
System.out.println("Octal equivalent = "+ono);
} //end of method
void hex(int n) //conversion to hexadecimal
String hno = "";
char hchars[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //stores hexadecimal digits
while(n>0)
rem=n%16; //stores remainder
hno=hchars[rem]+hno;
n=n/16;
System.out.println("Hexadecimal equivalent = "+hno);
} //end of method
public static void main(String args[])
{
Scanner hq = new Scanner(System.in);
System.out.println("Enter your choice:");
System.out.println("Press 1 for Decimal To Binary");
System.out.println("Press 2 for Decimal To Octal");
System.out.println("Press 3 for Decimal To Hexadecimal");
int ch = hq.nextInt();
System.out.println("Enter the number:");
int n = hq.nextInt();
Convert tpn = new Convert(); //object creation
switch(ch)
case 1:
tpn.binary(n); //calls binary(int n)
break;
case 2:
tpn.octal(n); //calls octal(int n)
break;
case 3:
tpn.hex(n); //calls hex(int n)
break;
default:
System.out.println("Invalid input.");
} //end of switch
} //end of main
} //end of class
Output:
Question 2
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 1’s in its binary
equivalent.
Code:
/**
* Program to check if a number is Evil or not
*/
import java.util.*; //imports all classes of the utility package
class Evil //class declaration
String toBinary(int n) //conversion to binary
String bin="";
int rem;
char bchars[]={'0','1'}; //stores binary digits
while(n>0)
rem=n%2; //stores remainder
bin=bchars[rem]+bin;
n=n/2;
return bin;
} //end of method
/**
* method to count the number of 1's present in the
* binary equivalent of the number
*/
int count(String bin)
int c=0, l=bin.length();
char ch;
for(int i=0; i<l; i++)
ch=bin.charAt(i);
if(ch=='1')
c++; //counts the 1's
} //end of for loop
return c;
} //end of method
public static void main(String args[])
Evil tpn = new Evil(); //object creation
Scanner hq = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = hq.nextInt();
String bin = tpn.toBinary(n); //calls toBinary(int n)
System.out.println("Binary Equivalent: "+bin);
int x = tpn.count(bin); //calls count(String bin)
System.out.println("Number of 1's: "+x);
if(x%2==0) //checks for even no. of 1's
{
System.out.println("It is an Evil Number.");
else
System.out.println("It is not an Evil Number.");
} //end of main
} //end of class
Output:
Question 3
The encryption of alphabets is 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
Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is
separated by single space. Decode the words according to their potential and arrange them in
ascending order.
Output the result in format given below:
Example 1
INPUT : THE SKY IS THE LIMIT.
POTENTIAL : THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
OUTPUT : IS THE THE SKY LIMIT
Code:
/**
* Program to find the potential of words in a sentence
* and arrange the words according to its potential
* in ascending order
*/
import java.util.*; //imports all classes of the utility package
class Potential //class declaration
int findPotential(String s) // Method 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);
/**
* e.g. : ch value of 'A' = 65
* 'A' - 64 = 1 (ASCII value of A)
*/
return p;
} //end of method
void printResult(String w[], int p[]) // Method to print the final result
int n = w.length;
String ans = "";
for(int i=0; i<n; i++)
{
ans = ans + " " + w[i];
ans = ans.trim();
System.out.println("\nOutput\t\t : \t"+ans);
} //end of method
void sortAscending(String w[], int p[]) //Method to sort the words in ascending order of their
potential
int n = w.length, t1 = 0;
String t2 = "";
for(int i=0; i<n-1; i++) //Bubble sort
for(int j=i+1; j<n; j++)
if(p[i]>p[j])
t1 = p[i];
p[i] = p[j];
p[j] = t1;
t2 = w[i];
w[i] = w[j];
w[j] = t2;
printResult(w,p);
} //end of method
public static void main(String args[])
Potential tpn = new Potential(); //object creation
Scanner hq = new Scanner(System.in);
System.out.print("Enter a sentence : \t");
String s = hq.nextLine();
StringTokenizer fma = new StringTokenizer(s," .,?!"); //store words as a token
int n = fma.countTokens();
String words[] = new String[n];
int potential[] = new int[n];
for(int i=0; i<n; i++)
words[i] = fma.nextToken(); // Saving words in an array
potential[i] = tpn.findPotential(words[i]); // Saving potential of every word
System.out.print("\nPotential\t : \t"); //Printing potential of words
for(int i=0; i<n; i++)
System.out.println(words[i]+"\t= "+potential[i]);
System.out.print("\t\t\t");
tpn.sortAscending(words,potential); //calls sortAscending(String w[], int p[])
} //end of main
} //end of class
Output:
Question 4
Write a menu driven program using switch case construct to input an m X n matrix and Display it.
Perform the following operations on user’s choice:
Display the largest element of each row
Display the smallest element of each row
Display the largest element of each column
Display the smallest element of each column
Code:
/**
* Menu driven program to input a m x n matrix and display
* the largest or smallest element of each row or column
* based on the user's preference
*/
import java.util.*; //imports all classes of the utility package
class SwitchMatrix //class declaration
int arr[][], m, n;
SwitchMatrix(int x, int y) //Parameterized constructor to initialise data members
m=x;
n=y;
arr= new int[m][n];
void input() //method to input array elements
Scanner hq = new Scanner(System.in);
System.out.println("Enter the elements of the array:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
} //end of method
void display() //method to display the matrix
System.out.println("The elements of the array are:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
} //end of method
void rowlarge() //method to find the largest element in a row
int l = 0;
for (int i=0;i<m;i++)
l=arr[i][0];
for(int j=0;j<n;j++)
{
if(arr[i][j]>l)
l=arr[i][j];
System.out.println("The largest element is: "+l);
} //end of method
void columnlarge() //method to find the largest element in a column
int l=0;
for (int i=0;i<m;i++)
l=arr[0][i];
for(int j=0;j<n;j++)
if(arr[j][i]>l)
l=arr[j][i];
System.out.println("The largest element is: "+l);
} //end of method
void rowsmall() //method to find the smallest element in a row
int l = 0;
for (int i=0;i<m;i++)
l=arr[i][0];
for(int j=0;j<n;j++)
if(arr[i][j]<l)
l=arr[i][j];
System.out.println("The smallest element is: "+l);
} //end of method
void columnsmall() //method to find the smallest element in a column
int l = 0;
for (int i=0;i<m;i++)
l=arr[0][i];
for(int j=0;j<n;j++)
if(arr[j][i]<l)
l=arr[j][i];
System.out.println("The smallest element is: "+l);
} //end of method
public static void main(String args[])
Scanner hq = new Scanner (System.in);
System.out.println("Enter the number of columns and rows of the array:");
int a = hq.nextInt();
int b = hq.nextInt();
SwitchMatrix tpn = new SwitchMatrix(a,b);
tpn.input();
tpn.display();
System.out.println("Enter a choice:");
System.out.println("1.Display the largest element of each row.");
System.out.println("2.Display the smallest element of each row.");
System.out.println("3.Display the largest element of each column.");
System.out.println("4.Display the smallest element of each column.");
int ch = hq.nextInt();
switch(ch)
case 1:
tpn.rowlarge();
break;
case 2:
tpn.rowsmall();
break;
case 3:
tpn.columnlarge();
break;
case 4:
tpn.columnsmall();
break;
default:
System.out.println("Invalid input.");
} //end of switch
} //end of main
} //end of class
Output:
Question 5:
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.
Code:
/**
* Program to find whether a number is a Unique number
*/
import java.io.*; //imports all classes of the Input and Output package
class Unique //class declaration
public static void main(String args[])throws IOException
BufferedReader hq = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter a number.");
int n = Integer.parseInt(hq.readLine());
String s = Integer.toString(n);
int l = s.length();
int f=0; //flag variable
for(int i=0;i<l-1;i++)
for(int j=i+1;j<l;j++)
if(s.charAt(i)==s.charAt(j))
f=1;
break;
} //end of if
} //end of inner for-loop
} //end of outer for-loop
if(f==0)
System.out.println("It is a Unique number.");
else
System.out.println("It is not a Unique number.");
} //end of main
} //end of class
Output:
Question 6:
A pronic number is a number which is represented by the product of two consecutive numbers , that
is a number of the form "n x (n+1)". They are also called rectangular numbers, oblong numbers and
heteromecic numbers.
For ex
6 which is equal to the product of 2X3
30 which is equal to the product of 5X6
Write a program to display all pronic numbers from 1 to n.
Display your output in the following format:
Sample Input:
Enter the value of n: 50
Pronic numbers up to 50 are :
0X1=0
1X2=2
2X3=6
3X4=12
4X5=20
5X6=30
6X7=42
Total Pronic numbers from 1to 50 : 7
Display “No Pronic Numbers” if there are no pronic numbers within the range.
Code:
/**
* Program to display Pronic numbers from 1 to n
*/
import java.util.*; //imports all classes of the Utility package
class Pronic //class declaration
public static void main(String args[])
Scanner hq = new Scanner (System.in);
System.out.println("Enter the value of n:");
int n = hq.nextInt(); //data member decalaration
int a=1, c=0, p=0;
System.out.println("The Pronic numbers upto "+n+" are:");
while(p<=n)
p=a*(a+1);
a++;
if(p<=n)
System.out.println(p);
c++; //counts the numbers within range
} //end of if
} //end of while loop
if(c==0)
System.out.println("No Pronic numbers.");
else
{
System.out.println("Total Pronic numbers from 1 to "+n+": "+c);
} //end of main
} //end of class
Output:
Question 7:
Write a program to input the size of a square matrix. Input the elements. Display them in the matrix
form and also print its diagonals in the given manner.
Note: A matrix is said to be a square matrix if number of rows = number of columns.
The Original array is:
1234
5678
9 12 12 32
43 43 5 6
The Diagonal Cross Pattern is:
1 4
6 7
12 12
43 6
Code:
/**
* Program to convert a square matrix
* to a diagonal matrix
*/
import java.util.*; //imports all classes of the Utility package
class Cross //class declaration
int arr[][], m, n; //data member decalaration
void print(int arr[][], int m, int n)
{ //method to print elements
System.out.println("The diagonal cross pattern is:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
} //end of for-loop
} //end of method
void diagonal(int arr[][], int m, int n)
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(i!=j && i+j+1!=n)
arr[i][j]= 0; //replaces no. outside cross diagonal with '0'
} //end of inner for-loop
} //end of outer for-loop
print(arr, m, n);
} //end of method
public static void main(String args[])
Scanner hq = new Scanner(System.in);
System.out.println("Enter the dimension of the array:");
int m = hq.nextInt();
int n = m;
int arr[][] = new int [m][n];
System.out.println("Enter the elements of the array:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
} //end of inner for-loop
} //end of outer-loop
System.out.println("The elements of the array are:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
} //end of inner-loop
System.out.println();
} //end of outer-loop
System.out.println();
Cross tpn = new Cross(); //object creation
tpn.diagonal(arr, m, n); //calls diagonal(int arr[][], int m, int n)
} //end of main
} //end of class
Output:
Question 8:
Write a program to fill up a two dimensional array of order m X n. Display the array in matrix form.
Sort the elements in ascending order using any standard sorting algorithm and overwrite the existing
two dimensional array. Now display the sorted sequence.
Hint: You can copy the elements of the two dimensional array in a one dimensional array .
For ex
If m=3
N=4
The input sequence
5361
2901
4152
Output
0111
2224
5569
Code:
/**
* Program to input a matrix and
* rearranging in ascending order
*/
import java.io.*; //imports all classes of the Input Output package
import java.util.*; //imports all classes of the Utility package
class Sort //class declaration
void sort(int arr[][], int m, int n)
{ //method to sort the array
int temp[] = new int [m*n];
int k=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
temp[k++] = arr[i][j];
} //end of inner for-loop
} //end of outer for-loop
Arrays.sort(temp);
k=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=temp[k++];
} //end of inner for-loop
} //end of outer for-loop
void print(int arr[][], int m, int n)
{ //method to print the array
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
} //end of inner for-loop
System.out.println();
} //end of outer for-loop
public static void main(String args[])
Scanner hq = new Scanner(System.in);
System.out.println("Enter the dimension of the array:");
int m = hq.nextInt();
int n = hq.nextInt();
int arr[][] = new int [m][n];
System.out.println("Enter the elements of the array:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
} //end of inner for-loop
} //end of outer for-loop
System.out.println("The elements of the array are:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
} //end of inner for-loop
System.out.println();
} //end of outer-loop
System.out.println();
Sort tpn = new Sort(); //object creation
tpn.sort(arr, m ,n); //calls sort(int arr[][], int m, int n)
System.out.println("Matrix after sorting:");
tpn.print(arr, m, n); //calls print(int arr[][],int m, int n)
} //end of main
} //end of class
Output: