Back to all lessons


Lesson 6

For, while, do-while

Forever young, forever young!

Task 0

Create a for loop, which will forever increase and display an int. Then change type to byte.

Task 1

Create a program, which reads integers (as many as user wants). If user passes 0, program should display a sum of all given numbers. (You can not use an array.)

Task 2

Display powers of 2: 1, 2, 4, 8, 16, ..., 1024. Program should print numbers line by line. Create an integer with a value 1930 called myNumber. Use “&” operator on myNumber and powers of two. What do you see?

Task 3

Print a multiplication table for size n by n. Use '\t' as whitespace between numbers in the same line. Example for n = 5:


1  2   3   4   5
2  4   6   8  10
3  6   9  12  15
4  8  12  16  20
5 10  15  20  25
                           
            
Task 4

Write a program that display sum of digits of a number.

Task 5

Write a program that checks, if a number is a prime number.

Task 6

Write a program that prints a pattern like that:


   * 
  * * 
 * * * 
* * * *