Sample Questions - For Loops (5 days)

 

What comes out?

1. for (int x = 12; x > 3; x-=2)

      System.out.print( x);

 

2.  for (int x = 3; x < 11 ; x = x + 2)

      System.out.print( x);

 

3.  for (int x = 20; x  >5 ; x -= 3)

      System.out.print( x);

 

4.  for (int y= 9; y != 16 ; y = y + 2)

      System.out.print( y);

 

5. for (int x= 7 ; x > 5 ; x--)

       System.out.print( "Tom");

 

6.  for (int x = 8; x >= 0 ; x = x - 2)

      System.out.print( "Pete");

 

7.  for (int x = 7; x > 11 ; x ++)

      System.out.print( x);

 

8.  int sum =0;

     for (int a = 1; a <= 3; a++)

     {sum = sum + a;

      System.out.print( sum);

     }

 

9.  int sum =0;

     for (int a = 1; a <= 3; a++)

     {sum = sum + a;

     }

      System.out.print( sum );

     

10.  int sum =0;

     for (int a = 1; a <= 3; a++)

     {System.out.print(sum);

      sum = sum + a;

     }

 

11.   for (int a = 1; a <= 3; a++)

           { sum =0;

             sum = sum + a;

              System.out.print( sum );

              }

 

12. for (int x=11; x > 3; x--)

        {System.out.print( "Tom");

          System.out.print( "Pete");

          System.out.println() ;

        }

 

13. sum = 0;

      for (int x=1; x <= 7; x += 2)

          sum += x;

      System.out.print( sum);

 

14. sum = 0;

      for (int x=1; x <= 7; x += 2)

         {     sum += x;

                System.out.print( sum);

          }

 

15. sum = 20;

      for (int x=2;x<= 8;x = x + 2)

         sum -= x;

      System.out.print( sum);

 

16. for (int x=1; x <= 25; x ++)

          if (x % 5==0 || x % 7 == 0)

System.out.println( x );

 

17. sum = 0;

       for (int x= 1 ; x <= 25 ; x++)

          if (x % 9==0)

           sum = sum + x;

       System.out.print( sum);

 

18. sum = 0;

       for (int x= 1 ; x <= 25 ; x++)

          if (x % 9==0)

        {   sum = sum + x;

          System.out.print( sum);

        }

 

19. for (int x=1; x <= 50; x++)

        if (x % 7==0 && x < 20)

System.out.print( x );

 

20. for (int x=1; x <= 30 ; x++)

        if (x % 2==0 && x % 7 ==0)

System.out.print( x) ;

 

21. Write a program that will print out pattern

    2 4 6 8 10 12 ->20

 

22. Write a program that will print out pattern

   100 95 90 85 -> 5

 

23. Write a program that will let you enter 2 numbers and will then print out all the numbers between the 2 numbers.

 

24. Write a program that will let find the sum of the numbers between 1 and 100.

 

25. Write a program that will let you enter a number and will then print out the sum of the numbers between 1 and that number.

 

26. Write a program that will print out all the numbers between 1 and 1000 that 7 or 5 go into evenly.

 

27. Write a program that will print out the sum for all the numbers between 1 and 1000 that 7 and 5 go into evenly.

 

28. Write a program that will let you enter a number and will then print out first 50 multiples for that number.

 

29. Write a program that will let you enter 50 numbers and will then print out the sum and the average for those numbers.

 

30. Write a program that will find the sum for 1/2 + 1/3 + 1/4 ... 1/100

 

31. In 1626 the Dutch bought Manhattan for $24. How much would they have today if they invested it at 4% compounding interest.

 

32. Write a program that will find the sum of the odd numbers between 1 and 100.

 

33. Write a program that will let you enter 2 numbers and will then print out the GCF for those numbers.

 

34. Write a program that will let you enter 2 numbers and will then print out the LCM for those numbers.

 

35. Write a program that will let you enter a number and will then print out he factors for that number.

 

36. Write a program that will tell you how much money you will have after 20 years if you start with 2000 and get 5% interest a year and withdraw 120 a year.

 

37. Write a program that will let you enter your name and then enter a number and will then print out the name that many times.

 

38. Write a program that will let you enter 20 numbers and will then print out the biggest and smallest numbers that were entered.

 

39. Write a program that will let you enter a base, then enter an exponent and will print out that number raised to that power.

 

40. Write a program that will let you enter a number and will then print out the factorial for that number. 

Ex. 

 Enter a number 5

Factorial is 120

(because 5 * 4 * 3 * 2 * 1 = 120)

 

 

 

 

 

 

 

 

 

 

 

   

 


HOME