img

Pattern Programs In Java

This is the solution for the practice question set on the concept of star and number patterns in programming languages using for and while loops (iteration statements) and numeric concepts. 


You can access the question sheet here. Try solving these questions on your own before accessing this solution set.


Question 1



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=1;i<=5;i++)

        {

            for(j=1;j<=5;j++)

            {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}


Question 2



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=1;i<=5;i++)

        {

            for(j=1;j<=5;j++)

            {

                if(i==1||i==5||j==1||j==5)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            System.out.println();

        }

    }

}


Question 3



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=1;i<=5;i++)

        {

            for(j=1;j<=i;j++)

            {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}


Question 4



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=5;i>=1;i--)

        {

            for(j=1;j<=i;j++)

            {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}


Question 5



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=5;i>=1;i--)

        {

            for(j=1;j<=5;j++)

            {

                if(j>=i)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            System.out.println();

        }

    }

}


Question 6



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=1;i<=5;i++)

        {

            for(j=1;j<=i;j++)

            {

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

            }

            System.out.println();

        }

    }

}


Question 7



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        for(i=5;i>=1;i--)

        {

            for(j=1;j<=i;j++)

            {

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

            }

            System.out.println();

        }

    }

}


Question 8



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j,k=65;

        for(i=5;i>=1;i--)

        {

            for(j=1;j<=i;j++)

            {

                System.out.print((char)k+" ");

                k++;

            }

            System.out.println();

        }

    }

}


Question 9



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        String s = "ABCDE";

        for(i=0;i<s.length();i++)

        {

            for(j=0;j<=i;j++)

            {

                System.out.print(s.charAt(j)+" ");

            }

            System.out.println();

        }

    }

}


Question 10



CODE : 


class Main {

    public static void main(String[] args)

    {

        int i,j;

        String s = "AMARA";

        for(i=s.length()-1;i>=0;i--)

        {

            for(j=0;j<=i;j++)

            {

                System.out.print(s.charAt(j)+" ");

            }

            System.out.println();

        }

    }

}


Question 11



CODE : 


class Main {

    public static void main(String[] args)

    {

        int n=5,i,j;

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

        {

            for(j=1;j<=n-i;j++)

                System.out.print(" ");

            for(j=1;j<=i;j++)

            {

                if(j==1 || i==1 || i==5)

                    System.out.print("* ");

                else    

                    System.out.print(" ");

            }

            for(j=1;j<=n-i;j++)

            {

                if(j==n-i || i==1 || i==5)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            System.out.println();

        }

    }

}


Question 12



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 5;

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

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=i;j>=1;j--)

            {

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

            }

            for(j=2;j<=i;j++)

            {

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

            }

            System.out.println();

        }

    }

}


Question 13



CODE : 


class Main {

    public static void main(String[] args)

    {

        int sp,i,j;

        //first part

        for(i=1;i<=4;i++)

        {

            sp=8-(i*2);

            for(j=1;j<=i;j++)

                System.out.print("* ");

            for(j=1;j<=sp;j++)

                System.out.print(" ");

            for(j=1;j<=i;j++)

                System.out.print("* ");

            System.out.println();

        }

        //second part

        for(i=4;i>=1;i--)

        {

            sp=8-(i*2);

            for(j=1;j<=i;j++)

                System.out.print("* ");

            for(j=1;j<=sp;j++)

                System.out.print(" ");

            for(j=1;j<=i;j++)

                System.out.print("* ");

            System.out.println();

        }

    }

}


Question 14



CODE : 


class Main {

    public static void main(String[] args)

    {

        int sp,i,j;

        //first part

        for(i=1;i<=4;i++)

        {

            sp=8-(i*2);

            for(j=1;j<=i;j++)

            {

                if(j==1 || j==i)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            for(j=1;j<=sp;j++)

                System.out.print(" ");

            for(j=1;j<=i;j++)

            {

                if(j==1 || j==i)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            System.out.println();

        }

        //second part

        for(i=4;i>=1;i--)

        {

            sp=8-(i*2);

            for(j=1;j<=i;j++)

            {

                if(j==1 || j==i)

                    System.out.print("* ");

                else

                    System.out.print(" ");

            }

            for(j=1;j<=sp;j++)

                System.out.print(" ");

                for(j=1;j<=i;j++)

                {

                    if(j==1 || j==i)

                        System.out.print("* ");

                    else

                        System.out.print(" ");

                }

            System.out.println();

        }

    }

}


Question 15


CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 5;

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

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=i;j++)

            {

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

            }

            System.out.println();

        }

    }

}


Question 16



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 5;

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

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=i;j++)

            {

                System.out.print("* ");

            }

            System.out.println();

        }

    }

}


Question 17



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 4;

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

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=2*i-1;j++)

            {

                System.out.print("*");

            }

            System.out.println();

        }

        for(i=4;i>=1;i--)

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=2*i-1;j++)

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


Question 18



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,num,rem,n = 5;

        for(i=0;i<5;i++)

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            num=(int)Math.pow(11,i);

            while(num>0)

            {

                rem=num%10;

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

                num=num/10;

            }

            System.out.println();

        }

    }

}


Question 19



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 5;

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

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=i;j++)

            {

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

            }

            System.out.println();

        }

    }

}


Question 20



CODE : 


class Main

{

    public static void main(String[] args)

    {

        int i,j,n = 4, counter = 1;

        for(i=4;i>=1;i--)

        {

            for(j=1;j<=n-i;j++)

            {

                System.out.print(" ");

            }

            for(j=1;j<=i;j++)

            {

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

            }

            counter++;

            System.out.println();

        }

    }

}

post-img

Essential Steps to Build a Solid Foundat...

Master core programming concepts like loops, strings, and arrays with curated practice questions.

Read More

post-img

Strings: 10 Practice Problems and Soluti...

This is the solution for the practice question set on the concept of strings in programming languages.

Read More

post-img

Arrays: 10 Practice Problems and Solutio...

This is the solution for the practice question set on the concept of arrays in programming languages.

Read More

philomathiq

© 2023 philomathiq. All rights reserved.