Computer Science, asked by levin647, 2 months ago

10. Write a program to generate a triangle or an inverted triangle till n terms based upon the user's choice.

Example 1:
Input: Type 1 for a triangle and
Type 2 for an inverted triangle
Enter your choice 1
Enter the number of terms 5
Sample Output:
1
22
3 3 3
4 4 4 4
5 5 5 5 5

Answers

Answered by kamalrajatjoshi94
6

Answer:

Program:-

import java.util.*;

public class Triangle

{

public static void main(String args[ ])

{

Scanner in=new Scanner(System.in);

int a,b,ch,n;

System.out.println("Enter 1 for right angled triangle");

System.out.println("Enter 2 for inverted triangle");

System.out.println("Enter your choice");

ch=in.nextInt();

switch(ch)

{

case 1:

System.out.println("Right angular pattern");

System.out.println("Enter the number of terms");

n=in.nextInt();

for(a=1;a<=n;a++)

{

for(b=1;b<=a;b++)

{

System.out.print(a+ " ");

}

System.out.println();

}

break;

case 2:

System.out.println("Inverted angular pattern");

System.out.println("Enter the number of terms");

n=in.nextInt();

for(a=n;a>=1;a--)

{

for(b=1;b<=a;b++)

{

System.out.print(a+ " ");

}

System.out.println();

}

break;

default:

System.out.println("Invalid choice");

}

}

}

Similar questions