Pyramid of numbers using (for) Java - java

a pyramid of numbers thats core is increasing by one which would be seen horizontally as 12345.
I figured out this code
but its not as the img I shown up there ^
the code I wrote is:
int rowCount = 1;
for (int i = 6; i > 0; i--)
{
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
rowCount++;
}
}
`

Related

For loop for increasing pyramid pattern

I am trying to figure out how to increment a pyramid based on input from the user.\
If a user enters the number 3, my program will print a pyramid of height 3, three times.\
What I would like it to do instead, is to print 3 pyramids, but the first pyramid should have a height of 1, and the second a height of 2, and the third a height of 3.
Here is my code:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for (int l = 0; l < n; l++) {
System.out.println("Pyramid " + n);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < n - i; k++) {
System.out.print(".");
}
System.out.println();
}
}
You use l to keep track of the number of pyramid you're working on, so you could just use it in the loop instead of n. Note that l starts with 0, not 1, so you may want to amend the loop accordingly and run from 1 to n, not from 0 to n-1
for (int l = 1; l <= n; l++) { // Note the loop starts at 1
System.out.println("Pyramid " + l);
for (int i = 1; i <= l; i++) { // Note the usage of l instead on n
for (int j = 0; j < l - i; j++) {
System.out.print(".");
}
for (int j = 0; j < i; j++) {
System.out.print("#");
}
for (int j = 1; j < i; j++) {
System.out.print("#");
}
for (int k = 0; k < l - i; k++) { // Note the usage of l instead on n
System.out.print(".");
}
System.out.println();
}
}
All you have to change is this part of code
System.out.println("Pyramid " + (l+1));
for (int i = 1; i <= l+1; i++) {

How to make diamond pattern using java?

How to make this pattern
if input N = 5
Output :
Mine is like this, it become 2N
if input N = 5
Output
Here's my code
int i,j;
for(i = 0; i <= n; i++)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
for(i = n - 1; i >= 1; i--)
{
for(j = 1; j <= n - i; j++)
System.out.print(" ");
for(j = 1; j <= 2 * i - 1; j++)
System.out.print("*");
System.out.print("\n");
}
What should i fix??
You can check odd numbers in your loop. Please see the following example:
public static void main(String[] args) {
printPattern(5);
}
private static void printPattern(int n) {
int i, j;
for (i = 0; i <= n; i++) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
for (i = n - 1; i >= 1; i--) {
if (i % 2 != 0) {
for (j = 1; j <= (n - i)/2; j++) {
System.out.print(" ");
}
for (j = 0; j <i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Instead of running these two loops from 0 to N twice. Just run half N/2 in each loop.
Example:
public static void main(String[] args) {
int n = 10;
for (int i = 0; i <= (n / 2 + 1); i++) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
// N/2
for (int i = n / 2 - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.print("\n");
}
}

How to create a square with numbers?

I'm trying to create a square shape with numbers like this:
1234
2341
3412
4123
How would I go about doing that?
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
System.out.println();
}
This prints:
1234
234
34
4
How do I get it to restart at 1 again?
You can do by using modulo
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print((j + i) % 4 + 1);
}
System.out.println();
}
output
1234
2341
3412
4123
You need to update to
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j < i; j++) {
System.out.print(j);
}
System.out.println();
}
add one more for loop
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
}
for (int j = 1; j <i; j++) {
System.out.print(j);
}
System.out.println();
}
There's probably a simpler way, but this should work:
for (int i = 1; i <= 4; i++) {
for (int j = 3; j <= 6; j++) {
System.out.print ((i + j) % 4 + 1);
}
System.out.println();
}
try this:
public class MyClass {
public static void main(String args[]) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <=4; j++) {
System.out.print(j);
count += 1;
}
if(count < 4){
int remain = 4 - count; // to identify how many times it goes again
for(int k=0;k<remain;k++){
System.out.print(k+1);
}
}
count = 0;
System.out.println();
}
}
}
Output:
1234
2341
3412
4123
How about this?
for(int i = 1; i <= 4; i++){
for(int j = 1; j <= 4; j++){
System.out.print(((i + j + 2) % 4) + 1);
}
System.out.println();
}
If you look at the expected output, it has a pattern, which is restricted to a finite and known sequence of numbers. Modulus is the ideal option to handle cases like this.
for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 3; j++) {
System.out.print(((i + j) % 4) + 1);
}
System.out.println();
}
Modulus (%) gives the remainder of one number divided by another, so (i+j) % 4 always gives a result between 0 and 3.
Add one to that to get the value you want (between 1 and 4).

replace every second line of my triangle with a blank line for loops

instead of getting a full triangle with 5 lines of "*" how do I replace every second line with a blank line, e.g. to look like this
5
555
55555
This is my code:
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5");
}
System.out.println();
}
you need small modification in your code and you can get what you want.
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
if(i%2==0)
System.out.print("*");
}
System.out.println();
}
check it out.
Just test for i % 2
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print(i % 2 == 0 ? "*" : " ");
}
System.out.println();
}
edit
If you want to print 5 instead or * then modify the code to use 5
Check this:
int num= 5; // 'num' denotes the number of lines
int numOfSpaces;
for(int i = 1; i <= num; i++) {
if( i%2 != 0) // checks whether the line number is odd. If odd, prints '*'
{
numOfSpaces = (num-i) / 2; // find the number of blank spaces
for(int j = 1; j <= numOfSpaces; j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) { // number of '*'s to be printed = current line number = i
System.out.print("*");
}
System.out.println();
}
else{
System.out.println(); // leaves every second line blank (ie.,if line number is even.)
}
}
//try this code. It may help you to achieve your output
[Check screen shot for the output of the below code][1]
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
if(i%2!=0)
{
System.out.print(" ");
}
else{
System.out.print("* ");
}
}
System.out.println();
}
The Code below is Working now.
A simple if condition will solve your problem. Just check if the
value of i is divisible by 2. If it is true then print a
blank line. Else Print * and you are done!
* will be printed on 1st, 3rd and 5th lines.
Blank Space will be printed on 2nd and 4th lines.
public class PrintPattern{
public static void main(String []args){
int lineNo=0; // New Variable for Line Number (Can Use variable 'i' also)
for(int i = 0; i < 5; i++) {
lineNo++;
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
if(lineNo%2!=0){
for(int k = 0; k <= (i*2); k++)
System.out.print('*'); // Will print '*' (or '5') Sequence for Odd Lines - 1st, 3rd, 5th Lines
System.out.println();
}
else
System.out.println();// Will print Blank Line for Even Lines - 2nd and 4th Lines
}
}
}
Try this
for(int i = 0; i < 5; i++) {
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("5"); //Use 5 instaed of * to draw
}
System.out.println();
System.out.println(); //This one will solve your problem
}
Output will be:
5
555
55555
5555555
555555555
I think that you can just enter one more print statement inside the loop:
for(int i = 0; i < 5; i++) {
System.out.println();
for(int j = i; j < 5; j++) {
System.out.print(" ");
}
for(int k = 0; k <= (i*2); k++) {
System.out.print("*");
}
System.out.println();
}
Output
*
***
*****
*******
*********

java pyramid with multiple of 2 not working

I am suppose to create a pyramid and multiply each number by two until it reach the middle then divide by two such as shown in example below.
However after writing my code I am unable to have the numbers double (i*2) then once it reaches the center it divides by two until the it becomes 1
My output:
package question5;
public class Pyramid {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++)
{
System.out.print(j+" ");
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
this is the weird output of it
This is working... You can use math.pow method.
public class test {
public static void main(String[] args) {
int x = 5;
int rowCount = 1;
System.out.println("Here Is Your Pyramid");
//Implementing the logic
for (int i = x; i > 0; i--)
{
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++)
{
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 0; j <= rowCount-1; j++)
{
System.out.printf("%2d", (int)Math.pow(2, j));
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--)
{
System.out.printf("%2d", (int)Math.pow(2, j-1));
}
System.out.println();
//Incrementing the rowCount
rowCount++;
}
}
}
Right now you're outputting j, a number from 1 to 5. You want to output 2j-1 instead, which is easy: 1 << (j - 1).
You also need to right-adjust the number. Your print statement then becomes:
System.out.printf("%4d", 1 << (j - 1));

Categories

Resources