Embed presentation
Download to read offline
![Write a java program that reads a number N as input and displays the following output using
stacks with parameters. Example o Input: Enter N: o Output: 5
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Stacks {
public static void main(String[] args) throws IOException {
// used variables
BufferedReader reader;
String input,outerSpaces,innerSpaces;
Integer number;
// taking input
System.out.print("Enter N : ");
reader = new BufferedReader(new InputStreamReader(System.in));
input = reader.readLine();
number = Integer.parseInt(input);
// printing upper stack
for(int i=1;i0;i--){
outerSpaces = getOuterSpaces(i);
innerSpaces = getInnerSpaces(number, i);
System.out.print(outerSpaces);
System.out.print(i);
System.out.print(innerSpaces);
if(i != number){
System.out.print(i);
}
System.out.print(outerSpaces);
System.out.println();
}
}](https://image.slidesharecdn.com/writeajavaprogramthatreadsanumbernasinputanddisplaysthe-230708181102-6b272f75/85/Write-a-java-program-that-reads-a-number-N-as-input-and-displays-the-pdf-1-320.jpg)

The program takes a number N as input, then uses stacks to print N lines of output. It prints the numbers from N to 1, with each number centered and surrounded by spaces that decrease by 2 each line. It uses methods to generate the outer and inner spaces for each line based on the line number and input N.
![Write a java program that reads a number N as input and displays the following output using
stacks with parameters. Example o Input: Enter N: o Output: 5
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Stacks {
public static void main(String[] args) throws IOException {
// used variables
BufferedReader reader;
String input,outerSpaces,innerSpaces;
Integer number;
// taking input
System.out.print("Enter N : ");
reader = new BufferedReader(new InputStreamReader(System.in));
input = reader.readLine();
number = Integer.parseInt(input);
// printing upper stack
for(int i=1;i0;i--){
outerSpaces = getOuterSpaces(i);
innerSpaces = getInnerSpaces(number, i);
System.out.print(outerSpaces);
System.out.print(i);
System.out.print(innerSpaces);
if(i != number){
System.out.print(i);
}
System.out.print(outerSpaces);
System.out.println();
}
}](https://image.slidesharecdn.com/writeajavaprogramthatreadsanumbernasinputanddisplaysthe-230708181102-6b272f75/85/Write-a-java-program-that-reads-a-number-N-as-input-and-displays-the-pdf-1-320.jpg)
