AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Program which displays a pyramid

Write a program that declares and initializes an integer with a value between 1 and 10 and displays a pyramid as shown below. The pyramid below is the output if the integer is initialized to 7.
Your code should print the correct pyramid for all possible values of the integer.
7
6 7 6
5 6 7 6 5
4 5 6 7 6 5 4
3 4 5 6 7 6 5 4 3
2 3 4 5 6 7 6 5 4 3 2
1 2 3 4 5 6 7 6 5 4 3 2 1

corejava x 351
programming x 167
Posted On : 2018-02-21 21:42:44.0
profile Braah Alhourani - anyforum.in Braah Alhourani
800
up-rate
3
down-rate

Answers


public class Pyramid {
public static void main(String[] args) {
int x=7;
for(int i=0;i<x;i++){
for(int j=x-i;j<=x;j++){
System.out.print(j+" ");
}
for(int k=x-1;k>=x-i;k--){
System.out.print(k+" ");
}
System.out.println("");
}

}
}

Posted On : 2018-02-22 22:47:13
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129025397
Reply This Thread
up-rate
0
down-rate

// Java Program for this types of pyramid for all types of integer value
import java.util.*;
class PyramidQ{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if(n<0 || n==0){
System.out.println("Invalid Input");
}else{
for(int i=n;i>=1;i--){
for(int j=i;j<=n;j++){
System.out.print(j+" ");
}
for(int k=n-1;k>=i;k--){
System.out.print(k+" ");
}
System.out.println("");
}
}
}
}

Posted On : 2018-02-25 05:20:01
Satisfied : 1 Yes  0 No
profile Rohit Kumar - anyforum.in Rohit Kumar
040
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in