Problem :3.10

 

Question 3.10:Write a program to print following using while loop:
i)
                      1
                    222
                  33333
                4444444

ii)
                        1
                      212
                    32123
                  4321234
                543212345


Solution:

1)

package com.company;
import java.util.Scanner;
public class Lab3Problem10i {
public static void main(String[] args)
{
    Scanner Obj = new Scanner(System.in);
    System.out.print("Enter the size: ");
    int n =Obj.nextInt();

    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=n-i;j++)
        {
            System.out.print(" ");
        }
        for(int k=1;k<i*2;k++)
        {
            System.out.print(i);

        }

        System.out.println();

    }
}
}






2) 


package com.company;
import java.util.Scanner;
public class Lab03Problem10b {
    public static void main(String[] args) {
        Scanner Obj = new Scanner(System.in);
        System.out.print("Enter the size: ");
        int n = Obj.nextInt();
        for (int i=0;i<=n;i++)
        {
            for(int j=0;j<n-i;j++)
            {
                System.out.print(" ");
            }
            for(int k=i;k>1;k--)
            {
                System.out.print(k);
            }
            for(int z=1;z<=i;z++)
            {
                System.out.print(z);
            }
            System.out.println();
        }
    }
}

Previous Post Next Post