icse-promo

Question

Write a program to input and sort the weight of ten people. Sort and display them in descending order using the selection sort technique

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class program5
{
    public static void main(String[]args)
    {
        int i=0,j=0,temp=0;
        Scanner sc = new Scanner(System.in);
        int[] weights = new int[10];
        System.out.println("Enter weights: ");
        for (i = 0; i < 10; i++)
        {
            weights[i] = sc.nextInt();
        }
        for (i = 0; i < 10; i++)
        {

            for (j = i+1; j < 10; j++)
            {
                if (weights[i] < weights[j])
                {
                    temp = weights[i];
                    weights[i] = weights[j];
                    weights[j] = temp;
                }
            }

        }
        System.out.println("Sorted weights:");
        for (i = 0; i < 10; i++)
        {
            System.out.println(weights[i]);
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *