printing numbers flipped in pyramid style - java

I am trying to print something like this using for loops:
1
121
12321
1234321
123454321
int mid = 1;
System.out.println(" " + mid + " ");
mid++;
for(int i = 1; i <=4; i++){
//left spaces
for(int x = 4; x > i; x--){
System.out.print(" ");
}
//left diguts
for(int z = 1; z <= i; z++){
System.out.print(z);
}
//middle digit
System.out.print(mid);
mid++;
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
//right spaces
for(int y = 4; y > i; y--){
System.out.print(" ");
}
System.out.println();
}
But i kept on getting:
1
121
12312
1234123
123451234

For the right digits you will have to print the digits in reverse order so make the change as below:
//right diguts
for(int b = 1; b <= i; b++){
System.out.print(b);
}
change this to
//right diguts
for(int b = i; b > 0; b--){
System.out.print(b);
}

Actually there is no need to separate 1 out. You can include it in common logic:
public class Pyramid {
public static void main(String[] args) {
int mid = 1;
for (int i = 0; i <= 4; i++) {
// left spaces
for (int x = 4; x > i; x--) {
System.out.print(" ");
}
// left digits
for (int z = 1; z <= i; z++) {
System.out.print(z);
}
// middle digit
System.out.print(mid);
mid++;
// right digits
for(int b = i; b > 0; b--){
System.out.print(b);
}
// right spaces
for (int y = 4; y > i; y--) {
System.out.print(" ");
}
System.out.println();
}
}
}

This is actually nice golf puzzle!
I did this only using two loops! :)
int n = 5;
for(int i=0; i < n; i++){
for(int j=0; j < 2*n; j++){
int abs = Math.abs(n-j);
System.out.print(abs>i ? " " : i-abs+1);
}
System.out.println();
}
Output:
1
121
12321
1234321
123454321

Related

How to Flip the triangle?

How to flip this triangle?
So i was making aritmethic sequance triangle. It was upside down.
How do I turn it 180 degree?
for example:
1=1
1+2=3
1+2+3=6
etc...
my code:
package javaapplication4;
public class NewClass5 {
public static void main(String[] args) {
int i=5,a;
for(int j=i; j>=1; j--) {
for(a=1; a<=i; a++)
System.out.print(a +" + ");
int n = 0;
for(a = 1; a<=i; a++) {
n = n + a;
}
System.out.print(" = "+ n);
System.out.println();
i--;
}
}
}
You can do it for any n, by getting input from the user
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int add = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
add += j;
}
System.out.println(add);
}
I think you just need to change the sequence of loop from descending to ascending, rest of the things should be the same:
for (int i = 1; i <= 5; i++) {
int sum = 0;
for (int j = 1; j <= i; j++) {
System.out.print(j);
if (j == i)
System.out.print("=");
else
System.out.print("+");
sum += j;
}
System.out.println(sum);
}
When I run this, I'm able to see expected output:
src : $ java NewClass5
1=1
1+2=3
1+2+3=6
1+2+3+4=10
1+2+3+4+5=15

Nested Loops Patterns

