1) Write a java programming nested while loop where you will declare two numbers one for outer loop and the other one for inner while loop, and display the result. (Try using scanner)
Steps: 1) Declare the variable of outer loop int and assign value of your choice
2) Declare while condition which outer variable is less the number of your choice
3) Declare variable of inner loop int and assign the value of your choice
4) Declare while condition where inner variable is less the number of your choice
5) Print the outer and inner value
6) Increment inner by 1
7) Increment outer by 1
Implement the program as follows:
Program: Nested_While.java
import java.util.Scanner; /* import Scanner class */
public class Nested_While {
public static void main(String[] args){ /* define main() method */
Scanner input = new Scanner(System.in); /* Initialize Scanner object, input */
int m,n; /* declare variables m, n */
System.out.print("Enter the number of Outer while loop executions : ");
m = input.nextInt(); /* Read value to the variable, m */
System.out.print("Enter the number of Inner while loop executions : ");
n = input.nextInt(); /* declare value to the variable n */
int i; /* Declare the variable of outer loop int, i */
int j; /* Declare the variable of inner loop int, j */
i = 0; /* assign value 0 to i */
while(i<m){ /* Declare while condition which outer variable, i is less the number m */
j=0; /* assign value 0 to j */
while(j<n){ /* Declare while condition which inner variable, j is less the number n */
System.out.println("i = " + i + ", j = " + j); /* Print the outer value, i and inner value, j */
j++; /* increment inner value j by 1 */
}
i++; /* increment outer value i by 1 */
}
}
}
Screenshot:
Output:
Please don't forget to give a Thumbs Up.
Get Answers For Free
Most questions answered within 1 hours.