Computer Science, asked by farooq96, 6 months ago

Write a program in java to input a positive integer and print the numeric pattern using the numbers from 1 to the inputted number in form of an Hourglass.​

Answers

Answered by Bᴇʏᴏɴᴅᴇʀ
8

Answer:-

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

Program to print Hourglass pattern in Java:-

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

\pink{\bigstar} \large\underline{\underline{\bf\green{C{o}de:-}}}

import java.util.*;

public class Hourglass

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of rows:");

//\sf\red{Taking \: value \: from \: the \: user}

int r = sc.nextInt();

System.out.println("The Pattern is::");

//\sf\red{Upper \: half \: of \: pattern}

for (int a = 1; a <= r; a++)

{

//\sf\red{Print \: 'a' \: spaces \: at \: beginning \: of \: each \: row}

for (int b = 1; b < a; b++)

{

System.out.print(" ");

}

//\sf\red{Print \: 'a' \: to \: rows \: value \: at \: end \: of \: each \: row}

for (int b = a; b <= r; b++)

{

System.out.print(b+" ");

}

System.out.println();

}

//\sf\red{Lower \: half \: of \: pattern}

for (int a = r-1; a >= 1; a--)

{

//\sf\red{Print \: 'a' \: spaces \: at \: beginning \: of \: each \: row}

for (int b = 1; b < a; b++)

{

System.out.print(" ");

}

//\sf\red{Print \: 'a' \: to \: rows \: value \: at \: end \: of \: each \: row}

for (int b = a; b <= r; b++)

{

System.out.print(b+" ");

}

System.out.println();

}

sc.close();

}

}

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

\pink{\bigstar} \large\underline{\underline{\bf\green{Out{p}ut:-}}}

★ Refer to attachment for Output.

★ Input value in the above Output is 10.

Attachments:
Similar questions