Java Bit Final
Java Bit Final
Write a program to find the sum, difference, product, quotient and remainder of two numbers passed as command line 4. Write a program to perform base conversion
argument. a) Integer to binary b) Integer to Octal c) Integer to Hexadecimal 7. Write a program to find the trace and transpose of a matrix.
class CommandLine [Link]("Quotient = " + q); import [Link].*; [Link]("Octal equivalent : "+oct); import [Link]; }
{ public static void main(String[] args) [Link]("Remainder = " + r); public class BaseConv [Link]("Hexadecimal equivalent : "+hex); class TransposeAndTrace { [Link]();
{ int a, b, s, d, p, q, r; } } { } public static void main(String[] args) }
a = [Link](args[0]); 1-start public static void main(String args[]) } { [Link]("Transpose of A : ");
b = [Link](args[1]); 2- initiate and declare a,b, sum, different, product, quotient, { 1-start int s=0, i, j, n; for (i = 0; i < n; i++)
s = a + b; d = a - b; p = a * b; reminder int n; 2- initialise variables and declare num to state the input Scanner sc = new Scanner([Link]); {
q = a / b; r = a % b; 3- accept the command line argument as input a and b String bin,oct,hex; numbers. Declare strings (hex, bin,oct) to store [Link]("Order of the square matrix : "); for (j = 0; j < n; j++)
[Link]("Sum = " + s); 4- perform arithmetic operation Scanner sc=new Scanner([Link]); converted values n = [Link](); {
[Link]("Difference = " + d); 5- print output [Link]("Enter an integer : "); 3- accept an integer as input for the variable num int[][] a = new int[n][n]; [Link](" "+a[j][i]);
[Link]("Product = " + p); 6-stop n=[Link](); 4- perform base conversion convert Num to binary octal and [Link]("Read the Elements of Matrix : "); }
bin=[Link](n,2); hexa decimal for (i = 0; i < n; i++) [Link]();
2. Given the sides of a triangle, write a program to check whether the triangle is equilateral, isosceles or scalene and find its area. oct=[Link](n,8); 5- display the output { }
import [Link].*; else if(a==b||b==c||c==a) hex=[Link](n,16); 6-stop for (j = 0; j < n; j++) [Link]("Trace of the matrix A = "+s);
public class Triangle [Link]("Isosceles Triangle"); [Link]("Binary equivalent : "+bin); { }
{ else a[i][j] = [Link](); }
public static void main(String args[]) [Link]("Scalene Triangle"); 5. Write a program to merge two arrays. } 1- START
{ s=(a+b+c)/2; import [Link].*; k = 0; } 2-initialise variables,declare integer variables for Matrix
float a, b, c; double area,s; area=[Link](s*(s-a)*(s-b)*(s-c)); class ArrayMerge while (i<m && j <n) for (i = 0; i < n; i++) dimension and 2D array for the matrix
Scanner sc=new Scanner([Link]); [Link]("Area : "+area); { public static void main(String[] args) { if (a[i] < b[j]) s=s+a[i][i]; 2- Accept input for the matrix, specifying it’s dimension and
[Link]("First side = "); }}} { c[k++] = a[i++]; [Link]("Matrix A : "); populating the elements
a=[Link](); 1-start int m, n, i, j, k; else for (i = 0; i < n; i++) 3- Calculate trace
[Link]("Second side = "); 2- declare 3 sides of triangle. Input 3 sides at run time Scanner s = new Scanner([Link]); c[k++] = b[j++]; { 4- Calculate transpose,use nested loops to transpose the
b=[Link](); 3- if side 1 == side2 and Side 2 == side 3 go to step 6 [Link]("Enter the size of first array : "); } for (j = 0; j < n; j++) original matrix
[Link]("Third side = "); 4- if side 1 == side 2 || side 2 ==side 3 || side 3 ==side1 m = [Link](); while (i < m) { 5- Display the transposed matrix
c=[Link](); go to steps 7 int a[] = new int[m]; c[k++] = a[i++]; [Link](" "+a[i][j]); 7-stop
if(a>=(b+c)||b>=(c+a)||c>=(a+b)) 5- else go to step 8 [Link]("Read elements of first array : "); while (j < n)
[Link]("Cannot form a triangle"); 6- print triangle is equilateral for (i = 0; i < m; i++) c[k++] = b[j++]; 8. Write java program to find the sum of the digits and reverse of a given number using class and objects.
else 7- print triangle is isosceles { a[i] = [Link](); [Link]("Array after merging : "); import [Link].*; [Link]("Reverse of "+n+" = ");
{ if(a==b && b==c && a==c) 8- print triangle is scalene } for (i = 0; i < m+n; i++) class SumAndRev for(i=0;i<k;i++)
[Link]("Equilateral Triangle"); 9- stop [Link]("Enter the size of second array : "); { { { [Link](rev[i]);
n = [Link](); [Link](c[i]); int a,n,k,i,sum=0,x; } }
3. Read an array of 10 or more numbers and write a program to find the , int b[] = new int[n]; } } } int[] rev = new int[50]; }
a) Smallest element in the array b) Largest element in the array c) Second largest element in the array [Link]("Read elements of second array : "); 1-start public void sumrev() class SumAndRevMain
import [Link].*; a[j]=a[j+1]; for (i = 0; i < n; i++) { 2- initialise a array arr1,arr2 and an array before merged { {
public class NumArray a[j+1]=t; b[i] = [Link](); array [Link]("Enter the number : "); public static void main(String args[])
{ } } 3- use a loop to copy all elements of arr1 merged array Scanner sc = new Scanner([Link]); {
public static void main(String args[]) } [Link](a); 4- use another loop to copy all elements of arr2 to the n=[Link](); SumAndRev ob = new SumAndRev();
{ } [Link](b); remaining positions in merged array x=n; [Link]();
int n,i,j,t; [Link]("Smallest element : "+a[0]); int c[] = new int[m+n]; 5- display the merged array k=0; [Link]();
int a[]=new int[100]; [Link]("Largest element : "+a[n-1]); i = 0; j = 0; 6-stop while(x>0) [Link]();
Scanner sc=new Scanner([Link]); for(i=n-1;a[i]==a[i-1];i- -); { }
[Link]("Enter the size of the array : "); [Link]("Second largest element : "+a[i-1]); 6. Java Programming Code to Find HCF LCM of Two Numbers a=x%10; }
n=[Link](); } import [Link].*; break; sum=sum+a; [Link]
[Link]("Enter the elements : "); } public class HcfLcm } } x=x/10; [Link] a class with instance variable for the input number,
for(i=0;i<n;i++) 1- Start { public static void main(String args[]) lcm=(a*b)/hcf; rev[k]=a; sum and reversed.
{ a[i]=[Link](); 2- Read array of 10 or more numbers from the user { int a,b,c,i,hcf=1,lcm; [Link]("HCF of "+a+ " and "+b+" = "+hcf); k++; [Link] numbers for sum and reverse.
} 3- Initialise variable smallest, largest and second largest to Scanner sc=new Scanner([Link]); [Link]("LCM of "+a+ " and "+b+" = "+lcm); } [Link] sum of digits.
for(i=0;i<n-1;i++) the first 3 elements of array [Link]("Enter two numbers : "); } } [Link] reverse by using loops
{ 4- Traverse array from the 4th element to the end a=[Link](); } public void display() [Link] output.
for(j=0;j<n-1;j++) 5- For each element compare it with smallest, largest and b=[Link](); 1-start { [Link]
{ second largest and update the variable c=a<b?a:b; 2- initialise and declare variable num1,num2,HCF and LCM [Link]("Sum of the digits of "+n+" = "+sum);
if(a[j]>a[j+1]) accordingly for(i=c;i>=2;i- -) 3- calculate the HCF
{ 6- Display the smallest,largest and second largest { 4- calculate the LCM use the formula LCM =
t=a[j]; 7-stop if(a%i==0 && b%i==0) (num1*num2)/HCF
{ 5- display the output
hcf=i; 6-STOP