in java pls
Write a method that will receive 2 numbers as parameters and will return a string containing the pattern based on those two numbers. Take into consideration that the numbers may not be in the right order.
Note that the method will not print the pattern but return the string containing it instead. The main method (already coded) will be printing the string returned by the method.
Remember that you can declare an empty string variable and concatenate everything you want to it.
As an example, if the user inputs "2 5"
The output should be:
2 3 3 4 4 4 5 5 5 5
input: 8 3
output:
3
4 4
5 5 5
6 6 6 6
7 7 7 7 7
8 8 8 8 8 8
//TestCode.java import java.util.Scanner; public class TestCode { public static String getPattern(int x, int y){ if(x > y){ int temp = x; x = y; y = temp; } String result = ""; int k = 1; for(int i = x;i<=y;i++){ for(int j = 0;j<k;j++){ result += (i+" "); } k++; result += "\n"; } return result; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); int x,y; x = scan.nextInt(); y = scan.nextInt(); System.out.println(getPattern(x,y)); } }
Get Answers For Free
Most questions answered within 1 hours.