Program to sort numbers in Descending Order using Selection
Sort
import java.util.Scanner;
public class SelectionSortDescending
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
for(int i=0; i<10; i++)
{
System.out.println("Enter a number");
a[i]=sc.nextInt();
}
System.out.println("Unsorted Array Elements");
for(int i=0; i<10; i++)
{
System.out.println(a[i]);
}
// Selection Sort Logic for sorting in descending order
int i, j, temp, big,pos;
for(i=0; i<9; i++)
{
big=a[i];
pos=i;
for(j=i+1; j<10; j++)
{
if(a[j]>big)
{
big=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
System.out.println("Sorted Array is ");
for(i=0; i<10; i++)
{
System.out.println(a[i]);
}
}
}
Program to print the left diagonal elements of a matrix
public class LeftDiagonal
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
for(int i=0; i<3; i++)
{
System.out.println("Enter the elements of row "+(i+1));
for(int j=0; j<3; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Left Diagonal Elements are: ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if(i==j)
System.out.println(a[i][j]);
}
}
}
}
Program to print the right diagonal of a matrix
import java.util.Scanner;
public class RightDiagonal
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
for(int i=0; i<3; i++)
{
System.out.println("Enter the elements of row "+(i+1));
for(int j=0; j<3; j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Right Diagonal Elements are: ");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
if((i+j)==(3-1))
System.out.println(a[i][j]);
}
}
}
}