Write a java program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed

Respuesta :

Answer:

Here is code in java.

import java.util.*;

class Number

{   //main method of the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

       // create an array of size 50 of double type

       double[] alpha = new double[50];

        //initialize array

       for(int x = 0; x< 50; x++)

               {

               if(x<25)

               {

                   alpha[x]= x * x;

               }

               else

               {

                   alpha[x]= 3 * x;

               }

            }

            //print 10 elements per line

            for(int y=1;y<=50;y++)

            {

                System.out.print(alpha[y-1] +" ");

                if(y%10==0)

                {

                    System.out.println();

                }

            }

   }catch(Exception ex){

       return;}

}

}

Explanation:

Create an array size 50 of double type.initialize first 25 elements as square

of the index variable and rest 25 as three times the index variable.Then print

the elements of array using for loop, if there is 10 elements in a line then

print a newline.

Output:

0.0 1.0 4.0 9.0 16.0 25.0 36.0 49.0 64.0 81.0

100.0 121.0 144.0 169.0 196.0 225.0 256.0 289.0 324.0 361.0

400.0 441.0 484.0 529.0 576.0 75.0 78.0 81.0 84.0 87.0

90.0 93.0 96.0 99.0 102.0 105.0 108.0 111.0 114.0 117.0

120.0 123.0 126.0 129.0 132.0 135.0 138.0 141.0 144.0 147.0

The program is an illustration of loops and conditional statements.

Loops are used to perform repetitive operations, while conditional statements are statements whose execution depends on the truth value of the condition.

The program in Java is as follows:

public class Main{

public static void main (String[] args) {

   //This declares the array of 50 elements

   double[] alpha = new double[50];

   //This iterates through the array

   for(int i = 0; i< 50; i++){

       //For the first 25 indices

       if(i<25){

           //The array is populated by the square of the index

           alpha[i]= i * i;

       }

       //For the other 25 elements

   else{

       //The array is populated by the three times the index

       alpha[i]= 3 * i;

       }

   }

   //This iterates through the array elements

   for(int i=0;i<50;i++){

       //This prints the array elements

       System.out.print(alpha[i] +" ");

       //This if condition ensures that 10 elements are printed on a line

       if((i+1)%10==0){

           System.out.println();

               }

           }

}

}

At the end of the program, 10 elements of the array are printed on a line.

Read more about similar programs at:

https://brainly.com/question/13026985

ACCESS MORE