10.5k views
2 votes
Write a for loop that displays your name and student number 12 times.

Example:

Mary Kaur ----- 123456

1 Answer

1 vote

Answer:

// here is code in java.

import java.util.*;

// class definition

class Solution

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// scanner object

Scanner s=new Scanner(System.in);

// variables

String s_name;

int s_num;

System.out.print("Please enter the name:");

// read the student name

s_name=s.nextLine();

System.out.print("Please enter the number:");

// read the student number

s_num=s.nextInt();

// print name and number 12 times

for(int i=0;i<12;i++)

{

System.out.println(s_name+"----"+s_num);

}

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Create a scanner class object to read input from user.Read the student name and the number from user and assign them to variable "s_name" and "s_num".Print the student name and number 12 times with the help of for loop.

Output:

Please enter the name:Mary Kaur

Please enter the number:123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

Mary Kaur----123456

User Ali Yar Khan
by
5.4k points