Java find output programs (Loops) | set 1

Find the output of Java programs | Loops | Set 1: Enhance the knowledge of Java Loops concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on January 30, 2021

Question 1

public class Main {
  public static void main(String[] args) {
    int i = 0;
    int num = 0;

    while (i < 5) {
      num = (++i * i++*2 + 1);
      System.out.println(num);
    }
  }
}

Output

3
19
51

Explanation

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point of the program.

In the main() method, we created two local variables i and num that are initialized with 0. Here, we used a while loop, that will execute till the value of the variable i is less than 5.

Now look to iterations:

Iteration1:
i=0, num=0 and condition is true.
num = 1*1*2+1;
num = 2+1;
num = 3;
Then variable 'i' becomes 2 because of pre and post increment operator.

Iteration2:
i=2, num=3 and condition is true.
num = 3*3*2+1;
num = 18+1;
num = 19;
Then variable 'i' becomes 4 because of pre and post increment operator.

Iteration3:
i=4, num=19 and condition is true.
num = 5*5*2+1;
num = 27+1;
num = 29;

Then variable 'i' becomes 6 and 
condition will be false and program gets terminated.

Question 2

public class Main {
  public static void main(String[] args) {
    int i = 0;
    int num = 0;

    while (i < 5) {
      num += (++i * i + Math.pow(i, 2));
      System.out.println(num);
    }
  }
}

Output

2
10
28
60
110

Explanation

In the above program, we created a class Main that contains a static method main(). The main() method is the entry point of the program.

In the main() method, we created two local variables i and num that are initialized with 0. Here, we used a while loop, that will execute till the value of the variable i is less than 5.

Now look to iterations:

i=0, num=0 and condition is true. Variable 'i' will increase 
using pre-increment operator then it will be 1.

num += (1*1+1);
num += (1+1);
num = num + 2;
num = 0+2;
num = 2;
Then variable 'i' becomes 1 and num will 2.
Iteration2:
i=1, num=2 and condition is true. Variable 'i' will increase 
using pre-increment operator then it will be 2.

num += (2*2+4);
num += 8;
num = 2 + 8;
num = 10;

Iteration3:
i=2, num=10 and condition is true. Variable 'i' will increase 
using pre-increment operator then it will be 3.

num += (3*3+9);
num += 18;
num = 10 + 18;
num = 28

Iteration4:
i=3, num=28 and condition is true. Variable 'i' will increase 
using pre-increment operator then it will be 4.

num += (4*4+16);
num += 32;
num = 28 + 32;
num = 60;

Iteration4:
i=4, num=60 and condition is true. Variable 'i' will increase 
using pre-increment operator then it will be 5.

num += (5*5+25);
num += 50;
num = 60 + 50;
num = 110;
Then condition will false and loop gets terminated.

Question 3

public class Main {
  public static void main(String[] args) {
    byte val = 260;

    while (val < 256) {
      System.out.println("india");
      val++;
    }
  }
}

Output

Main.java:3: error: incompatible types: possible lossy conversion from int to byte
    byte val = 260;
               ^
1 error

Explanation

The above program will generate syntax error because the highest value of the byte variable is 255 but we assigned 260 which is the out of the limit of the byte that's why error gets generated.

Question 4

public class Main {
  public static void main(String[] args) {
    int LOOP = 0;

    while (false == false) {
      if (++LOOP == 5) break;
      System.out.println("India");
    }
  }
}

Output

India
India
India
India

Explanation

The above program will print "India" 4 times on the console screen. Here we created a variable LOOP initialized with 0.

while (false==false)

In the above-given loop condition, we checked "false==false" then the condition will always true, but we used a break statement to terminate the loop. We increased the value of the variable LOOP using pre-increment operator when the value of the variable LOOP reaches to 5, then the loop will be terminated.

Question 5

public class Main {
  public static void main(String[] args) {
    int val = 0;

    while (1) {
      if (++val == 5) break;
      System.out.println("India");
    }
  }
}

Output

Main.java:5: error: incompatible types: int cannot be converted to boolean
    while (1) {
           ^
1 error

Explanation

The above program will generate syntax error because we cannot use int value in the condition of the while loop, in Java we need to use the Boolean value in while loop condition.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.