Ask Question
4 November, 14:39

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

+1
Answers (1)
  1. 4 November, 16:27
    0
    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
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “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 ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers