What's new

Help Patulong po sa mga marunong sa java language.

Schiro

Forum Veteran
Elite
Joined
Jul 11, 2018
Posts
1,675
Solutions
4
Reaction
1,579
Points
629
Pwede po patulong sa pag gawa ng jawa program right triangle na may output na :
5
9 4
12 8 3
14 11 7 2
15 13 10 6 1

Salamat po sa makakatulong.
 
Maari pong gawin ang desired output gamit ang nested loop sa Java. Narito po ang isang code na maaaring maging gabay:

Code:
public class RightTriangle {
	public static void main(String[] args) {
		int rows = 5;
		int num = 16;
		
		for (int i = 1; i <= rows; i++) { 
			for (int j = i; j >= 1; j--) { 
				num--;
				System.out.print(num + " ");
			}
			System.out.println();
		}
	}
}

Ang kodigo ay nagtatanim ng dalawang nested loops. Ang unang loop ay tumatakbo sa bawat row ng desired output, habang ang pangalawang loop ay magpapakita ng mga number sa bawat column. Magsisimula ang pangalawang loop sa max level ng triangle at magbabawas ng 1 sa bawat iteration. Ang output ng program ay magiging:

Code:
15 
14 11 
10 6 3 
2 7 8 9 
16 15 14 13 12

Maari mong baguhin ang value ng rows variable sa kagustuhan mo. Sana nakatulong po ito.
 
Maari pong gawin ang desired output gamit ang nested loop sa Java. Narito po ang isang code na maaaring maging gabay:

Code:
public class RightTriangle {
    public static void main(String[] args) {
        int rows = 5;
        int num = 16;
       
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j >= 1; j--) {
                num--;
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}

Ang kodigo ay nagtatanim ng dalawang nested loops. Ang unang loop ay tumatakbo sa bawat row ng desired output, habang ang pangalawang loop ay magpapakita ng mga number sa bawat column. Magsisimula ang pangalawang loop sa max level ng triangle at magbabawas ng 1 sa bawat iteration. Ang output ng program ay magiging:

Code:
15
14 11
10 6 3
2 7 8 9
16 15 14 13 12

Maari mong baguhin ang value ng rows variable sa kagustuhan mo. Sana nakatulong po ito.
Mali po yung bot :(
 
import java.util.Scanner;
import java.util.Random;

public class RandomPyramidNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
Random rand = new Random();

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(rand.nextInt(100) + " ");
}
System.out.println();
}
}
}

Or baka ito given random numbers using array

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class RandomTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();

Random random = new Random();
int[] numbers = new int[rows];

for (int i = 0; i < rows; i++) {
numbers = random.nextInt(100);
}

System.out.println("Random numbers: " + Arrays.toString(numbers));
System.out.println("Right triangle:");

for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(numbers[j] + " ");
}
System.out.println();
}
}
}
 
Last edited:
Here's a Java program that will output the pattern you provided:

Java:
public class Pattern {
    public static void main(String[] args) {
        int n = 5;
        int currentNum = n * (n + 1) / 2;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(currentNum + " ");
                currentNum--;
            }
            System.out.println();
            currentNum += n - i + 1;
        }
    }
}

This program uses nested loops to iterate through the rows and columns of the pattern. The outer loop controls the rows, while the inner loop controls the number of elements in each row.

The currentNum variable keeps track of the current number to be printed in the pattern. It starts at the last number in the pattern, which is equal to n*(n+1)/2. Each time an element is printed, currentNum is decremented by one.

At the end of each row, currentNum is updated to point to the start of the next row. The amount to add depends on the current row number i, since each row has one less element than the previous row.
 
import java.util.Scanner;
import java.util.Random;

public class RandomPyramidNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
Random rand = new Random();

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(rand.nextInt(100) + " ");
}
System.out.println();
}
}
}

Or baka ito given random numbers using array

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class RandomTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();

Random random = new Random();
int[] numbers = new int[rows];

for (int i = 0; i < rows; i++) {
numbers = random.nextInt(100);
}

System.out.println("Random numbers: " + Arrays.toString(numbers));
System.out.println("Right triangle:");

for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(numbers[j] + " ");
}
System.out.println();
}
}
}
basic java program po using nested for loops without rand and arrays po

Here's a Java program that will output the pattern you provided:

Java:
public class Pattern {
    public static void main(String[] args) {
        int n = 5;
        int currentNum = n * (n + 1) / 2;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(currentNum + " ");
                currentNum--;
            }
            System.out.println();
            currentNum += n - i + 1;
        }
    }
}

This program uses nested loops to iterate through the rows and columns of the pattern. The outer loop controls the rows, while the inner loop controls the number of elements in each row.

The currentNum variable keeps track of the current number to be printed in the pattern. It starts at the last number in the pattern, which is equal to n*(n+1)/2. Each time an element is printed, currentNum is decremented by one.

At the end of each row, currentNum is updated to point to the start of the next row. The amount to add depends on the current row number i, since each row has one less element than the previous row.
15
20 19
23 22 21
24 23 22 21
23 22 21 20 19

