Page 364 - ComputerScience_Class_11
P. 364

The output of the preceding program is as follows:
                      BlueJ: Terminal Window - Java

                   Options
                  x = 2.3333333333333335

                  y = 1.6666666666666667





               Some More Programs                                                   #Creativity & Innovativeness
                                                                                    #Coding & Computational Thinking


                Program 1      Write a Java program to input and store n integers (n > 0) in a single subscripted variable and
                               print each number with its frequency. The output should contain number and its frequency
                               in two different columns.
                1       import java.util.*;
                2       class NumberFrequency

                3       {
                4           public static void main(String args[])
                5           {
                6               Scanner sc = new Scanner(System.in);
                7               int n, i, j, count;
                8               System.out.print("Enter the size of the array: ");
                9               n = sc.nextInt();

                10              if (n <= 0)
                11              {
                12                  System.out.println("Invalid Input! n should be greater than 0.");
                13                  return;
                14              }
                15              else
                16              {
                17                  int freq[] = new int[n];
                18
                19                  System.out.println("Enter elements in the array one by one: ");
                20                  for (i = 0; i < n; i++)

                21                  {
                22                      freq[i] = sc.nextInt();
                23                  }
                24                  //Sort the array
                25                  for (i = 0; i < n - 1; i++)
                26                  {
                27                      for (j = 0; j < n - i - 1; j++)





                  362  Touchpad Computer Science (Ver. 3.0)-XI
   359   360   361   362   363   364   365   366   367   368   369