0% found this document useful (0 votes)
20 views3 pages

Training For

Uploaded by

loltwitch1997
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)
20 views3 pages

Training For

Uploaded by

loltwitch1997
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/ 3

Training ( 3 (

Question 1

class Main {

public static void main(String[] args) {

int n = 4;

for (int i = 1; i <= n; ++i) {

System.out.println("Java is fun");

}}}

how this program works.

Iteration Variable Condition: i Action

i = 1 Java is fun is printed.


1st true
n = 5 i is increased to 2.

i = 2 Java is fun is printed.


2nd true
n = 5 i is increased to 3.

i = 3 Java is fun is printed.


3rd true
n = 5 i is increased to 4.

i = 4 Java is fun is printed.


4th true
n = 5 i is increased to 5.

Output

Java is fun

Java is fun

Java is fun

Java is fun
Question 2

public class Main {

public static void main(String[] args) {

for (int i = 0; i <= 10; i = i + 2) {

System.out.println(i);

Question 3

public class Main {

public static void main(String[] args) {

for (int i = 1; i <= 2; i++) {

System.out.println("Outer: " + i);

for (int j = 1; j <= 3; j++) {

System.out.println(" Inner: " + j);

}
Question 4
Write a program to print numbers from 1 to 10.

public class PrintNumbers


{
public static void main(String[] args)
{
for(int i=10 ; i>=1; i--)
{
System.out.println(i);
}
}
}

Question 5
Write a program to calculate the sum of first 10 natural number.

public class SumNumbers


{
public static void main(String[] args)
{
int sum = 0;
for(int i=1; i<=10; i++)
{
sum += i;
}
System.out.println("Sum: " + sum);
}
}

Question 6

Use a for loop to print "Yes" 5 times.

(int i = 0; i < 5; ) {
System.out.println( );
}

You might also like