Friday 18 August 2017

Write a program to accept the age of 20 students of a class. Re-arrange the data in ascending order (smallest value to largest value). Display the age of the youngest and eldest student in the class. Also display number of students in the class for each age present in the list.

import java.util.Scanner;

public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of students ");
int n=sc.nextInt();
System.out.println("Enter the age of students ");
int arr[]=new int[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
if(arr[i]>arr[j]) {
int temp=arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
}
}
}
System.out.println("Age in ascending order ");
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("");
System.out.println("age of the youngest student is "+arr[0]+" and age of the eldest student is "+arr[n-1]);

for(int i=0; i<n ;i++){
        int count =1;
        while((i < n-1 )&& (arr[i] == arr[i+1]))
        {
        count++;
        i=i+1;
        }
        System.out.println(" The number of students of " +arr[i]+" "+"is"+" " +count);
                         }
}
}

No comments:

Post a Comment