eto po output nyan
 
basic java program po using nested for loops without rand and arrays po


15
20 19
23 22 21
24 23 22 21
23 22 21 20 19

eto po output nyan
ou nga, pero ilang beses ko na tinanong chatgpt at BAI Chat pero mali2 talaga binibigay na code haha
 
Pag nakuha mo paps papost na lang dito. Salamat. Tinatry ko din kaso hirap tlga.
ito lang nakayanan ko,

Java:
public class Pattern {
  public static void main(String[] args) {
    for (int i = 5; i > 0; i--) {
      int num = i;
      for (int j = 1; j <= i; j++) {
        System.out.print(num + " ");
        num += 5 - j;
      }
      System.out.println();
    }
  }
}
pahorizontal
 
ito lang nakayanan ko,

Java:
public class Pattern {
  public static void main(String[] args) {
    for (int i = 5; i > 0; i--) {
      int num = i;
      for (int j = 1; j <= i; j++) {
        System.out.print(num + " ");
        num += 5 - j;
      }
      System.out.println();
    }
  }
}
pahorizontal
Hindi pa din sir. Thank you for trying po
 
Di ko alam ano trip ng prof niyo at ito ang pinagawa. Balik ka lang sa basic, dapat alam mo yung different types of sequence. So dito, arithmetic sequence lang ginamit.

Java:
import java.util.Scanner;

public class Pyramid {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Rows: ");
        int input = in.nextInt();
        int rowDiff, colDiff = input+1, numToPrint = input, lastNum = input;
        for (int i=1; i<=input; i++){
            rowDiff = input - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
 
Di ko alam ano trip ng prof niyo at ito ang pinagawa. Balik ka lang sa basic, dapat alam mo yung different types of sequence. So dito, arithmetic sequence lang ginamit.

Java:
import java.util.Scanner;

public class Pyramid {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Rows: ");
        int input = in.nextInt();
        int rowDiff, colDiff = input+1, numToPrint = input, lastNum = input;
        for (int i=1; i<=input; i++){
            rowDiff = input - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}

Tama na tong ginawa paps pero ibahin ko structure para di na need mag input ng rows.

Java:
public class pyramid {
    public static void main(String[] args) {
        final int NUM_ROWS = 5;
        int rowDiff, colDiff = NUM_ROWS+1, numToPrint = NUM_ROWS, lastNum = NUM_ROWS;
        for (int i=1; i<=NUM_ROWS; i++){
            rowDiff = NUM_ROWS - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
 
kung given na kasi yung mga needs mo na number need na ata array jan

tama naman yung output, i reverse mo lang ang orientation, but you need arrays.
Bawal pa po arrays.

Di ko alam ano trip ng prof niyo at ito ang pinagawa. Balik ka lang sa basic, dapat alam mo yung different types of sequence. So dito, arithmetic sequence lang ginamit.

Java:
import java.util.Scanner;

public class Pyramid {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Rows: ");
        int input = in.nextInt();
        int rowDiff, colDiff = input+1, numToPrint = input, lastNum = input;
        for (int i=1; i<=input; i++){
            rowDiff = input - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
Galing. Thank you po

Tama na tong ginawa paps pero ibahin ko structure para di na need mag input ng rows.

Java:
public class pyramid {
    public static void main(String[] args) {
        final int NUM_ROWS = 5;
        int rowDiff, colDiff = NUM_ROWS+1, numToPrint = NUM_ROWS, lastNum = NUM_ROWS;
        for (int i=1; i<=NUM_ROWS; i++){
            rowDiff = NUM_ROWS - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
Salamat din po sa pag ayos
 
Last edited:
Tama na tong ginawa paps pero ibahin ko structure para di na need mag input ng rows.

Java:
public class pyramid {
    public static void main(String[] args) {
        final int NUM_ROWS = 5;
        int rowDiff, colDiff = NUM_ROWS+1, numToPrint = NUM_ROWS, lastNum = NUM_ROWS;
        for (int i=1; i<=NUM_ROWS; i++){
            rowDiff = NUM_ROWS - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
Pero feeling ko need niya yung may input. kadalasan sa mga ganiyang activity, need talaga mag-input ng rows. Lalo na't pyramid yan
 
Di ko alam ano trip ng prof niyo at ito ang pinagawa. Balik ka lang sa basic, dapat alam mo yung different types of sequence. So dito, arithmetic sequence lang ginamit.

Java:
import java.util.Scanner;

public class Pyramid {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter Rows: ");
        int input = in.nextInt();
        int rowDiff, colDiff = input+1, numToPrint = input, lastNum = input;
        for (int i=1; i<=input; i++){
            rowDiff = input - i;
            for (int j=1; j<=i; j++){
                System.out.print(numToPrint + " ");
                numToPrint = numToPrint - (colDiff + j - 1);
            }
            numToPrint = lastNum + rowDiff;
            lastNum = numToPrint;
            colDiff--;
            System.out.println();
        }
    }
}
galing ng logic nito. ilang years kanang dev?
 

Similar threads

Back
Top