0% found this document useful (0 votes)
13 views12 pages

12 ISC IMPORTANT PROGRAMS

The document contains multiple Java classes that implement various functionalities such as matrix operations, searching algorithms (linear and binary), sorting (insertion sort), checking for prime triplets, and Armstrong numbers. Each class includes methods for user input, processing data, and displaying results. The code demonstrates basic programming concepts like loops, conditionals, and array manipulation.

Uploaded by

cruisetom2713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

12 ISC IMPORTANT PROGRAMS

The document contains multiple Java classes that implement various functionalities such as matrix operations, searching algorithms (linear and binary), sorting (insertion sort), checking for prime triplets, and Armstrong numbers. Each class includes methods for user input, processing data, and displaying results. The code demonstrates basic programming concepts like loops, conditionals, and array manipulation.

Uploaded by

cruisetom2713
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

import java.util.

*;
class matrix
{
void f()
{
int ar[][]=new int [4][4];
int x = ar.length;
Scanner sc = new Scanner(System.in);
int i,j,se=0,so=0;
System.out.println("enter elements into matrix");
for(i=0;i<4;i++)
{
for (j=0;j<4;j++)
ar[i][j]=sc.nextInt();
}
System.out.print("original matrix");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.printf("%5d",ar[i][j]);
if(ar[i][j]%2==0)
se=se+ar[i][j];
else
so=so+ar[i][j];
System.out.println();
}
System.out.println("sum of even"+se);
System.out.println("sum of odd"+so);
}
}
import java.util.*;
class kkk
{
void f()
{
int ar[][]=new int[4][4];
Scanner sc= new Scanner(System.in);
int i,j,big=0;
System.out.println("enter elements into matrix");
for (i=0;i<4;i++)
{
for(j=0;j<4;j++)
ar[i][j]=sc.nextInt();
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
big=Math.max(big,ar[i][j]);
}
}
System.out.println("largest"+big);
}
}

output:- enter elements into matrix


26
17
38
4 9 - Largest
5
import java.util.*;
class hhh
{
void f()
{
int ar[][]=new int[4][4];
Scanner sc= new Scanner(System.in);
int i,j,big=0;
System.out.println("enter elements into matrix");
for (i=0;i<4;i++)
{
for(j=0;j<4;j++)
ar[i][j]=sc.nextInt();
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
big=Math.max(big,ar[i][j]);
}
}
System.out.println("largest"+big);
int r=0,c=0;
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(ar[i][j]>ar[r][c])
{
r=i;
c=j;
}
}
}
}
}
import java. util.*;
class vvv
{
void boundary(int M,int N)
{
int x[][]=new int[M][N];
Scanner sc = new Scanner(System.in);
int i,j;
System.out.println("enter elements");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
x[i][j]=sc.nextInt();
}
for (i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(i==0||j==0||i==M-1||j==N-1)
System.out.printf("%5d",x[i][j]);
else
System.out.print(" ");
}
System.out.println();
}
}
}
import java.util.*;
class LinearSearch
{
void main ()
{
Scanner Sc= new Scanner(System.in);
System.out.println("Enter the size of array :");
int l=Sc.nextInt();
int arr[]=new int[l];
int Pos=-1;
//Input
System.out.println("Enter "+l+" elements : ");
for(int i=0;i<l;i++)
{
arr[i]=Sc.nextInt();
}
System.out.println("Enter the element to be
searched : ");
int n=Sc.nextInt();
//Search
for(int i=0;i<l;i++)
{
if(arr[i]==n)
{
Pos=i;
break;
}
}
//Print
if(Pos==-1)
{
System.out.println("Element not found :");
}
else
{
System.out.println("Element found at Position : "+
(Pos+1));
}
}}
import java.util.*;
class BinarySearch
{
void main()
{
Scanner Sc = new Scanner(System.in);
int arr[] = new int [10];
System.out.println("Enter elements : ");
int l=arr.length;
for(int i=0;i<l;i++)
{
arr[i]=Sc.nextInt();
}
System.out.println("Enter the element to be search :
");
int n=Sc.nextInt();
int s=0,e=l-1,m,Pos=-1;
m=(s+e)/2;
while(s<=e)
{
if(n==arr[m])
{
Pos=m;
break;
}
else if(n<arr[m])
{
e=m-1;
}
else
{
s=m+1;
}
}
if(Pos==-1)
{
System.out.println("Element not found !");
}
else
{
System.out.println("Element found at Position :
"+Pos+1);
import java.util.*;
class InsertionSort {
void insertionSort(int[] arr)
{
int n = arr.length;
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
insertionSort(arr);
System.out.println("Sorted array:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " “);
}
}
}
import java.util.Scanner;
class RearrangeArray
{
void insertionSort(int[] arr)
{
int n = arr.length;
for (int i = 1; i < n; i++)
{
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements: ");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
insertionSort(arr);
System.out.println("Rearranged (sorted) array in
ascending order:");
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " “)
}
}
}
import java.util.Scanner;
class PrimeTriplet
{
boolean isPrime(int num)
{
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}

void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to find prime
triplets: ");
int limit = sc.nextInt();
System.out.println("Prime triplets up to " + limit
+ ":");
for (int i = 2; i <= limit - 4; i++) {
if (isPrime(i) && isPrime(i + 2) && isPrime(i + 4))
{
System.out.println("(" + i + ", " + (i + 2) + ", "
+ (i + 4) + ")");
}
}
}
import java.util.Scanner;
class ArmstrongNumber
{
int armstrongSum(int number, int power)
{
if (number == 0)
{
return 0;
}
int digit = number % 10;
return (int) Math.pow(digit, power) +
armstrongSum(number / 10, power);
}
boolean isArmstrong(int number)
{
int originalNumber = number;
int digitsCount = (int) Math.log10(number) + 1;
int sum = armstrongSum(number, digitsCount);
return sum == originalNumber;
}
void main()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isArmstrong(number))
{
System.out.println(number + " is an Armstrong
number.");
}
else
{
System.out.println(number + " is not an
Armstrong number.");
}
}
}
import java.util.Scanner;
class MatrixExample
{
void main()
{
int[][] matrix = new int[4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the elements for a 4x4
matrix:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
{
System.out.print("Enter element at [" + i + "]["
+ j + "]: ");
matrix[i][j] = sc.nextInt();
}
}
int rows = matrix.length;
int columns = matrix[0].length;
System.out.println("\nMatrix entered:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("\nNumber of rows: " + rows);
System.out.println("Number of columns: " +
columns);
}}
import java.util.*;
class ReverseArray
{
void main ()
{
Scanner Sc= new Scanner (System.in);
int n=Sc.nextInt();
int ar[]=new int [n];
System.out.println("Enter 'n' numbers");
for(int i=0;i<n;i++)
{
ar[i]=Sc.nextInt();
}
System.out.println("");
System.out.println("Reversed Array");
//Reverse
for(int i=n-1;i>=0;i--)
{
System.out.print(ar[i]+" ");
}
System.out.println("");
}
}

You might also like