Numbers and Asterisks – V Pattern

The program must accept an integer N as the input. The program must print the desired pattern as shown in the Example Input/Output section.

Boundary Condition(s):
2 <= N <= 100

Input Format:
The first line contains the value of N.

Output Format:
The lines containing the desired pattern as shown in the Example Input/Output section.

Example Input/Output 1:
Input:
3

Output:
1*2*3
*1*2*
**1**

Example Input/Output 2:
Input:
6

Output:
1*2*3*4*5*6
*1*2*3*4*5*
**1*2*3*4**
***1*2*3***
****1*2****
*****1*****

#include<stdio.h>

int main() {
    int n;
    scanf("%d", &n);

    for(int i = 1; i <= n; i++) {
        // Print leading asterisks
        for(int j = 1; j < i; j++) {
            printf("*");
        }

        // Print numbers and asterisks
        for(int j = 1; j <= n - i + 1; j++) {
            printf("%d", j);
            if(j != n - i + 1) {
                printf("*");
            }
        }

        // Print trailing asterisks
        for(int j = 1; j < i; j++) {
            printf("*");
        }
        printf("n");
    }
    return 0;
}
n = int(input())

for i in range(1, n+1):
    # Print leading asterisks
    print('*' * (i-1), end='')

    # Print numbers and asterisks
    for j in range(1, n - i + 2):
        print(j, end='')
        if j != n - i + 1:
            print('*', end='')

    # Print trailing asterisks
    print('*' * (i-1))
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    for(int i = 1; i <= n; i++) {
        // Print leading asterisks
        cout << string(i-1, '*');

        // Print numbers and asterisks
        for(int j = 1; j <= n - i + 1; j++) {
            cout << j;
            if(j != n - i + 1) {
                cout << '*';
            }
        }

        // Print trailing asterisks
        cout << string(i-1, '*') << endl;
    }
    return 0;
}
import java.util.Scanner;

public class Pattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        for(int i = 1; i <= n; i++) {
            // Print leading asterisks
            for(int j = 1; j < i; j++) {
                System.out.print("*");
            }

            // Print numbers and asterisks
            for(int j = 1; j <= n - i + 1; j++) {
                System.out.print(j);
                if(j != n - i + 1) {
                    System.out.print("*");
                }
            }

            // Print trailing asterisks
            for(int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Previous Article

Except the Nth Position

Next Article

Add Square of Each Digit to the Number

Write a Comment

Leave a Comment

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