Sunday, March 22, 2020

Patterns Problems


Question 1- Write a program to print following pattern –
Enter the number of lines you want: 5
1
22
333
4444
55555
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    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(i);
        }
        System.out.println();
    }
}
}


Question 2-  Write a program to print following pattern –
Enter the number of lines you want: 5
1
12
123
1234
12345
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    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(j);
        }
        System.out.println();
    }
}
}


Question 3- Write a program to print following pattern –
Enter the number of lines you want: 5
*
**
***
****
*****
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    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();
    }
}
}


Question 4- Write a program to print following pattern –
Enter the number of lines you want: 5
1
21
321
4321
54321
import java.util.*;
class pattern
{
public static void main(String args[])
{
    System.out.print("Enter the number of lines you want: ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j>=1;j--)
        {
            System.out.print(j);
        }
        System.out.println();
    }
}
}

No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...