Write a program using Java to print the following pattern. The number of rows should be obtained from the user into an integer variable
Sample input:
5
Sample output:
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
Answers
Answered by
1
Answer:
Question:-
- To print in Java the following pattern taking no. of rows as input.
Sample Input:
5
Sample Output:
12345
23451
34512
45123
51234
Code:-
import java.util.*;
class Code
{
public static void main (String ar [])
{
Scanner sc=new Scanner (System.in);
/*
*Solved By:-
*@swetank232894
*/
System.out.println("Enter no. of rows");
int n=sc.nextInt();
System.out.println("Pattern is as follows...");
for(I=1;I<=n;I++)
{
for(j=I;j<=n;j++)
{
System.out.print(j+" ");
}
for(j=1;j<I;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Variable Description:-
n--------Input variable to accept no. of rows from user
I---------Loop variable
j---------Loop variable
Output seen in terminal window:-
Enter no. of rows:
5
Pattern is as follows...
12345
23451
34512
45123
51234
Similar questions
History,
2 months ago
English,
2 months ago
Chemistry,
2 months ago
Hindi,
5 months ago
Social Sciences,
5 months ago
Math,
11 months ago
Social Sciences,
11 months ago
English,
11 months ago