For better view dowload PDF version here
—Question 1—
import java.util.Scanner;
class A
{
void main()
{
int cod[] = new int[10];
int pric[]= new int[10]; //unit price
int qty[] = new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("Enter product code, unit price and quantity of 10 products ");
for(int i=0; i<10; i++)
{
System.out.println("---Product " + (i+1) + " ---");
cod[i] = sc.nextInt();
pric[i] = sc.nextInt();
qty[i] = sc.nextInt();
}
System.out.println("Code\t\tUnit Price\t\tQuantity\t\tTotal Price")
for(int i=0; i<10; i++)
{
System.out.print(cod[i] + "\t\t");
System.out.print(pric[i] + "\t\t");
System.out.print(qty[i] + "\t\t");
System.out.print(pric[i] * qty[i] + "\n");
}
}
}
—Question 2—
class a
{
void findMarks(float M[])
{
int n=M.length;
int minNo=a[0];
int maxNo=a[0];
int sum=0;
for(int i=0;i<n;i++)
{
sum += a[i]
if(a[i] > maxNo){
maxNo = a[i];
}
if(a[i] < minNo)
{
minNo = a[i];
}
}
System.out.println("Highest No: " + maxNo);
System.out.println("Lowest No: " + minNo);
System.out.println("Average: " + (sum/10));
}
}
—Question 3—
class A
{
static void main()
{
char x1[] = {'x','y','z','w'};
char x2[] = {'x','y','z','w'};
boolean isIdentical = true;
for(int i=0; i<4; i++)
{
if(x1[i] != x2[i])
isIdentical = false;
}
if(isIdentical)
System.out.println("Both the arrays are identical");
else
System.out.println("Both the arrays are not identical");
}
}
—Question 4—
import java.util.Scanner;
class A
{
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of N");
int N = sc.nextInt();
int x[] = new int[N];
int y[] = new int[N];
int z[] = new int[N];
int j=0;
//get input in both arrays
for(int i=0; i<N; i++)
{
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
for(int i=0; i<N; i++)
{
if(x[i] == y[i])
{
z[j] = x[i];
j++;
}
}
System.out.println("Elements of array x: ");
displayArray(x);
System.out.println("Elements of array y: ");
displayArray(y);
System.out.println("Elements of array z: ");
displayArray(z);
}
static void displayArray(int a[])
{
for(int i=0; i<a.length; i++)
{
System.out.print(a[i] + " ");
}
}
}
—Question 5—
import java.util.Scanner;
public class A {
public static void main(String []args) {
int i,j,temp,n;
Scanner sc = new Scanner(System.in);
n = 15;
int AR[] = new int[n];
System.out.println("Enter " + n + " numbers: ");
for (i = 0; i <n; i++)
AR[i] = sc.nextInt();
for (i = 0; i < ( n - 1 ); i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (AR[j] > AR[j+1]) //swap the elements if first one is greater than second
{
temp = AR[j];
AR[j] = AR[j+1];
AR[j+1] = temp;
}
}
}
System.out.println("******Sorted list******");
for (i = 0; i < n; i++)
System.out.print(AR[i] + " ");
}
}
—Question 6—
import java.util.Scanner;
public class A {
public static void main(String []numgs)
{
int i,j,temp,n;
Scanner sc = new Scanner(System.in);
n = 15;
int num[] = new int[n];
System.out.println("Enter " + n + " numbers: ");
for (i = 0; i <n; i++)
num[i] = sc.nextInt();
for (i = 0; i < ( n - 1 ); i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (num[j] > num[j+1]) //swap the elements if first one is greater than second
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
System.out.println("******Sorted list******");
for (i = 0; i < n; i++)
System.out.print(num[i] + " ");
//start the process of searching
System.out.println("Enter number to search");
int val = sc.nextInt();
int start=0, end=n-1,mid=0, found=0;
while(start <= end)
{
mid = (start+end)/2;
if(num[mid] == val)
{
found=1;
break;
}
else if(val > num[mid])
start = mid+1;
else
end = mid-1;
}
if(found == 1)
System.out.println(val + " found at index : " + mid);
else
System.out.println(val + " not found");
}
}
—Question 7—
import java.util.*;
class A
{
public static void main()
{
float A[] = new float[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 elements");
for(int i=0; i<A.length; i++)
{
A[i] = sc.nextInt();
}
System.out.println("Before Sorting : ");
for (int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
for (int i = 0; i < A.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < A.length; j++)
{
if (A[j] > A[index])
{
index = j;//searching for lowest index
}
}
float smallerNumber = A[index];
A[index] = A[i];
A[i] = smallerNumber;
}
System.out.println("Sorted array : ");
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
}
}
—Question 8—
import java.util.Scanner;
class A
{
static void main()
{
Scanner sc = new Scanner(System.in);
int M=10, N=5;
int a[] = new int[M];
int b[] = new int[N];
System.out.println("Enter " + M + " Numbers for automorphic") ;
for(int i=0; i<a.length; i++)
a[i] = sc.nextInt();
System.out.println("Enter " + N + " Numbers for palindrome") ;
for(int i=0; i<b.length; i++)
b[i] = sc.nextInt();
printNumbers(a,b);
}
static void printNumbers(int Y[], int Z[])
{
int sizeY = Y.length;
int sizeZ = Z.length;
System.out.println("The automorphic numbers are: ");
for(int i=0; i<sizeY; i++)
{
int num = Y[i];
if(automorphic(num))
System.out.print(num + " ");
}
System.out.println();
System.out.println("\nThe palindrome numbers are: ");
for(int i=0; i<sizeZ; i++)
{
int num = Z[i];
if(palindrome(num))
System.out.print(num + " ");
}
}
static boolean automorphic(int num)
{
int count=0, sqr = num*num;
int temp =num;
//count digit of number
while(temp>0){
count++;
temp=temp/10;
}
//taking last count digit of sqr of num
int lastSquareDigits = (int) (sqr%(Math.pow(10,count)));
if(num == lastSquareDigits)
return true;
else
return false;
}
static boolean palindrome(int n)
{
int r,sum=0,temp;
temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10; //eliminating rightmost digit
}
if(temp==sum)
return true;
else
return false;
}
}
—Question 9—
import java.util.Scanner;
class b
{
static void main()
{
int temp,n;
Scanner sc = new Scanner(System.in);
n = 30;
int num[] = new int[n];
System.out.println("\nEnter weight of " + n + " people: ");
for (int i = 0; i <n; i++)
num[i] = sc.nextInt();
System.out.println("Before Sorting");
for (int i = 0; i < n; i++)
System.out.print(num[i] + " ");
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (num[j] < num[j+1]) //swap the elements if first one is greater than second
{
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
System.out.println("\n\n******Sorted list******");
for (int i = 0; i < n; i++)
System.out.print(num[i] + " ");
}
}
—Question 10—
import java.util.Scanner;
class A
{
public static void binarySearch()
{
int arr[] = {3,5,8,10,13,-14,17,-20,22,-35,52,76,80};
int first = 0, last=arr.length-1, found=0, n = arr.length;
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter value to search");
int num = sc.nextInt();
int mid = (first + last)/2;
while( first <= last )
{
if(arr[mid] == num)
{
found=1;
break;
}
else if ( arr[mid] < num )
{
first = mid + 1;
}
else
{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( found == 1 )
System.out.println("Element found at index " + mid);
else
System.out.println("Search Unssuccessful!");
}
}
—Question 11—
class b
{
static void main()
{
int A[] = {2000, 1998, 1996, 2010, 1984, 2012, 2011, 1992, 2013, 1999, 2018, 2016};
System.out.println("Before Sorting : ");
for (int i = 0; i < A.length; i++)
{
System.out.print(A[i] + " ");
}
for (int i = 0; i < A.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < A.length; j++)
{
if (A[j] > A[index])
{
index = j;
}
}
int smallerNumber = A[index];
A[index] = A[i];
A[i] = smallerNumber;
}
System.out.println("Sorted array : ");
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
}
}
—Question 12—
class a
{
static void main()
{
int arr[][] = {{4,3,2}, {1,-5,6}, {7,-8,9}};
int pro=1, sum=0;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(arr[i] + "\t");
int num = arr[i][j];
if(num % 2 == 0)
pro = pro * num;
else
sum = sum + num;
}
System.out.println();
}
System.out.println("\nSum of odd numbers: " + sum);
System.out.println("\Product of even numbers: " + pro);
}
}
—Question 13—
class a
{
static void main()
{
int arr[][] = {{4,3,2}, {1,-5,6}, {7,-8,9}};
int pro=1, sum=0;
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
System.out.print(arr[i] + "\t");
int num = arr[i][j];
if(i == j)
sum = sum + num;
}
System.out.println();
}
System.out.println("\nSum of left diagonal: " + sum);
}
}
—Question 14—
import java.util.Scanner;
class a
{
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of M");
int M = sc.nextInt();
int arr[][] = new int[M][M]
int pro=1, sum=0;
System.out.println("Enter the elements ");
for(int i=0; i<M; i++)
{
for(int j=0; j<M; j++)
{
arr[i][j] = sc.nextInt();
int num = arr[i][j];
if(i+j == 2)
{
if(num % 2 == 0)
sum = sum + num;
}
}
}
for(int i=0; i<M; i++)
{
for(int j=0; j<M; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println("\nSum of right diagonal: " + sum);
}
}
—Question 15—
class a
{
static void main()
{
int arr[][] = {{3,4,5,6}, {1,2,7,9}, {4,5,6,8}};
int pro=1, sum=0;
for(int i=0; i<3; i++)
{
for(int j=0; j<4; j++)
{
int num = arr[i][j];
sum = sum + num;
}
System.out.println("Sum of row " + i + ": " + sum);
sum=0;
}
}
}
—Question 16—
import java.util.Scanner;
class aaa
{
static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of R and C");
int R = sc.nextInt();
int C = sc.nextInt();
int arr[][] = new int[R][C];
int sum=0;
System.out.println("Enter the elements ");
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
arr[i][j] = sc.nextInt();
}
}
//print the array
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
System.out.println();
//calculate column sum
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
if(j < R)
{
int num = arr[j][i];
sum = sum + num;
}
}
System.out.println("Sum of column " + i + ": " + sum);
sum=0;
}
}
}