Question

In: Computer Science

in java Write a program that will print the following output 1 1 2 1 1...

in java

Write a program that will print the following output

1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 18 8 4 2 1

Solutions

Expert Solution

class numberSeries
{    /***The given series is in format 1 121 12421 1248421 etc
     * Let n be the highest value in loop then the outer loop will execute n th root of 2 plus 1 times
     * Here n=32 nth root of 2=5 so outer loop will execute 5+1=6 times
     * There are 2 inner loops one for printing numbers in ascending order
     *  and one for printing numbers in descending order***/
     public static void main(String[] args)
 {
   //Declaring loop variables i and j and initialize i as 1
  int i=1,j;
  //Loop to print n terms of pattern
  while(i<=32)
  {
    //Inner loop to print numbers in increasing order 
    //After execution of each time j changes to j*2
   for(j=1;j<=i;j=j*2)
        //Display value of j
        System.out.print(j+" ");
    //Inner loop to print numbers in decreasing order 
    //After execution of each time j changes to j/2
    // starting value of j is i/2
    for(j=i/2;j>=1;j=j/2)
        System.out.print(j+" ");
    i=i*2;
  }
  }
}

OUTPUT

1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 16 8 4 2 1


Related Solutions

arduino programing Question 1: write an APL program that will print the following output on the...
arduino programing Question 1: write an APL program that will print the following output on the serial monitor 10 times only.   Output:  1 2 3 4 5 Question 2: write an APL program that will turn the traffic lights on and off by taking input from the Serial port working at 11500 bits processing rate. Use r or R for RED LIGHTS, y or Y for YELLOW LIGHTS and g or G for GREEN LIGHTS.    Question 3: write an APL...
. Write a program to print * in the following order using 2d array in java...
. Write a program to print * in the following order using 2d array in java                                              *             *             *             *             *                                              *             *             *             *                                              *             *             *                                              *             *                                                          *
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a Java program to print the pattern of asterisks shown below. For i=1 * For...
Write a Java program to print the pattern of asterisks shown below. For i=1 * For i=2 * * * For i=3 * ** * * * For i=n * * * * * * … … … * * * * * * ……… n
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to...
1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to 1000 which are divisible by 3, 5 and by both numbers. (b) Differentiate between instance and local variable in Java (c) In a tabular form, differentiate between instance and class variable
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {            public static void main(String args[])            {                   String place[]=new String[4];           place[0]="Salmaniya"; place[1]="Salmabad"; place[2]="Isa Town"; place[3] = “Manama”         System.out.println(place[3]);         System.out.println(place[1].toLowerCase());         System.out.println(place[2].substring(4,6);         System.out.println(place[3].charAt(4));         System.out.println(place[1].equals(place[2]));            } }    b. public class ChangeIt { public void doIt( int[] z ) { z[0] = 0; } } public class TestIt { public static void main ( String[] args ) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT