Professional Documents
Culture Documents
import java.util.*;
class Prg_1
{ void main()
{ int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that every
4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored in a
separate row.
5
7
4
3
6
7
8
8
8
6
5
2
3
5
4
9
The array is:
5743
6788
8652
3549
import java.util.*;
class Prg_1
{ void main()
{ int a[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int r[]={0,0,0,0};
int c[]={0,0,0,0};
int t=0;
for( i=0;i<4;i++)
{for( j=0;j<4;j++)
{ r[t]=r[t]+a[i][j];
c[t]=c[t]+a[j][i];}
t++;}
for( i=0;i<4;i++)
System.out.println("Sum of row "+(i+1)+" is "+r[i]);
for( j=0;j<4;j++)
System.out.println("Sum of column "+(j+1)+" is "+c[j]);
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
5
4
3
3
6
2
6
7
4
8
5
6
4
3
2
The array is:
1 5 4 3
3 6 2 6
7 4 8 5
6 4 3 2
Sum of row 1 is 13
Sum of row 2 is 17
Sum of row 3 is 24
Sum of row 4 is 15
Sum of column 1 is
Sum of column 2 is
Sum of column 3 is
Sum of column 4 is
17
19
17
16
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter the array 2 elements which will be stored such
that every 4 elements get stored in a separate row.");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
for( i=0;i<4;i++)
for( j=0;j<4;j++)
c[i][j]=a[i][j]+b[i][j];
System.out.println("Added matrix");
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print( c[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array 1 elements which will be stored such that every 4 elements get
stored in a separate row.
5
2
3
3
3
3
3
3
6
4
6
4
7
8
5
4
The array is:
5 2 3 3
3 3 3 3
6 4 6 4
7 8 5 4
Enter the array 2 elements which will be stored such that every 4 elements get
stored in a separate row.
43
2
3
4
6
8
8
6
4
5
9
5
7
5
4
3
The array is:
43 2 3 4
6 8 8 6
4 5 9 5
7 5 4 3
Added matrix
48 4 6 7
9 11 11 9
10 9 15 9
14 3 9 7
15
import java.util.*;
class Prg_2
{ void main()
{int a[][]=new int [4][4];
int b[][]=new int [4][4];
int c[][]=new int [4][4];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the array 1 elements which will be stored such that
every 4 elements get stored in a separate row.");
int i, j;
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter the array 2 elements which will be stored such that
every 4 elements get stored in a separate row.");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
b[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(b[i][j] + " ");
}
System.out.println();
}
for(i=0;i<4;i++)
for( j=0;j<4;j++)
c[i][j]=a[i][j]-b[i][j];
System.out.println("Subtracted matrix");
for(i=0;i<4;i++)
{ for( j=0;j<4;j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
boolean x;
System.out.println("The prime elements:");
for( i=0;i<4;i++)
for( j=0;j<4;j++)
{ x=prime(a[i][j]);
if(x)
System.out.println(a[i][j]);
}
}
static boolean prime(int n)
{ int cn=0;
for(int i=1;i<=n;i++)
if(n%i==0)
cn++;
if(cn==2)
return true;
else
return false;
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
4
7
5
5
4
23
1
2
4
6
8
0
9
8
7
7
The array is:
4 7 5 5
4 23 1 2
4 6 8 0
9 8 7 7
The prime elements:
7
5
5
23
2
7
7
{b[c]=a[i][j];
c++;}
for( i=0;i<15;i++)
for(j=0;j<16-i-1;j++)
if(b[j]>b[j+1])
{t=b[j];
b[j]=b[j+1];
b[j+1]=t;}
System.out.println("After sorting");
for( i=0;i<16;i++)
System.out.println(b[i]);
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
6
5
5
4
4
4
4
7
8
2
3
41
2
3
6
8
The array is:
6 5 5 4
4 4 4 7
8 2 3 41
2 3 6 8
After sorting
2
2
3
3
4
4
4
4
5
5
6
6
7
8
8
41
{ for(int y=0;y<4;y++)
{ if(x==m)
{a[y][x]=b[c];c++;}
}
}
c=0;
}
}
}
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print(a[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
2
4
5
4
8
7
6
5
4
3
1
2
3
5
6
The array is:
1 2 4 5
4 8 7 6
5 4 3 1
2 3 5 6
Array after sorting the columns
1231
2345
4456
5 876
1
3
5
6
7
8
The array is:
5 4 7 3
5 8 9 6
4 2 1 3
5 6 7 8
The highest
The highest
The highest
The highest
in
in
in
in
row
row
row
row
1
2
3
4
is
is
is
is
7
9
4
8
h=a[j][i];}
System.out.println("The highest in "+(i+1)+" column is "+h);
h=0;
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get stored
in a separate row.
7
6
6
6
5
4
3
5
8
7
6
5
3
2
4
6
The array is:
7 6 6 6
5 4 3 5
8 7 6 5
3 2 4 6
The highest
The highest
The highest
The highest
in
in
in
in
1
2
3
4
column
column
column
column
is
is
is
is
8
7
6
6
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The array is: \n");
for(i=0;i<4;i++)
{
for(j=0; j<4; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int b[]=new int [4];int c=0;
for( i=0;i<4/2;i++)
{ for( j=0;j<4;j++)
{ b[c]=a[i][j];
int x=4-i-1;
//for(int x=4-i+1;x>=0;x++)
a[i][j]=a[x][j];
a[x][j]=b[c];
c++;
}
c=0;
}
System.out.println("The reversed array is:");
for( i=0;i<4;i++)
{ for( j=0;j<4;j++)
{ System.out.print(a[i][j]+" ");}
System.out.println();
}
}
}
Output:
Enter the array elements which will be stored such that every 4 elements get
stored in a separate row.
1
6
3
7
8
6
6
6
5
4
3
2
3
4
5
6
The array is:
1 6 3 7
8 6 6 6
5 4 3 2
3 4 5 6
The reversed array is:
3456
5432
8666
1 637
{ System.out.print(a[i][j]+" ");}System.out.println();}}}
Output:
Enter the elements
6
3
4
5
7
8
9
3
2
1
2
3
4
5
7
7
The array is:
6 3 4 5
7 8 9 3
2 1 2 3
4 5 7 7
The array after interchanging:
4577
7893
2123
6 345
}
}
System.out.println("The array is: \n");
for(i=0;i<5;i++)
{
for(j=0; j<5; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int c=0;
for( i=5-1;i>=0;i--)
{ for( j=5-1;j>=0;j--)
if(i==j)
{ for(int k=0;k<j;k++)
{System.out.print(a[c][k]+" ");}
System.out.println();c++;
}
}
}
}
Output:
Enter the array elements which will be stored such that every 5 elements get
stored in a separate row.
1
5
4
8
7
4
1
2
1
4
5
6
9
8
7
4
5
6
3
2
1
5
6
9
8
5
1
6
5
5
4
2
9
6
6
8
1
8
3
9
7
4
7
2
8
1548
412
56
4
8
9
7
6
5
4
4
3
2
The array is:
5 4 4 3
3 5 7 8
9 7 6 5
4 4 3 2
Product of
Product of
Product of
Product of
Product of
Product of
Product of
Product of
column 1 :540
row 1 :240
column 2 :560
row 2 :840
column 3 :504
row 3 :1890
column 4 :240
row 4 :96
55
41
55
40
83 60 84
73
89
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
System.out.print(d[j][k]+" ");
System.out.println();
}
for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}
k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
System.out.print(d[j][k]+" ");
System.out.println();
}
for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}
k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of right diagonal elements are :"+sum2);
}
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
System.out.print(d[j][k]+" ");
System.out.println();
}
for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}
k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of Diagonal elements are :"+sum1+" and
"+sum2);
}
}
Output:
Enter the size of 2D array :
5
Enter the values of 2D array of 5 * 5 matrix
1
3
6
9
8
7
4
5
2
1
4
6
3
2
5
6
9
8
7
4
1
2
3
6
5
13698
74521
46325
69874
12365
Sum of Diagonal elements are :20 and 23
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
System.out.print(d[j][k]+" ");
System.out.println();
}
for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}
k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Sum of the sum of both its diagonal
elements :"+(sum1+sum2));
}
}
Output:
Enter the size of 2D array :
3
Enter the values of 2D array of 3 * 3 matrix
5
4
5
6
9
8
7
2
1
545
698
721
Sum of the sum of both its diagonal elements :36
import java.io.*;
import java.lang.*;
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
{
d[j][k]=Integer.parseInt(br1.readLine());
}
System.out.println();
}
for(j=0;j<i;j++)
{
for(k=0;k<i;k++)
System.out.print(d[j][k]+" ");
System.out.println();
}
for(j=0;j<i;j++)
{
sum1=sum1+d[j][j];
}
k=i-1;
for(j=0;j<i;j++)
{
if(k>=0)
{
sum2=sum2+d[j][k];
k--;
}
}
System.out.println("Difference between the sums of both its
diagonal elements :"+(Math.abs(sum1-sum2)));
}
}
Output:
Enter the size of 2D array :
3
Enter the values of 2D array of 3 * 3 matrix
1
2
3
6
5
4
7
8
9
123
654
789
Difference between the sums of both its diagonal elements :0
23.
import java.util.*;
class bubsort
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[] = new int[5];
int i,k,t,sn,ss;
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
a[i]= sc.nextInt();
}
for(i=0;i<4;i++)
{
for(k=0;k<4;k++)
{
if(a[k] > a[k+1])
{
t=a[k];
a[k]=a[k+1];
a[k+1]=t;
}}}
System.out.println("The sorted array is :");
for(i=0;i<5;i++)54
{
System.out.println(a[i]);
}
}}
Output:
Enter 5 numbers :
51
23
56
89
78
The sorted array is :
23
51
56
78
89
24.
import java.io.*;
public class SelectionSort
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
int a[] = new int[5];
int i,k,t,min,minpos;
// To accept the number you are looking for
System.out.println("Enter 5 numbers :");
for(i=0;i<5;i++)
{
System.out.println("Enter the next number.");
a[i]= Integer.parseInt(br.readLine());
}
//To arrange number in ascending order
for(i=0;i<=3;i++)
{
min=a[i];
minpos=i;
for(k=i+1;k<=4;k++)
{
if(a[k] < min)
{
min=a[k];
minpos=k;
}
}
t=a[i];a[i]=a[minpos];a[minpos]=t;
}
//To print the array
System.out.println("The sorted array is :");
for(i=0;i<5;i++)
{
System.out.println(a[i]);
}
}}
Output:
Enter 5 numbers :
Enter the next number.
6
Enter the next number.
5
Enter the next number.
4
Enter the next number.
1
Enter the next number.
3
The sorted array is :
1
3
4
5
6
if(search==A[mid])
{
System.out.println("Found");
flag = true;
break;
}
else if(search>A[mid])
{
lb = mid+1;
}
else{
ub = mid-1;
}
}while(ub!=lb);
if(flag == false)
{
System.out.println("Not Found");
}
}
}
Output:
Enter 5 numbers :
78
12
32
56
48
Enter the search term :
48
Found