[Question 5] ICSE 2016 Computer Application Solved - Pattern

Question 5.

Using the switch statement, write a menu driven program for the following:
(i) To print the Floyd’s triangle [Given below]
1
2  3
4  5  6
7  8  9  10
11 12 13 14 15
(ii) To display the following pattern
I
I C
I C S
I C S E
For an incorrect option, an appropriate error message should be displayed.

SOURCE CODE


import java.io.*;
class ICSE2016_Ques5_Pattern
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Press 1 to print Floyd's triangle");
        System.out.println("Press 2 to print second pattern");
        int n=Integer.parseInt(br.readLine());
        switch(n)
        {
            case 1:
            System.out.print("Enter a number  :");
            int m=Integer.parseInt(br.readLine());
            int a=1;
            for(int i=1;i<=m;i++)
            {
                for(int j=1;j<=i;j++,a++)
                {
                    System.out.print(a+" ");
                }
                System.out.println();
            }
            break;
            case 2:
            System.out.print("Enter a word  :");
            String st=br.readLine();
            int l=st.length();
            for(int i=0;i<l;i++)
            {
                for(int j=0;j<=i;j++)
                {
                    System.out.print(st.charAt(j));
                }
                System.out.println();
            }
            break;

            default:
            System.out.println("Wrong choice");
        }
    }
}

OUTPUT




If you have any question then leave a comment below I will do my best to answer that question.



Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.