Sample Questions - while loops (3 days)

What comes out?

1. x = 7;

     while  (x < 12)

        {System.out.print(x);

          x++;

        }

 

2. x = 5; y = 12;

    while (x < y)

         {System.out.print( x + y);

          x++;

         y--;

        }

 

3. sum = 40;

     while (sum > 10)

      {sum -= 7;

       System.out.print(sum);

      }

 

4. sum = 0;

     while (sum < 25)

       {sum + =4;

          System.out.print(sum);

      }

 

5. x = 15;  

     while (x > 4)

       {if (x % 2== 0)

          System.out.print(x);

        x--;

      }

 

6. x = 3; y = 8;

      while (x <= y)

        {System.out.print(x * y);

          x ++;

        }

 

7. x = 4; y= 7;

     while (x + y < 25)

       {System.out.print( x + y);

         x++;

        y++;

       }

 

8. x = 7;

     while ( x >= 3)

       {System.out.print(x * 2);

         x--;

        }

 

9. x = 8;

      while (x < 15){

         System.out.print(x + 3);

          x = x + 2;

       }

 

10. x = 15;

      while (x < 15){

           System.out.print( x);

           x-= 3;

         }

 

11. x =10; y = 5;

      while (x * y < 200)

         {System.out.print(x);

           x =x + 2;

          y = y + 2;

      }

 

12. sum = 50; x = 6;

       while (sum > 0)
{ sum = sum - x;

               x = x + 3;

          }

      System.out.print( sum);

 

13. power = 1;

       while (power < 100)
{power  * = 2;

            System.out.print( power);

          }

 

14. j = 1;

  while (j <= 10)
{System.out.println(j);

            j++;

          }

 

15. a = 2;

       while (a != 20)
{System.out.print(a);

             a *= 2;

            }

 

16. x = 15;

      while (x >= 2)

        {if (x % 3 ==0)

            System.out.print( x);

         x--;

      }

 

17. sum =0; x = 1;

         while (sum < 100)

            {sum += x;

              x++;

           }

        System.out.print(sum +" " +x);

 

18. x = 20;

        while ( x > 1)

           {if x % 3==0 || x % 5==0)

              System.out.print(x);

            x--;

          }

 

19. a= 5; b = 6;

      sum = 0;

      while (sum < 75)

       {sum = sum + a + b;

         a = a + 2;

        b = b + 3;

      }

 System.out.print( sum);

 

20. a = 48;

       b = 47;

       while (a % b != 0)

          b--;

        System.out.print(b);

 

21. Write a program that wiill let you enter a number and will then print out the multiple for that number under 1000.

 

22. Write a program that will let you enter numbers until you enter a -1 and will then print out the sum and the average of the numbers that you entered. 

 

23. A cell doubles every hour. If you start with 1 cell how many hours until you have 5000000 cells. Write a program that will tell you the answer.

 

24   Find LCM for 2 numbers entered.

 

25. Find GCF for 2 numbers entered.

 

26. Print out  first 5 factors of a number that is entered.

 

27. Print out 5 biggest factors of a number that is entered.

 

28.1/2 + 1/3 + 1/4 .... What denominator makes sum go over 4? Write a program to tell you the answer.

 

29. 1 + 4 + 7 + 10 ... What is last number that makes sum go over 1 million.

 

30. 2000 in bank. 5% interest a year. You withdraw 50 a year. How many years until you have 5000.

 

31. You sell house for 200000. You get 4% interest a year. You spend 14000 a year. How many years until you run out of money.

 

32. You start with 2 rabbits. They double every 14 days. How many days until you have 5000 rabbits.

 

 

 

            

        

     

 

 

 

 

 

   

 


HOME