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

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

bbbb*bbbb

bbb***bbb

bb***** bb

b******* b

*********

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 :

bbbb*bbbb

bbb***bbb

bb***** bb

b******* b

*********

Solution

import java.io.*;
import java.util.*;
class Main
{
public
    static void main(String ar[])
    {
        Scanner sc = new Scanner(System.in);
        int n, i, j, k, l;
        n = sc.nextInt();
        for (i = 1; i <= n; i++)
        {
            for (j = i; j < n; j++)
            {
                System.out.print("b");
            }
            for (k = 2; k < 2*i + 1; k = k +1)
            {
                System.out.print("*");
            }
            for (l = i; l < n; l++)
            {
                System.out.print("b");
            }
            System.out.print("\n");
        }
    }
}
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<n;j++)
        {
            cout<<"b";
        }
        for(int k=2;k<2*i+1;k++)
        {
            cout<<"*";
        }
        for(int l=i;l<n;l++)
        {
            cout<<"b";
        }
        cout<<endl;
    }
    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 *