0% found this document useful (0 votes)
4 views3 pages

SelectionSortDescending and DDA

The document contains three Java programs: one for sorting an array of numbers in descending order using selection sort, another for printing the left diagonal elements of a 3x3 matrix, and a third for printing the right diagonal elements of the same matrix. Each program prompts the user for input and displays the results accordingly. The code is structured with appropriate loops and conditions to achieve the desired functionality.
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)
4 views3 pages

SelectionSortDescending and DDA

The document contains three Java programs: one for sorting an array of numbers in descending order using selection sort, another for printing the left diagonal elements of a 3x3 matrix, and a third for printing the right diagonal elements of the same matrix. Each program prompts the user for input and displays the results accordingly. The code is structured with appropriate loops and conditions to achieve the desired functionality.
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/ 3

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]);
}
}
}
}

You might also like