JAVA I will be testing through eclipse Question: Triangle Pattern public static
ID: 669986 • Letter: J
Question
JAVA I will be testing through eclipse
Question: Triangle Pattern
public static void triangle(int m, int n)
// Precondition: m <= n
// Postcondition: The method has printed a pattern of 2*(n-m+1) lines
// to the standard output. The first line contains m asterisks, the next
// line contains m+1 asterisks, and so on up to a line with n asterisks.
// Then the pattern is repeated backwards, going n back down to m.
/* Example output:
triangle(3, 5) will print this:
***
****
*****
*****
****
***
*/
Hint: Only one of the arguments changes in the recursive call. Which one?
(
Explanation / Answer
class Pyramid
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print("*");
System.out.print(" ");
}
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print("*");
System.out.print(" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.