How to Flip the triangle? - java

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

Related

Can this problem be solved using nested loops?

Would it be possible to produce this layout with nested loops? I'm still new to nested to Java/loops and cannot solve this issue.
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
I'm having trouble looping through five times with the "*" character without allowing the "+" to increment.
Here is my code:
class Main {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 1; m++) {
for (int n = 0; n < m; n++) {
System.out.print("+");
}
}
System.out.println();
}
System.out.print("*****+++++");
}
}
there are could be multiple approaches to this problem, here is how I would think about it:
you need to display 9 lines of "something", so lets have top level loop:
for (int i=0; i<9; i++) {...}
now, each iteration of this loop you need to display X stars, Y equal signs, Z plus signs:
for (int i=0; i<9; i++) {
for (int j=0; j< #X# ; j++) System.out.print("*");
for (int j=0; j< #Y# ; j++) System.out.print("=");
for (int j=0; j< #Z# ; j++) System.out.print("+");
System.out.println();
}
now you need to determine rules how X,Y,Z are changed, here is the logic I came up with:
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
so, final code will look like:
public static void main(String[] args) throws InterruptedException {
int stars = 5; // initial state
int equals = 4;
int pluses = 1;
for (int i=0; i<9; i++) {
for (int j=0; j<stars; j++) System.out.print("*");
for (int j=0; j<equals; j++) System.out.print("=");
for (int j=0; j<pluses; j++) System.out.print("+");
System.out.println();
if (stars > 0 && equals == 0) {
stars = 0;
equals = 5;
}
equals--;
pluses++;
}
}
If you notice there is a pattern.
It prints a * starting from left to right.
It prints a + for each increasing number starting from right to left.
It prints a = instead of * if you are past mid point (first the mid point of left to right, then the mid point of top to bottom).
The logic can be applied as following,
class Main {
public static void main(String[] args) {
int max = 10;
int switchPoint = max - 1;
for(int i = 1; i <= max -1; i++) {
for(int j = 1; j <= max; j++) {
if(j > switchPoint)
System.out.print("+");
else if( i > max/2 || j > max / 2)
System.out.print("=");
else
System.out.print("*");
}
switchPoint--;
System.out.println();
}
}
}
/* Output:
*****====+
*****===++
*****==+++
*****=++++
*****+++++
====++++++
===+++++++
==++++++++
=+++++++++
*/
As you may notice the behavior of the "*" and "=" is different in the first five lines than in the next five lines, so you may either divide the loop in two loops or make a single one and check wether you are printing the first five lines or the last ones, that check may be done either by an if statement or in the loops conditions.
So the code will look like:
class Main {
public static void main(String[] args) {
for (int i = 1; i < 10; i++){
for (int j = 1; i <= 5 && j <= 5; j++){
System.out.print("*");
}
for (int k = 5; (i <= 5 && k > i) || (i > 5 && k > i-5); k--){
System.out.print("=");
}
for (int l = 1; l <= i; l++){
System.out.print("+");
}
System.out.println();
}
}
}
public class A {
public static void main(String[] args) {
for (int k = 4; k > 0; k--) {
System.out.print("*****");
for (int l = 0; l < k; l++) {
System.out.print("=");
}
for (int m = 0; m < 5 - k; m++) {
System.out.print("+");
}
System.out.println();
}
System.out.print("*****+++++");
System.out.println();
for (int s = 4; s > 0; s--) {
for (int k = 0; k < s; k++) {
System.out.print("=");
}
for (int l = 0; l < 5; l++) {
System.out.print("+");
}
for (int m = 0; m < 5 - s; m++) {
System.out.print("+");
}
System.out.println();
}
}
}

nested for loops inverted half pyramid

for a school project, I am trying to make an inverted half pyramid
my code is currently this
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
with this output:
12345
1234
123
12
1
desired output:
54321
=4321
==321
===21
====1
Update (based on the updated requirement):
You need a loop to print the = equal to (rows - row number) times.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = i; j < rows; j++) {
System.out.print("=");
}
for (int j = i; j >= 1; --j) {
System.out.print(j);
}
System.out.println();
}
}
}
Output:
54321
=4321
==321
===21
====1
Original answer:
Your inner loop should be
for (int j = i; j >= 1; --j)
i.e. for each row, it should start with the row number (i.e. i) and go down up to 1.
It is straight forward you will have to change to things: your inner loop and you will have to move println statement inside the loop
//code
public static void main(String[] args){
int rows = 5;
for (int i = rows; i >= 1; --i){
for(int j = i; j >= 1; --j)
System.out.print(j + " ");
System.out.println();
}
}

String,which repeats ,but not starts from the beginning in Java (diamond pattern

This is the work that i done so far:I have to print diamond pattern which always starts with uppercase from string, which repeats,but not always starts from the beginning.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
userInput = Character.toUpperCase(userInput.charAt(0)) + userInput.substring(1);
int i;
int j;
if (userInput.length() % 2 != 0) {
for(i = 1; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
} else {
for(i = 2; i < userInput.length(); i += 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.println("");
}
for(i = userInput.length(); i > 0; i -= 2) {
for(j = 0; j < userInput.length() - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
System.out.print(userInput.charAt(j));
}
System.out.print("\n");
}
}
}
For example my input is "Peter".
So my output is:
P
Pet
Peter
Pet
P
but it must be:
P
Ete
Rpete
Rpe
T
I dont know what to change to make this work
Here's a shorter version of your code:
public static void main(String[] args) {
String userInput = "Peter";
int length = userInput.length();
int m, j, i, n = 0;
for (m = length % 2 > 0 ? 1 : 2; m < length * 2; m += 2) {
i = m < length ? m : length * 2 - m;
for (j = 0; j < length - 1 - i / 2; ++j) {
System.out.print(" ");
}
for(j = 0; j < i; ++j) {
char c = userInput.charAt(n++ % length);
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
}
System.out.println("");
}
}
You need some few changes:
Declare int n=0; after int j;
Always print userInput.charAt(n++ % userInput.length()) instead of charAt(j)
In order to get only the first character in line in uppercase:
char c = userInput.charAt(n++ % userInput.length());
c = j == 0 ? Character.toUpperCase(c) : Character.toLowerCase(c);
System.out.print(c);
Check the modulo operator.
With these changes, you'll get this output:
P
Ete
Rpete
Rpe
T
Given the fact that the input itself gets printed in a cylic manner, we can make use out of it. My proposal would be to concatenate the input string and print out the substrings which are determined by the structure of the diamond pattern.
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.next();
String concatenated = userInput;
// build up the index array
int i, cumSum = 0;
ArrayList<Integer> helperIndex = new ArrayList<>();
for(i = 1; i < userInput.length(); i += 2) {
helperIndex.add(i);
cumSum += i;
}
for(i = userInput.length(); i > 0; i -= 2) {
helperIndex.add(i);
cumSum += i;
}
int numOfWordRepitition = cumSum / userInput.length() ;
for (i = 0; i < numOfWordRepitition; i++){
concatenated += userInput;
}
// print out diamond
String substr;
int prev = helperIndex.get(0);
int next = helperIndex.get(0);
substr = concatenated.substring(0 , helperIndex.get(0));
System.out.println(Character.toUpperCase(substr.charAt(0)) + substr.substring(1));
for(i = 1; i < userInput.length(); i++){
next += helperIndex.get(i);
substr = concatenated.substring(prev , next);
substr = Character.toUpperCase(substr.charAt(0)) + substr.substring(1);
System.out.println(substr);
prev = next;
}
}

Creating a double mirrored triangle

I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}

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;
}
}

Categories

Resources