Q:

Java find output programs (Loops) | set 3

0

Find the output of Java programs | Loops | Set 3: Enhance the knowledge of Java Loops concepts by solving and finding the output of some Java programs.

Question 1:

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

    int r1 = 0,
    r2 = 0;

    while (val > 0) {
      r1 = val % 10;
      val = val / 10;
      r2 = r2 * 10 + r1;
      loop++;
    }
    System.out.println(r);
  }
}

Question 2:

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

    int r1 = 0,
    r2 = 0;

    while (val > 0) {
      r1 = val % 10;
      val = val / 10;
      r2 = r2 * 10 + r1;
    }
    System.out.println(r2);
  }
}

Question 3:

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

    for (int i = 0; int j = 0; i < val; i++, j++) {
      System.out.println(i * j);
    }
  }
}

Question 4:

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

    for (int K = 1; K <= val; K++) {
      for (int L = 1; L <= K; L++)
      System.out.print(L + " ");
      System.out.println();
    }
  }
}

Question 5:

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

    for (int K = 1; K <= val; K--) {
      for (int L = 1; L <= (K * 2 - 1); L++)
      System.out.print("*");
      System.out.println();
    }
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

Main.java:15: error: cannot find symbol
    System.out.println(r);
                       ^
  symbol:   variable r
  location: class Main
1 error

Explanation:

The above program will generate syntax error because variable r is not declared in the above program but we used variable r in the below statement,

System.out.println(r);

Answer Question 2:

Output:

5321

Explanation:

In the above program, we declared three variables Val, r1, and r2 initialized with 1235, 0, and 0 respectively. Here, we find the reverse of variable Val. Here, r1 variable is used to get the remainder that is the last digit of the number. Then we divide val by 10 every time it will be divided until it becomes 0, and we got the final reversed number in the variable r2 and that will be printed on the console screen.

Answer Question 3:

Output:

Main.java:5: error: '.class' expected
    for (int i = 0; int j = 0; i < val; i++, j++) {
                        ^
Main.java:5: error: illegal start of expression
    for (int i = 0; int j = 0; i < val; i++, j++) {
                          ^
Main.java:5: error: ')' expected
    for (int i = 0; int j = 0; i < val; i++, j++) {
                           ^
Main.java:5: error: > expected
    for (int i = 0; int j = 0; i < val; i++, j++) {
                                      ^
Main.java:5: error: not a statement
    for (int i = 0; int j = 0; i < val; i++, j++) {
                                 ^
Main.java:5: error: ';' expected
    for (int i = 0; int j = 0; i < val; i++, j++) {
                                           ^
Main.java:5: error: ';' expected
    for (int i = 0; int j = 0; i < val; i++, j++) {
                                                ^
7 errors

Explanation:

The above program will generate syntax errors because of for loop, we did not declare variable j correctly.

The correct way is given below,

for (int i = 0,j = 0; i < val; i++, j++)

Answer Question 4:

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Explanation:

In the above program, we created a variable val initialized with 5. Here, we used a nested loop, where the outer loop is used for the number of rows printed, and the inner loop will print execute according to the value of K.

For K=1;
It will print 1

For K=2;
It will print 1 2

For K=3;
It will print 1 2 3

For K=4;
It will print 1 2 3 4

For K=5;
It will print 1 2 3 4 5

Then the loop will terminate.

Answer Question 5:

Output:

Infinite loop

Explanation:

The loop will execute infinite times because we used decrement operator with variable K that why condition will never false and the loop will not terminate.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now