In: Computer Science
1)Working of the code
source code
//Importing scanner class to accept user input import java.util.Scanner; public class Main { public static void main(String[] args) { //Creating Scanner object to read user input Scanner sc=new Scanner(System.in); //prompt user to enter name System.out.print("Enter name: "); //Reading the name String name=sc.nextLine(); //prompt user to enter age System.out.print("Enter age: "); //Reading the age int age=sc.nextInt(); //calling printName() method printName(name); //calling printAge() method printAge(age); } //printName() method definition public static void printName(String name) { //initializing variable i to 1 int i=1; //checking i is less than or equals to 20.if yes execute the loop body //if not, exit from the loop while (i<=20) { //printing name System.out.println(name); //incrementing i by 1 i++; } } //printAge() method definition public static void printAge(int age) { //initializing variable i to 1 int i=1; //checking i is less than or equals to age.if yes execute the loop body //if not, exit from the loop while (i<=age) { //printing i System.out.println(i); //incrementing i by 1 i++; } } }
Screen shot of the program
Screen shot of the output
2) Working of the program
Source code
public class Main { public static void main(String[] args) { //declare integer variable i and initialize with 1 int i = 1; //Execute loop body as long as i is less than or equal to 10 while (i <= 10) { //print multiple of i and put a space System.out.print(3 * i+" "); //increment i by 1 i++; } } }
Screen shot of the code
Screen shot of the output