[Solved] Character Pattern 8 with Java, C++

Write a program to generate the following pattern Character Pattern 8.

*********

b******* b

bb***** bb

bbb***bbb

bbbb*bbbb

Input and Output Format:

Input consists of a single integer that corresponds to n, the number of rows. n is always an odd number.

Sample Input :

5

Sample Output :

*********

b******* b

bb***** bb

bbb***bbb

bbbb*bbbb

Solution

import java.io.*;
import java.util.*;
class Main
{
public
    static void main(String ar[])
    {
        Scanner sc = new Scanner(System.in);
        int i, n, j, l, k;
        n = sc.nextInt();
        for (i = n; i >= 1; i--)
        {
            for (j = n; j > i; j--)
            {
                System.out.print("b");
            }
            for (k = 2; k < 2*i + 1; k = k +1)
            {
                System.out.print("*");
            }
            for (l = n; l > i; l--)
            {
                System.out.print("b");
            }
            System.out.print("\n");
        }
    }
}
#include <iostream>
using namespace std;
int main()
{
    int n, i, j;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        for (j = 1; j < n * 2; j++)
        {
            if (i >= j || j >= n * 2 - i)
                printf("b");
            else
                printf("*");
        }
        printf("\n");
    }
    return (0);
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *