हिंदी

Write a program to generate a triangle or an inverted triangle till n terms based upon the User's choice of the triangle to be displayed. Example1:Input: Type 1 for a triangle and Type 2 for an - Computer Applications

Advertisements
Advertisements

प्रश्न

Write a program to generate a triangle or an inverted triangle till n terms based upon the User's choice of the triangle to be displayed.

Example1: Example2:
Input: Type 1 for a triangle and Type 2 for an inverted triangle Enter your choice 1 Enter the number of terms 5 Input: Type 1 for a triangle and Type 2 for an inverted triangle of alphabets Enter your choice 2 Enter the number of terms 5

Sample Output:

     * * * * *
       * * * *
         * * *
           * *
             *

Sample Output:

A B C D E
A B C D
A B C
A B
A

कोड लेखन

उत्तर

import java.util.*;
public class SQ22
{ public static void main(String args[])
{Scanner ob = new Scanner(System.in);
int i, j, ch, n, tms, p, space = 0;
System.out.println("****Menu Driven Program****");
System.out.println("Enter your choice");
System.out.println("1. for Triangle / 2. for Inverted Triangle");
ch = ob.nextInt();
System.out.println("Enter the number of terms");
tms = ob.nextInt();
System.out.println("Enter the type of triangle 1. Asterisk Triangle 2. Alphabet Triangle");
n = ob.nextInt(); 

if (ch == 1 && n == 1) // ASTERISK TRIANGLE
{    for (i = 0; i < tms; i++)
     {for (j = 0; j <= i; j++) 
     System.out.print("*");

     System.out.println():
     }
}
else if (ch == 1 && n == 2) // ALPHABET TRIANGLE
{    for (i = 65; i < (65 + tms); i++)
     { for (j = 65; j <= i; j++)
     System.out.print((char)j);
     System.out.println();
     } 
}
else if (ch == 2 && n == 1) // ASTERISK INVERTED TRIANGLE
{    for (i = tms; i > 0; i--)
     { for (j = i; j > 0; j--)
     System.out.print("*");

     System.out.println();
     space++;
     for (p = 0; p < space; p++)
     System.out.print(" ");
     }
}
else if (ch == 2 && n == 2) // ALPHABET- INVERTED TRIANGLE
{    space = 0;
     for (i = (65 + tms); i >= 65; i--)
     { for (j = 65; j<i; j++)
     System.out.print((char)j);

     System.out.println();
     }
}}}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 4: String Handling - EXERCISES [पृष्ठ २९२]

APPEARS IN

अविचल Computer Applications [English] Class 10 ICSE
अध्याय 4 String Handling
EXERCISES | Q IX. 22 | पृष्ठ २९२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×