Write this code shown here as pseudocode in Java n 10 while

Write this code, shown here as pseudocode, in Java. n = 10 while (n > 1) print n if (n is even) then n = n/2 else n = 3*n + 1//end of while loop Write out Java code that generates the following number sequence using a while loop: (This exercise is related very closely to one that you did in Homework #5) 1 12 3 5 8 13 21 34 55 89 144 233

Solution

Question 5:

WhileLoop.java


public class WhileLoop {

  
   public static void main(String[] args) {
       int n = 10;
       while(n > 1){
           System.out.println(n);
           if(n % 2 == 0){
               n = n /2;
           }
           else{
               n = 3 * n + 1;
           }
       }

   }

}

Question b)

FibonaciiSeries.java


public class FibonaciiSeries {

   public static void main(String[] args) {
       int n = 13;
      
       int a = 1;
       int b= 1;
       System.out.print(a+\" \"+b+\" \");
       while(n - 2 > 0){
           int c = a + b;
           System.out.print(c+\" \");
           a=b;
           b=c;
           n--;
       }
      
   }

}

Output:

1 1 2 3 5 8 13 21 34 55 89 144 233

 Write this code, shown here as pseudocode, in Java. n = 10 while (n > 1) print n if (n is even) then n = n/2 else n = 3*n + 1//end of while loop Write out J

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site