1
333
55555
7777777
999999999
Program to print number pyramid. I want to print this pattern in Java.
My code:
private static void pyramid() {
System.out.println("Please Enter any number less than 10 : ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int temp = num;
for (int row = 0; row <= num; row++) {
for (int column = 0; column < temp; column++) {
System.out.print(" ");
}
temp--;
for (int k = 0; k <= row-1; k++) {
if (row % 2 != 0) {
System.out.print(row);
}
System.out.println();
}
}
}
And I am getting following output:
Please Enter any number less than 10 :
9
1
3
3
3
5
5
5
5
5
7
7
7
7
7
7
7
9
9
9
9
9
9
9
9
9
System.out.println(" 1");
System.out.println(" 333");
System.out.println(" 55555");
System.out.println(" 7777777");
System.out.println("999999999");
Your teacher probably wants you to use loops.
So you should note that the bulk standard for loop which programmers can bang out with their eyes closed:
for (int i = 0; i < someNumber; i++) {
//..
}
Is fairly configurable. For instance the i++ at the end means to increment i, but we could increment by bigger amounts (or decrement, or do some other funky things like stepping through a list of objects and so on and so forth).
e.g.
i += 3;
Will increase i by three.
You can also nest loops inside one another, e.g.
for (int i = 1; i < 10; i+=2) {
String s = "";
for (int j = 0; j < i; j++) {
s += i;
}
System.out.println(s);
}
The padding at the front I leave as an exercise to the reader.
Note that this pattern (one loop inside another, and the inner loop being bounded by the outer loops counter) is actually quite common 'out in the wild', and so is worth investing the time to understand.
Related
Below is the code I'm messing with, pretty basic. I was looking for a way to print only specific elements of the array. For example, if I wanted to print only the element at index 1 of array[i], as well as the element at index 1 of array[j] when its value is 1. See below for the output I'm looking for.
Expected output :
1 3 5
4
7 8 9
Code:
public class multiDimensional {
public static void main(String args[]){
int arr[][] = {{1,3,5}, {2,4,6}, {7,8,9}};
for(int i=0; i < 3; i++){
for(int j=0; j < 3; j++){
System.out.print(arr[i][j]+" ");
//System.out.println();
}
System.out.println();
}
}
}
Actual output :
1 3 5
2 4 6
7 8 9
You can produce your expected output by using an if statement to decide when to write a value:
public static void main(String args[]){
int arr[][] = {{1,3,5}, {2,4,6}, {7,8,9}};
for(int i=0; i < 3; i++){
for(int j=0; j < 3; j++){
if (i == 0 // in the first row
|| i == 2 // in the last row
|| j == 1) { // in the middle column of the middle row
System.out.print(arr[i][j]+" ");
} else {
System.out.print(" "); // this is here to keep the spacing right
}
//System.out.println();
}
System.out.println();
}
}
Note: There are many other ways of coding this, however the approach I am showing is merely to demonstrate how the if statement works.
I want to create a matrix of size N by N where N is a constant value defined globally, for now I just want to create a matrix where N=6. Where I fall short is I want to make it diagonally, like so:
0 1 2 3 4 5
1 0 1 2 3 4
2 1 0 1 2 3
3 2 1 0 1 2
4 3 2 1 0 1
5 4 3 2 1 0
Currently I have this method:
public static void drawMatrix(){
for (int line = 0; line < N; line++){
for (int j = 0; j < N; j++){
System.out.print(j + " ");
}
System.out.println();
}
}
Unfortunately it's only able to print 0 1 2 3 4 5 in every line, so I suppose I need another nested for-loop, however I'm not sure how to set it up.
j is the column number, so it will be the same for all rows. What you need to do is to add or subtract j from the line number, depending on the line number, in order to make a "shift". Since the result could become negative, you will need to add N and mod by N:
if (j > line) {
System.out.print((N-line+j)%N + " ");
} else {
System.out.print((line-j+N)%N + " ");
}
Demo.
You can also rewrite it without an if using a conditional expression:
int sign = j > line ? -1 : 1;
System.out.print((N+sign*(line-j))%N + " ");
Demo.
A little change in your code works
public static void drawMatrix() {
for(int line = 0; line < N; line++) {
for(int j = 0; j < N; j++) {
System.out.print(Math.abs(line - j) + " ");
}
System.out.println();
}
}
I would do something like :
int n=6;
for(int row=0;row<n;row++)
{
for(int col = 0;col<n;col++)
{
System.out.print(abs(col-row) +" ");
}
System.out.println();
}
assuming you can use abs().
I hoped that help your purpose.
This also works :
public static void main(String[] args) {
int N = 6;
int column = 0;
for (int row = 0; row < N; row++) {
for (column = row; column >= 0; column--) //prints till row number reaches 0
System.out.print(column + " ");
for (column = 1; column < N - row; column++)//from 1 to N-row
System.out.print(column + " ");
System.out.println();
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Create a java code that display the following figure:
1
2 3 4
3 4 5 6 7
4 5 6 7 8 9 10
Hey guys! New on here. We have an exercise to do involving java programming and for some reason It seems to get my head blows out in solving this problem. There's what I have done so far but the output is incorrect.
public class Pyramid {
public static void main(String[] args) {
int size = 10;
for (int row = 1; row <= size; row++) {
int j = 1;
int i = 1;
int counter1 = 1;
int counter2 = 1;
// print space
while (j++ <= size - row) {
System.out.print(" ");
}
// count up
j = size - (size - row);
if (j == 10) j = 0;
counter1 = 1;
while (counter1 <= row) {
System.out.print(j);
if (j == 9 && counter1 != row) j = -1;
j++;
counter1++;
}
j = j - 2;
counter2 = 1;
// count down
while (counter2 < row) {
System.out.print(j--);
if (j < 0) j = 9;
counter2++;
}
System.out.print("\n");
}
}
}
I can see two major mistakes in what you posted.
10 is the highest number in the pyramid but it has only 4 rows. So size should be equal to 4 (unless it is just an example and the number of rows isn't important).
I don't see any reason why you make a countdown. You can see that the numbers are always increasing in the same row.
In addition to that:
You should separate the numbers by a blank. The numbers in your example were stuck to each other. This imply that you have to change the number of spaces before each row. (In your example you can simply double the spaces)
Really not important: you can use System.out.println() when you want an new line.
Here you are doing counting down after the mid point of each raw. So instead of increasing number, you are decreasing in the count down part (j is decreasing).
And here you need 4 numbers of raws. but u have programmed for 10 numbers of raws with some conditions (here you don't need to specify the highest number. You just input the number of raws is enough. Please find the Logic below).
And even though you need to increase the number of raws, the logic should not be changed.
Logic:
For each raw the number of elements should be the raw's odd number and the 1st element of each raw should be started with the raw's number.
for example,
if we consider 3rd raw, then 3rd odd number is = (3*2) - 1 = 5. So number of elements in the raw is 5.
And the start element of 3rd raw is 3.
So raw will be:
3,4,5,6,7
Please try below code:
int n = 5;
for (int i = 1; i < n; i++) {
int odd = (i*2) - 1;
for (int j = 1; j <= odd; j++) {
if (j==1) {
for (int x = n; x >=i; x--)
System.out.print(" ");
}
System.out.print(((j + i)-1) + " ");
}
System.out.println();
}
Output is:
1
2 3 4
3 4 5 6 7
4 5 6 7 8 9 10
Try this
class Pyramid {
public static void main(String[] args) {
int myLevel;
int i, j, k;
int x = 1;
myLevel = 6;
for (i = 1; i <= myLevel; i++) {
for (k = 1; k <= myLevel - i; k++) {
System.out.print(" ");
}
for (j = k + 1; j <= myLevel; j++) {
System.out.print(x);
}
for (int l = myLevel; l > k - 1; l--) {
System.out.print(x);
}
x++;
System.out.println("");
}
}
}
Here is the series :
12345
22345
33345
44445
I tried to solve this but it is not coming correct...
Here is the code :
class q14
{
public static void main ( )
{
int i,j,k;
for (i=1;i<=5;i++)
{
for (j=i;j<=5;j++)
{
for (k=1;k<=i;k++)
{
System.out.print (i + " ");
}
System.out.print (j + " ");
}
System.out.println();
}
}
}
The following block should generate the series as you described it.
int numberOfLines = 4;
int numberOfDigitsPerLine = 5;
for (int i=1; i<numberOfLines+1; i++){
for(int j=1; j<=numberOfDigitsPerLine; j++) {
if(j>=i) {
System.out.print(j);
} else {
System.out.print(i);
}
}
System.out.println();
}
Change numberOfLines and numberOfDigitsPerLine as necessary.
Elaboration:
First you must analyze the series, by the looks of it the first number starts with 1 and goes onward for 5 digits, the second line goes along 5 digits as well up to 5 as previously but it replaces the first digit with 2.
Moving down the numbers we can see a pattern of which the N-th number will have N amount of N digits followed by consecutive digits up to the number 5.
So in my code above I chose max N to be 4 as you described it, and the numbers go up to 5, these are represented by the variables numberOfLines and numberOfDigitsPerLine respectively.
The block itself checks what is N at that point (in my block it is represented by i) and then proceeds to go towards the max number 5, this is done within the j for loop. If j is larger or equal to N then we print j, otherwise we haven't finished printing all of the N's yet so we print N instead.
Here it is:
for (int i = 1; i <= 5; i++)
{
for(int k = 1; k <= i;k++)
System.out.print(i);
for (int j = i + 1; j <= 5; j++)
System.out.print(j);
System.out.print("\n");
}
You dont need a third loop for your series
for (int j=1;j<=5;j++) {
for (int k=1;k<=5;k++){
if(k<=j)
System.out.print (j + " ");
else
System.out.print (k + " ");
}
System.out.println();
}
output
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
Demo
Try this:
for(int i=1;i<=4;i++)
{
for(int j = 1; j<=5;j++)
{
if(i>j)
{
for(int x= 1 ; x<=i;x++)
{
System.out.print(i);
j++;
}
}
System.out.print(j);
}
System.out.println("\n");
}
public class testing
{
public void show() {
int num = 0;
int n = 5;
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) {
System.out.print(num++);
System.out.print(" ");
}
System.out.println(" ");
}
}
}
This question came up in one of our previous exams and I don't understand it. The answer is
0
1 2
3 4 5
6 7 8 9
But I have no clue how they got it. I kind of understand the 2nd to 4th line but have no clue how they got 0 on the first line. Any explanation would be highly appreciated, thanks!
but have no clue how they got 0 on the first line ?
int num = 0; --> it is 0 initially
For the first iteration your inner loop executes only 1 time
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) { ---> for(int j=0;j<1;j++) // for 1st time
So that is why the below line
System.out.print(num++); //printed 0
Note : there is a tool known as debugger , Use it !!
Maybe the following amended code could help to understand
int num = 0;
int n = 5;
for (int i=1; i<n; i++) { // loop from 0 to 4
System.out.printf("num=%d i=%d : ", num, i);
for (int j=0; j<i; j++) { // loop from 0 to i
System.out.print(num++); // print num then increment num
System.out.print(" ");
}
System.out.println(" ");
}
output
num=0 i=1 : 0
num=1 i=2 : 1 2
num=3 i=3 : 3 4 5
num=6 i=4 : 6 7 8 9
I believe your problem lies in this line
System.out.print(num++);
in a more verbose way it does the following
System.out.print(num);
num = num + 1;