So yeah, I'm now working on nested loops but I think I'm stuck on somewhere because my desired output is:
*
**
***
**
*
And here's my code:
//intro here
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print("");
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
}
but the output happening was:
*
**
***
I don't know now what to do, does it need another for with a d-- or something?
Yes. You need another for loop for print decreasing number of stars. This should work well!
import java.util.Scanner;
public class Nested {
public static void main(String[] args) {
Scanner var = new Scanner(System.in);
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
//create star rows from 1 to 6
for (int a = 1; a <= x; a++) {
for (int b = 1; b <= a; b++) {
System.out.print("*");
}
System.out.println();
}
//create start rows from 5 to 1
for (int a = x - 1; a > 0; a--) {
for (int b = a; b > 0; b--) {
System.out.print("*");
}
System.out.println();
}
}
}
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int b = 0; b <= x - a; b++){
System.out.print(""); //this code block do nothing in ur case
}
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
}
you can achieve the desired output by:
int x = 0;
System.out.print("Enter: ");
x = var.nextInt();
for(int a = 1; a <= x; a++){
for(int c = 0; c < a; c++){
System.out.print("*");
}
System.out.println();
}
for (int a = x - 1; a > 0; a--){
for(int c = a; c > 0; c--)
System.out.print("*");
System.out.print(""\n");
}

Need help adjusting a diamond shape in Java

I wrote some code that will print a diamond
static void printDiamond(int size) {
for (int i = 0; i < size; i++) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
for (int i = size-1; i >= 0; i--) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
The issue I'm having with the diamond is that it will print double for whatever I input. So, if the user were to input a 6 for the diamond, it should look like this:
XX
X X
X X
X X
X X
XX
With my code, if the user inputs 5, it prints out the following:
XX
X X
X X
X X
X X
X X
X X
X X
X X
XX
Instead of printing out 5 rows, it printed out 10. If I input 3, it will print out 6 rows instead of 3. It seems that for my diamond, it is outputting the number that it receives from the user, but then prints out that amount times 2. Is there a way I can half the size of what the printDiamond method outputs so it has the correct number of rows?
I was able to correct your code by tweaking the loop boundary conditions. First, you were printing the top portion of your diamond with a height of size and you were also printing the bottom potion with a height of size, for a total height of 2*size.
The other big problem was that you were not handling odd-numbered inputs, as all diamonds were coming out as even number height. I also corrected this problem. Have a look at the code below.
static void printDiamond(int size) {
for (int i = 0; i < (int)Math.ceil(size/2.0); i++) {
for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
for (int i = (int)Math.floor(size/2.0)-1; i >= 0; i--) {
for (int a = 0; a < (int)Math.ceil(size/2.0) - (i + 1); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
printDiamond(5);
System.out.print("\n");
printDiamond(6);
Output:
XX
X X
X X
X X
XX
XX
X X
X X
X X
X X
XX

Double Dimensional Array (Prime(sum) counting flaw)

import java.util.Scanner;
public class array1 {
public static void main(String [] args){
int table[][] = new int[5][5];
Scanner scan = new Scanner(System.in);
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
System.out.println("Write a value for row " +i + " column " +j);
int n = scan.nextInt();
table[i][j] = n;
}
}
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
int sum = 0;
boolean prime = true;
for(int i =0; i < 5; i++){
for(int j =0; j < 5; j++){
for(int e = 2; e < table[i][j]; e++ ){
if(table[i][j] % e == 0){
prime = false;
}
}
if(prime == true){
sum += table[i][j];
}
else{}
}
}
System.out.println();
System.out.println("Sum of all prime numbers in this array is " +sum);
}
}
Well, as the title itself says uhmm The program is supposed to sum up all the prime numbers in the user defined Array table but it's just summing up the first row. i checked up all the brackets, nothing helps. Any help would be appreciated! Thank you!
You should reset prime for each iteration:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
for (int e = 2; e < table[i][j]; e++) {
if (table[i][j] % e == 0) {
prime = false;
}
}
if (prime == true) {
sum += table[i][j];
} else {
}
prime = true;
}
}

Looping Algorithm

How do I make this:
*******
-*****-
--***--
---*---
--***--
-*****-
*******
The following is my code that I have written to try to accomplish the above, but it is not working as expected:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
stars();
}
}
This is how I might write it.
// three loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int i = 0; i < y && i < size - y - 1; i++)
System.out.print(' ');
for (int i = Math.min(y, size - y - 1); i < Math.max(y + 1, size - y); i++)
System.out.print('*');
System.out.println();
}
}
or
// two loops
public static void stars(int size) {
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++)
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
System.out.println();
}
}
or
// one loop
public static void stars(int size) {
for (int i = 0; i < size * size; i++) {
int y = i / size, x = i % size;
System.out.print(
(x >= y && x < size - y) ||
(x >= size - y - 1 && x <= y) ? '*' : ' ');
if (x == size - 1)
System.out.println();
}
}
Note: Whether this uses one, two or three loops, the time complexity is O(N^2). A simple way to determine this is the number of stars produced is O(N^2) no matter how it is done.
I would do something like this with substrings.
String a = "*******"; //7 stars
String blank = " "; //7 spaces
int j = 7;
for (int i = 0; i < 7; i++) {
if (i > j){
System.out.print(blank.substring(0,i));
System.out.println(a.substring(i,j));
}
else{
System.out.print(blank.substring(0,j));
System.out.println(a.substring(j,i));
}
j--;
}
System.out.println(a);
**Previous edit wouldn't have worked. Changes made.
This works.
Try something like this code I compiled on IDEOne (it seems to work, though):
http://ideone.com/9xZ1YB
class Main
{
public static void main(String[] args)
{
stars();
}
static void stars()
{
final int MAX_WIDTH = 7;
for (int i = 0; i < 7; ++i)
{
int width;
if (i < 3) width = MAX_WIDTH - i * 2;
else if (i > 3) width = (i - 3) * 2 + 1;
else width = 1;
// Before spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
// Stars
for (int j = 0; j < width; ++j)
{
System.out.print("*");
}
// After spaces
for (int j = 0; j < (MAX_WIDTH - width) / 2; ++j)
{
System.out.print(" ");
}
System.out.println();
}
}
}
For a beginner in algorithms I would recommend you to break down the structure in sub-parts and then try to solve the pattern.
For this specific pattern it could be broken down into several triangles. Each triangle is then solved by different for loops as shown in the image below.
public static void printPattern(int num) {
// this loop generates first 4 lines
for (int i = 0; i < num / 2 + 1; i++) {
// draws the red triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
// draws the green triangle of '*'
for (int j = i; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '*'
for (int j = i + 1; j < num / 2 + 1; j++) {
System.out.print("*");
}
// draws the orange triangle of '-'
for (int j = 0; j < i; j++) {
System.out.print("-");
}
System.out.println();
}
/* this loop generates last 3 lines */
for (int i = 0; i < num / 2; i++) {
// draws the green triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
// draws the red triangle of '*'
for (int j = 0; j < i + 2; j++) {
System.out.print("*");
}
// draws the orange triangle of '*'
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
// draws the blue triangle of '-'
for (int j = i + 1; j < num / 2; j++) {
System.out.print("-");
}
System.out.println();
}
}
Using similar technique you could generate any pattern.
If I understood you right, your problem is to print indent in lines 2-7.
Imagine same problem with asterisk symbol replaced by 'x' and whitespace replaced by '-'. Then you need to draw
xxxxxxx
-xxxxx-
--xxx--
---x---
--xxx--
-xxxxx-
xxxxxxx
That means you should output 0, 1, 2 space(s) before asterisks in first, second, thrid strings respectively. I let details for you to figure them out.
public static void stars(/*int jmlBaris*/){
String starstr = "*";
String blank = "_";
int spaceBlank;;
for(int i=7; i>=1;i-=2){
spaceBlank = (7-i)*.5;
String starrep = StringUtils.repeat(starstr, i);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
for(int j=3 j<=7; j+=2){
spaceBlank = (7-j)*.5;
starrep = StringUtils.repeat(starstr, j);
String blankrep = StrinUtils.repeat(blank, spacesBlank);
system.out.println(blankrep + starrep + blankrep);
}
}
public static void main(String[] args){
stars();
}
You have little missing to put space on your code. I don't care about right space, who can see that? But left space is very important!!
Try this:
public static void stars(/*int jmlBaris*/) {
for ( int i = 7; i >= 1; i-=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
for (int i = 1; i <= 7; i+=2) {
for (int k = 0; k < ((7-i) / 2); k++){ /* Missing Here */
System.out.print(" "); /* Missing Here */
} /* Missing Here */
for (int j = 1; j <= i; j++){
System.out.print("*");
}
System.out.println("");
}
}
int N = 7;
for (int y=0; y<N; y++)
{
for (int x=0; x<N; x++)
System.out.print( (y-x)*(N-y-x-1)<=0 ? '*' : '-');
System.out.println();
}
or, more symmetrically,
int n = 3;
for (int y=-n; y<=n; y++)
{
for (int x=-n; x<=n; x++)
System.out.print( y*y>=x*x ? '*' : '-');
System.out.println();
}

Categories

Resources