freetimestudys@gmail.com          +91-7097633380
  Login Here                        

sum of two matrices

import java.io.*;
class clsmatrix
{
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
int i,j,m,n;
int a[][] = new int[10][10];
int b[][] = new int[10][10];
int c[][] = new int[10][10];
System.out.println("Enter the order of matrix");
m=Integer.parseInt(br.readLine());
n=Integer.parseInt(br.readLine());
System.out.println("Enter the first matrix");
for(i=0;i{
for(j=0;j{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Enter the second matrix");
for(i=0;i{
for(j=0;j{
b[i][j]=Integer.parseInt(br.readLine());
}
}
for(i=0;i{
for(j=0;j{
c[i][j]=a[i][j] + b[i][j] ;

}
}
System.out.println("The third matrix is");
for(i=0;i{
for(j=0;j{
System.out.print(c[i][j]);
}
System.out.print("\n");
}
}
}

intput and output

Enter the order of matrix
3
3
Enter the first matrix
1 1 1
1 1 1
1 1 1
Enter the second matrix
2 2 2
2 2 2
2 2 2
The third matrix is
3 3 3
3 3 3
3 3 3

SORTING NAMES IN ASCENDING ORDER

import java.io.*;
class clsnamesort
{
public static void main(String args[])throws IOException
{
int i,j,n;
String t;
String Sname[] = new String[25];
System.out.println("Enter Total no of names : ");
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
System.out.println("Enter The names : ");
for(i=0;i{
Sname[i]=br.readLine();
}
for(i=0; ifor(j=1; jif(Sname[i].compareTo(Sname[j])>0)
{
t = Sname[i];
Sname[i] = Sname[j];
Sname[j] = t;
}
System.out.println("After Sorting ");
for(i=0;iSystem.out.println(Sname[i]);
}
}

intput and output

Enter The names :
chirag
ankit
After Sorting
ankit
chirag

Largest and Smallest Number in an Array

public class FindLargestSmallestNumber {

public static void main(String[] args) {

//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};

//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];

for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];

}

System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}

intput and output

Largest Number is : 98
Smallest Number is : 23

Count even numbers in an array.

public class Array {
public static void main(String[] args) {
int number[]=new int[]{1,2,3,4,5,6,7,8,9};
for(int p=0;p
if(number[p]%2==0)
System.out.println(number[p]+ " is Even number");
else
System.out.println( number[p]+" is odd umber");
}
}
}

intput and output

1,3,5,7,9 is odd Number
2,4,6,8 is Even Number

Count occurrence of a given number in an array

import java.util.Scanner;
public class Count_Occurrence
{
public static void main(String[] args)
{
int n, x, count = 0, i = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the element of which you want to count number of occurrences:");
x = s.nextInt();
for(i = 0; i < n; i++)
{
if(a[i] == x)
{
count++;
}
}
System.out.println("Number of Occurrence of the Element:"+count);
}
}

intput and output

Enter no. of elements you want in array:5 Enter all the elements: 2 3 3 4 3 Enter the element of which you want to count number of occurrences:3 Number of Occurrence of the Element:3

Check if given number is palindrome or not in java

class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome

temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}

intput and output

palindrome number

Implement Binary Search in java

public class MyBinarySearch {

public int binarySearch(int[] inputArr, int key) {

int start = 0;
int end = inputArr.length - 1;
while (start <= end) {
int mid = (start + end) / 2;
if (key == inputArr[mid]) {
return mid;
}
if (key < inputArr[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}

public static void main(String[] args) {

MyBinarySearch mbs = new MyBinarySearch();
int[] arr = {2, 4, 6, 8, 10, 12, 14, 16};
System.out.println("Key 14's position: "+mbs.binarySearch(arr, 14));
int[] arr1 = {6,34,78,123,432,900};
System.out.println("Key 432's position: "+mbs.binarySearch(arr1, 432));
}
}

intput and output

Key 14's position: 6 Key 432's position: 4

implement bubble sort in java

import java.util.*;

public class BubbleSort_6_18
{
public static void main(String[] args)
{
Random generator = new Random();

int[] list = new int[11];
for(int i=0; i {
list[i] = generator.nextInt(10);
}
System.out.println("Original Random array: ");
printArray(list);
bubbleSort(list);
System.out.println("\nAfter bubble sort: ");
printArray(list);
}
public static void bubbleSort(int[] list)
{
for(int i=0; i for(int j=i + 1; j {
if(list[i] > list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}

}
}

public static void printArray(int[] list)
{
for(int i=0; i  System.out.print(list[i] + ", ");
}
}
}

intput and output

Original Random array:
8, 5, 2, 8, 0, 1, 0, 5, 3, 8, 8,
After bubble sort:
0, 0, 1, 2, 3, 5, 5, 8, 8, 8, 8,