ADVERTISEMENT
ADVERTISEMENT

Java find output programs (Loops) | set 3

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.
Submitted by Nidhi, on January 30, 2021

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

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

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

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.

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

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

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

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.

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

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.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.