Professional Documents
Culture Documents
Computer Project
Computer Project
Computer Project
Question 1
Write a menu driven program using switch case construct that converts a decimal number into its
equivalent Binary, Octal, Hexadecimal.
Code:
/**
*/
int rem;
String bno="";
while(n>0)
bno=bchars[rem]+bno;
n=n/2;
}
System.out.println("Binary equivalent = "+bno);
} //end of method
while(n>0)
ono=ochars[rem]+ono;
n=n/8;
} //end of method
while(n>0)
hno=hchars[rem]+hno;
n=n/16;
} //end of method
int ch = hq.nextInt();
int n = hq.nextInt();
switch(ch)
case 1:
break;
case 2:
break;
case 3:
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:
/**
*/
String bin="";
int rem;
while(n>0)
bin=bchars[rem]+bin;
n=n/2;
return bin;
} //end of method
/**
char ch;
ch=bin.charAt(i);
if(ch=='1')
return c;
} //end of method
int n = hq.nextInt();
{
System.out.println("It is an Evil Number.");
else
} //end of main
} //end of class
Output:
Question 3
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
Code:
/**
* in ascending order
*/
s = s.toUpperCase();
int p = 0, l = s.length();
char ch;
ch = s.charAt(i);
p = p + (ch-64);
/**
*/
return p;
} //end of method
void printResult(String w[], int p[]) // Method to print the final result
int n = w.length;
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 = "";
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[])
String s = hq.nextLine();
int n = fma.countTokens();
System.out.println(words[i]+"\t= "+potential[i]);
System.out.print("\t\t\t");
} //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.
Code:
/**
*/
int arr[][], m, n;
m=x;
n=y;
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
} //end of method
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
int l = 0;
l=arr[i][0];
for(int j=0;j<n;j++)
{
if(arr[i][j]>l)
l=arr[i][j];
} //end of method
int l=0;
l=arr[0][i];
for(int j=0;j<n;j++)
if(arr[j][i]>l)
l=arr[j][i];
} //end of method
int l = 0;
l=arr[i][0];
for(int j=0;j<n;j++)
if(arr[i][j]<l)
l=arr[i][j];
} //end of method
int l = 0;
l=arr[0][i];
for(int j=0;j<n;j++)
if(arr[j][i]<l)
l=arr[j][i];
} //end of method
int a = hq.nextInt();
int b = hq.nextInt();
tpn.input();
tpn.display();
System.out.println("Enter a choice:");
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:
/**
*/
import java.io.*; //imports all classes of the Input and Output package
System.out.println("Enter a number.");
int n = Integer.parseInt(hq.readLine());
String s = Integer.toString(n);
int l = s.length();
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
if(f==0)
else
} //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
Sample Input:
0X1=0
1X2=2
2X3=6
3X4=12
4X5=20
5X6=30
6X7=42
Display “No Pronic Numbers” if there are no pronic numbers within the range.
Code:
/**
while(p<=n)
p=a*(a+1);
a++;
if(p<=n)
System.out.println(p);
} //end of if
if(c==0)
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.
1234
5678
9 12 12 32
43 43 5 6
1 4
6 7
12 12
43 6
Code:
/**
* to a diagonal matrix
*/
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
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
print(arr, m, n);
} //end of method
int m = hq.nextInt();
int n = m;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
} //end of outer-loop
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();
} //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
5361
2901
4152
Output
0111
2224
5569
Code:
/**
*/
int k=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
temp[k++] = arr[i][j];
Arrays.sort(temp);
k=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=temp[k++];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.print(arr[i][j]+" ");
int m = hq.nextInt();
int n = hq.nextInt();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
arr[i][j]=hq.nextInt();
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 outer-loop
System.out.println();
} //end of main
} //end of class
Output: