0% found this document useful (0 votes)
46 views10 pages

Nested For Loop

The document contains 8 code snippets that use for loops to print patterns. Each snippet takes user input for the number of rows (n) and uses nested for loops to print characters or symbols in a specific pattern based on the row and column counts.

Uploaded by

Zack Biden
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views10 pages

Nested For Loop

The document contains 8 code snippets that use for loops to print patterns. Each snippet takes user input for the number of rows (n) and uses nested for loops to print characters or symbols in a specific pattern based on the row and column counts.

Uploaded by

Zack Biden
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Q1

import java.util.*;

class Main {

    public static void main(String[] args) {

        

            // Fill your code here

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

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

            {

                for(int j=i;j<=n;j++)

                {

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

                }

                System.out.println("\n");

            }

        

     }

}
Q2

import java.util.*;

public class Main {

    public static void main(String[] args) {

        //fill your code here

        Scanner sc= new Scanner(System.in);

        int n = sc.nextInt();

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

        {

            for(int j= n-i+1;j>=1;j--)

            {

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

            }

            System.out.println("\n");

        }

    }

}
Q3

import java.util.*;

class Main {

    public static void main(String[] args) {

        

            // Fill your code here

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            for(int i=n;i>=1;i--)

            {

                for(int j=n;j>=n-i+1;j--)

                {

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

                }

                System.out.println("\n");

            }

        

     }

}
Q4

import java.util.*;

class Main {

    public static void main(String[] args) {

        

            // Fill your code here

            Scanner sc= new Scanner(System.in);

            int n = sc.nextInt();

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

            {

                for(int j=1;j<=n;j++)

                {

                    System.out.println("*");

                }

                System.out.println("\n");

            }

        

     }

}
Q5

import java.util.*;

import java.io.*;

class Main{

    public static void main(String[] args) throws Exception{ 

    

//      Fill your code

      Scanner sc = new Scanner(System.in);

      int n = sc.nextInt();

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

      {

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

          {

              System.out.print("*");

          }

          System.out.println("\n");

      }

    }

}
Q6

import java.util.*;

class Main {

    public static void main(String[] args) {

        

            // Fill your code here

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            for(int i=0;i<n/2-1;i++)

            {

                for(int j=0;j<n;j++)

                {

                    if(j>n/2-i-1 && j<n/2+i)

                    {

                        System.out.print("S");

                    }

                    else{

                        System.out.print("C");

                    }

                }

                System.out.println();

            }

            for(int i=0;i<2;i++)

            {

                for(int j=0;j<n;j++)

                {

                    System.out.print("S");
                }

                System.out.println();

            }

            for(int i=n/2-2;i>=0;i--)

            {

                for(int j =0;j<n;j++)

                {

                    if(j>n/2-i-1 && j<n/2+i)

                    {

                        System.out.print("S");

                    }

                    else{

                        System.out.print("K");

                    }

                }

                System.out.println();

            }

        

     }

}
Q7

import java.util.*;

class Main {

    public static void main(String[] args) {

        

            // Fill your code here

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            int a = (n+1)/2;

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

            {

                for(int j= 1 ;j<=n;j++)

                {

                    if(i == j && j == a)

                    {

                        System.out.print("*");

                    }

                    else

                    {

                        System.out.print("~");

                    }

                }

                System.out.println("\n");

            }

        

     }

}
Q8

import java.util.*;

class Main {

    public static void main(String[] args){

            Scanner sc =new Scanner(System.in);

            int n= sc.nextInt();

            int w = n*3/2;

            for(int i=0;i<n;i++)

            {

                for(int j=0;j<w;j++)

                {

                    if(j<n/2)

                    {

                        System.out.print("(");

                    }

                    else if(j<n)

                    {

                        System.out.print("#");

                    }

                    else{

                        System.out.print(")");

                    }

                }

                System.out.println();

            }

     }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy