import java.util.Scanner;
public class pyramidMaxSum {
public static void main(String[] args) {
int n;
System.out.println("Enter number of rows: ");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println("Enter numbers:");
for (int i = 0; i < n; i++) {
int[] nums = new int[n];
nums[i] = sc.nextInt();
for (int j = 0; j < n; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print(nums[i]+" ");
}
System.out.println();
}
}
}
This is my code.I can't seem to be able to get the desired output,which in turn does not allow me to finish my task.I want to print a pyramid/triangle using the standart input.From this input:
5
5
10 20
1 3 1
99 20 7 40
5 15 25 30 35
I need to get this output
5
10 20
1 3 1
99 20 7 40
5 15 25 30 35
My result is as follows:
5
10 10
20 20 20
1 1 1 1
3 3 3 3 3
You need 2 loops: one to input the numbers and one to print the pyramid.
public static void main (String[] args) throws java.lang.Exception
{
// Get number of rows
System.out.println("Enter number of rows: ");
Scanner sc = new Scanner(System.in);
int numRows = sc.nextInt();
// Get all the numbers
System.out.println("Enter numbers:");
int numNumbers = numRows * (numRows + 1) / 2;
int [] numbers = new int[numNumbers];
for (int i = 0; i < numNumbers; i++) {
numbers[i] = sc.nextInt();
}
// Print the pyramid
for( ..... Leaving this blank as this looks like homework...) {
}
Related
Trying to figure out how to print a multiplication table.
The code I have right now is:
Scanner scan= new Scanner(System.in);
System.out.print("Please enter number 1-10:");
int n=scan.nextInt();
if(n<=10)
{
for(int m= 1;m<=n;m++)
{
for(int o= 1;o<=n;o++)
{
System.out.print(m*o+"\t");
}
System.out.println();
}
}
else
System.out.print("INVALID");
It prints out :
I'm trying to get it to print out as :
Please read the comments marked with // CHANGE HERE.
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number 1-10: ");
int n = scan.nextInt();
// CHANGE HERE - strict checking
if (n >= 1 && n <= 10)
{
// CHANGE HERE - added to print the first (header) row
for (int m = 0; m <= n; m++)
{
if (m == 0)
System.out.print("\t");
else
System.out.print(m + "\t");
}
System.out.println();
for (int m = 1; m <= n; m++)
{
// CHANGE HERE - added to print the first column
System.out.print(m + "\t");
for (int o = 1; o <= n; o++)
{
System.out.print(m * o + "\t");
}
System.out.println();
}
}
else
System.out.println("INVALID");
}
}
Try this.
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number 1-10:");
int n = scan.nextInt();
if (n <= 10) {
for (int i = 1; i <= n; ++i)
System.out.print("\t" + i);
System.out.println();
for (int m = 1; m <= n; m++) {
System.out.print(m);
for (int o = 1; o <= n; o++) {
System.out.print("\t" + m * o);
}
System.out.println();
}
} else
System.out.print("INVALID");
output:
Please enter number 1-10:7
1 2 3 4 5 6 7
1 1 2 3 4 5 6 7
2 2 4 6 8 10 12 14
3 3 6 9 12 15 18 21
4 4 8 12 16 20 24 28
5 5 10 15 20 25 30 35
6 6 12 18 24 30 36 42
7 7 14 21 28 35 42 49
I need a code that reads the number of tests that will be done and then read values on a matrix for each test.
INPUT:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
My attempt:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Number of tests");
int n = input.nextInt();
String[] name = new String[n];
int test[][] = new int[n][3];
for (int i = 0; i < n; i++) {
System.out.println("Name of the test" + i);
name[i] = input.nextLine();
System.out.println("Values");
for (int j = 0; j < 3; j++) {
test[i][j] = input.nextInt();
}
}
}
You need a 3-D array instead of a 2-D because for each of the test has a 2-D array under it.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
final int ROWS = 3, COLS = 3;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int test[][][] = new int[n][ROWS][COLS];
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
test[i][j][k] = input.nextInt();
}
}
}
// Display
System.out.println("Displaying...");
for (int i = 0; i < n; i++) {
System.out.println("Test " + (i + 1));
for (int j = 0; j < ROWS; j++) {
for (int k = 0; k < COLS; k++) {
System.out.printf("%4d", test[i][j][k]);
}
System.out.println();
}
}
}
}
A sample run:
3
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
Displaying...
Test 1
0 20 10
1 12 34
1 5 6
Test 2
0 22 10
1 10 34
0 0 0
Test 3
0 10 10
0 0 20
0 0 0
I have been trying to find a way to print an inverted pyramid that has a certain number sequence. The sequence needed is as follows as well as what I currently have.
The prompt asked to write a method that takes two numbers and create an inverted pyramid with the first row having a length of the first integer and starting with the number entered second. Then only have the sequence start at 1 after 9 is reached.
Needed: Currently Have:
1 2 4 7 2 7 4 1 2 3 4 5 6 7
3 5 8 3 8 5 8 9 1 2 3 4
6 9 4 9 6 5 6 7 8 9
1 5 1 7 1 2 3 4
6 2 8 5 6 7
3 9 8 9
1 1
static int plotTriangle(int a, int b){
int num = b;
for (int row = a; row >= 0; row--){
for (int i = a; i - row >= 0; i--){
System.out.print(" ");
num += (num+a-row);
num -= 2;
}
for (int i = 0; i <= row; i++){
num++;
while (num >= 10){
num -= 9;
}
System.out.print(num + " ");
}
System.out.println();
}
return 0;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Enter length: ");
int l = in.nextInt();
System.out.print("Enter Start: ");
int s = in.nextInt();
int triangle = plotTriangle(l, s);
}
Try this:
public static void main(String[] args) {
int length = 7;
int[][] numbers = new int[length][length];
int count = 1;
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < (i+1); ++j) {
numbers[i][j] = count++;
if(count > 9)
count = 1;
}
}
for(int i = 0; i < numbers.length; ++i) {
for(int j = 0; j < numbers.length; ++j) {
if(numbers[j][i] == 0)
System.out.print(" ");
else
System.out.print(numbers[j][i]);
System.out.print(" ");
}
System.out.println();
}
}
This will give you your result. Note that I didn't include the dynamic part with the scanner. I used the length and the start-number as constants.
Explanation:
In the first loop I am basically just storing the numbers in an array. And in the second loop this array is printed in a different order.
I am still a beginner in Java and Im really confused in nested loop and how to handle the rows and columns.
My goal is,
enter num: 5
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25.
This is my code so far,
System.out.print("Enter Number: ");
x = in.nextInt();
for(int a = 0; a < x; a++) //rows
{
for(int b = 0; b < x; b++) //columns
{
if(b % 2 == 0){
} else{
}
}
System.out.println();
}
To load a matrix into memory:
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = scanner.nextInt();
}
}
System.out.println(Arrays.deepToString(matrix));
So, I'm trying to create a program that captures 9 numbers in a 2D array, being of size 3 x 3. After capturing all the numbers, they are re-arranged, reversed, and displayed.
So if a user entered:
1 2 3
4 5 6
7 8 9
It should be reversed as this:
9 8 7
6 5 4
3 2 1
So far I have this:
public class Reversenumber {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int val1 = 0;
int val2 = 0;
int[][] array2 = new int[3][3];
int val3 = 0;
int val4 = 0;
for (val1 = 0;val1<3; val1++){
for (val2 = 0;val2<3; val2++){
System.out.println("Please type number for column "+(val1+1)+" and row "+(val2+1));
array1[val1][val2] = kb.nextInt();
}
}
System.out.println("\nThe inverse of this array is:");
for(val1 = 0;val1<3; val1+=1){
for (val2 = 0;val2<3; val2+=1){
System.out.print(array1[val1][val2]+" ");
}
System.out.println("");
}
}
}
The problem I'm having is outputing the inverse of the array. The first for loop captures the information perfectly, but the second set of for loop displays that same information in a 3x3 array, but not inverted. Help anyone?
I hope you don't mind, I changed the variables around completely since they are confusing. also changed the way you input stuff.
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int[][] array2 = new int[3][3];
for (int y = 0; y < array1.length; y++) {
System.out.println("input 3 numbers for row " + (y + 1));
for (int x = 0; x < array1[y].length; x++) {
array1[y][x] = kb.nextInt();
}
}
System.out.println("\nThe inverse of this array is:");
for (int y = array1.length; --y >= 0;) {
for (int x = array1[y].length; --x >= 0;) {
System.out.printf("%3d", array1[y][x]);
}
System.out.println();
}
output
input 3 numbers for row 1
1 2 3
input 3 numbers for row 2
4 5 6
input 3 numbers for row 3
7 8 9
The inverse of this array is:
9 8 7
6 5 4
3 2 1
So instead of inputting each separately you put 3 on the same line and put a space between them.
Assuming you mean invert like so:
1 2 3
4 5 6
7 8 9
to
1 4 7
2 5 8
3 6 9
It's as simple as keeping the loop the same, but swapping the coordinates between the arrays:
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int[][] array1 = new int[3][3];
int val1 = 0;
int val2 = 0;
int[][] array2 = new int[3][3];
int val3 = 0;
int val4 = 0;
for (val1 = 0; val1 < 3; val1++) {
for (val2 = 0; val2 < 3; val2++) {
System.out.println("Please type number for column " + (val1 + 1) + " and row " + (val2 + 1));
array1[val1][val2] = kb.nextInt();
}
}
System.out.println("\nOriginal array is:");
for (val1 = 0; val1 < 3; val1 += 1) {
for (val2 = 0; val2 < 3; val2 += 1) {
System.out.print(array1[val1][val2] + " ");
}
System.out.println("");
}
System.out.println("\nInverted array is:");
for (int x = 0; x < 3; x += 1) {
for (int y = 0; y < 3; y += 1) {
System.out.print(array1[y][x] + " ");
}
System.out.println("");
}
System.out.println("\nThe Reversed array is:");
for (int x = 2; x >= 0; x -= 1) {
for (int y = 2; y >= 0; y -= 1) {
System.out.print(array1[x][y] + " ");
}
System.out.println("");
}
}
Edit: I added the reversed scenario you asked about in the comment below. You actually expected output like this:
9 8 7
6 5 4
3 2 1