SE2 - JAVA - Part 3 - For loop
SE2 - JAVA - Part 3 - For loop
Use For Loop when you know exactly how many times you want to loop through a
block of code.
Syntax
For (initialization; condition; iteration) {
// code block to be executed
}
• Initialization is executed (one time) before the execution of the
code block.
• Condition defines the condition for executing the code block.
• Iteration is executed (every time) after the code block has been
executed.
OutPut
0
1
2
3
4
Example explained
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has
been executed.
Problem 1
Write a program that displays the word "Java" 10 times.
int i;
for(i=1; i<=10; i=i+1) {
System.out.println("java");
}
Problem 2
How many times the word "Java" will be displayed?
Case 1
int i;
for(i=1; i<=10; i=i+3) {
System.out.println("Java ");
}
Answer: 4
Case 2
int i;
for(i=1; i<10; i=i+3) {
System.out.println("Java ");
}
Answer: 3
Case 3
int i;
for(i=20; i<10; i=i+1) {
System.out.println("Java");
}
Answer: 0
Case 4
int i;
for(i=1; i<=10; i=i-1) {
System.out.println("Java ");
}
Answer: infinite
Case 5
int i;
for(i=3; i<=10; i=i+1) {
System.out.println("Java ");
}
System.out.println("Java ");
Problem 3
Write a java program that displays the following sequence from 1 to 20 on the same
line, separated with a comma ,
1, 2, 3, 4, 5, 6, …. 19, 20
int i;
for(i=1; i<=20; i=i+1) {
System.out.print(i + ", ");
}
Problem 4
Write a java program that displays the following sequence from 5 to 50 on the same
line, separated with "-"
5 – 10 – 15 – 20 … 50
int i;
for(i=5; i<=50; i=i+5) {
System.out.print(i + "- ");
}
Problem 5
Write a program that displays the sequence of numbers from -10 to 10 on the same line
separated with "/".
-10 /-8 / -6 / -4 … 4 / 6 / 8 / 10
int i;
for(i=-10; i<=10; i=i+2) {
System.out.print(i + "/ ");
}
Problem 6
Write a program that displays all numbers from 200 to 0 on the same line separated
with a /.
200 /180 /160 /140 /120 /100 /80 /60 /40 /20 /0
int i;
for(i=200; i>=0; i=i-20) {
System.out.print(i + "/ ");
}
Problem 7
Part 1:
Write a program that enters an integer number, and then displays all numbers from 0 to
it on the same line separated with a space.
Enter a number: 16
0 1 2 3 4 5 .. 16
int a,i;
System.out.print("Enter a number: ");
a=scan.nextInt();
for(i=0; i<=a; i=i+1) {
System.out.print(i + " ");
}
Part 2:
Write a program that enters an integer number, and then displays the following se-
quence from 0 to it, on the same line separated with a space.
Enter a number: 16
0 3 6 9 12 15
int a,i;
System.out.print("Enter a number: ");
a=scan.nextInt();
for(i=0; i<=a; i=i+3) {
System.out.print(i + " ");
}
int a,i;
System.out.print("Enter a number: ");
a=scan.nextInt();
for(i=0; i<=a; i=i+1) {
if (i%5==0) System.out.print("*" + i);
else System.out.print("+" + i);
}
Problem 9
Write a program that enters an integer number and then displays all its divisors.
The output should be as the following form.
Enter a number: 20
1, 2, 4, 5, 10, 20
int i, a;
System.out.print("Enter a number: ");
a=scan.nextInt();
for(i=1; i<=a; i=i+1) {
if(a%i==0) System.out.print(i + " ");
}
Problem 10
Write a java program that enters an integer number then calculates the sum of numbers
from 1 to it.
- Example, if a=5, then the program should calculate the sum of 1,2,3,4 and 5.
S=1+2+3+4+5
- Example, if a=50, then the program should calculate the sum of 1,2,3,4 till 50.
The Output of the program should be:
Enter a number: 50
Sum = 1275
int a, i, s=0;
System.out.print("Enter a number: ");
a=scan.nextInt();
for(i=1; i<=a; i=i+1) {
s=s+i;
}
System.out.println("Sum = " + s);
Problem 11
Write a java program that enters an integer number (a), then calculates and displays
the result of the following sequence from 1 to a.
Note that "I" and "s" should be of type double.
𝟏 𝟏 𝟏 𝟏 𝟏
S= + + + +⋯
𝟏 𝟐 𝟑 𝟒 𝒂
Problem 12
Write a java program that enters an integer number and then displays its factorial.
Factorial is to multiply all numbers starting from 1 to the needed number.
Example: 5! = 1 * 2 * 3 * 4 * 5
Problem 13
Write a python program that calculates two numbers, "a and b" and then calculates
and displays a power b (ab).
• Example 53 = 5*5*5
• Example 64 = 6*6*6*6
Enter a number: 5
Enter the power: 3
5 power 3 = 125
Problem 14
Perfect number, a number that is equal to the sum of its proper divisors. The smallest
perfect number is 6, which is the sum of 1, 2, and 3.
Other perfect numbers are 28, 496, and 8128.
28 = 1 + 2 + 4 + 7 + 14
Write a java program that enters an integer number, then display if it is a perfect num-
ber or not.
The Output of the program should be:
Enter a number: 6
6 is a perfect number