0% found this document useful (0 votes)
8 views

SE2 - JAVA - Part 3 - For loop

The document provides an overview of the Java for loop, including its syntax and usage for executing code multiple times. It contains various programming problems and examples demonstrating how to implement for loops to achieve specific outputs, such as displaying sequences of numbers and calculating sums. Additionally, it covers concepts like factorials, divisors, and perfect numbers through practical coding exercises.

Uploaded by

julianabrome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SE2 - JAVA - Part 3 - For loop

The document provides an overview of the Java for loop, including its syntax and usage for executing code multiple times. It contains various programming problems and examples demonstrating how to implement for loops to achieve specific outputs, such as displaying sequences of numbers and calculating sums. Additionally, it covers concepts like factorials, divisors, and perfect numbers through practical coding exercises.

Uploaded by

julianabrome
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java – SE2 for loop JAVA – Programming Language

 Java For loop


A For Loop statement allows us to execute a statement or group of statements
multiple times.

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.

Informatics / 2024 - 2025 1


Java – SE2 for loop JAVA – Programming Language

The example below will display the numbers 0 to 4:

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.

Informatics / 2024 - 2025 2


Java – SE2 for loop JAVA – Programming Language

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

Informatics / 2024 - 2025 3


Java – SE2 for loop JAVA – Programming Language

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 ");

Answer: 8 inside the loop + 1 outside the loop = 9

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 + ", ");
}

Informatics / 2024 - 2025 4


Java – SE2 for loop JAVA – Programming Language

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 + "/ ");
}

Informatics / 2024 - 2025 5


Java – SE2 for loop JAVA – Programming Language

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 + "/ ");
}

Informatics / 2024 - 2025 6


Java – SE2 for loop JAVA – Programming Language

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 + " ");
}

Update it to display the following +1 +2 +3 +4 *5 +6 +7 +8 +9 *10 +11 … *30

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);
}

Informatics / 2024 - 2025 7


Java – SE2 for loop JAVA – Programming Language

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);

Informatics / 2024 - 2025 8


Java – SE2 for loop JAVA – Programming Language

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

The Output of the program should be :


Enter a number: 5
5 != 120

Informatics / 2024 - 2025 9


Java – SE2 for loop JAVA – Programming Language

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

The Output of the program should be:

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

Informatics / 2024 - 2025 10

You might also like