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
Related
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));
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...) {
}
1
2 3
4 5 6
I have to print the triangle using single for loop.
I have tried using two for loops and i did it successfully but i have to solve it using single for loop.
The trick is that the last number of the current row is the sum of the previous row last number and the number of the row
In other words: lastNum = prevLastNumber + rowNum
int row = 1;
int last = 0;
for (int i = 1; i < 37; i++) {
if (i < (row + last)) {
System.out.print(i + " ");
} else {
System.out.print(i + "\n");
row++;
last = i;
}
}
And the output is the following:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
Try this.
public class Pyramid7Floyds {
public static void main(String[] args) {
int nextNumber = 1;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(nextNumber<10 ? (" " + nextNumber++) : (" " + nextNumber++) ); //2spaces in single digit & 1 space in double digit.
//System.out.format("%3d",nextNumber++ ); //You may use this line for formatting as a replacement of above line. (comment above line before using this)
}
System.out.println();
}
}
}
public class HelloWorld{
public static void main(String []args){
int j=1;
for(int i=1;i<=6;i++){
System.out.print(i+" ");
if (i==j){
System.out.print('\n');
j=2*j+1;
}
}
}
}
There's this problem from my programming class that I can't get right... The output must be all odd numbers, in odd amounts per line, until the amount of numbers per line meets the odd number that was entered as the input. Example:
input: 5
correct output:
1
3 5 7
9 11 13 15 17
If the number entered is even or negative, then the user should enter a different number. This is what I have so far:
public static void firstNum() {
Scanner kb = new Scanner(System.in);
int num = kb.nextInt();
if (num % 2 == 0 || num < 0)
firstNum();
if (num % 2 == 1)
for (int i = 0; i < num; i++) {
int odd = 1;
String a = "";
for (int j = 1; j <= num; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
firstNum();
}
The output I'm getting for input(3) is
1 3 5
1 3 5
1 3 5
When it really should be
1
3 5 7
Can anyone help me?
Try this:
public static void firstNum() {
Scanner kb = new Scanner(System.in);
int num = kb.nextInt();
while (num % 2 == 0 || num < 0) {
num = kb.nextInt();
}
int odd = 1;
for (int i = 1; i <= num; i += 2) {
String a = "";
for (int j = 1; j <= i; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
}
if (num % 2 == 1) {
int odd = 1;
}
for (int i = 0; i < num; i++) {
String a = "";
for (int j = 1; j <= odd; j++) {
a = odd + " ";
odd += 2;
System.out.print(a);
}
System.out.println();
}
You should assign odd before for loop.
In inner for loop compare j and odd together.
For questions like this, usually there is no need to use and conditional statements. Your school probably do not want you to use String as well. You can control everything within a pair of loops.
This is my solution:
int size = 7; // size is taken from user's input
int val = 1;
int row = (size / 2) + 1;
for (int x = 0; x <= row; x++) {
for (int y = 0; y < (x * 2) + 1; y++) {
System.out.print(val + " ");
val += 2;
}
System.out.println("");
}
I left out the part where you need to check whether input is odd.
How I derive my codes:
Observe a pattern in the desired output. It consists of rows and columns. You can easily form the printout by just using 2 loops.
Use the outer loop to control the number of rows. Inner loop to control number of columns to be printed in each row.
The input number is actually the size of the base of your triangle. We can use that to get number of rows.
That gives us: int row = (size/2)+1;
The tricky part is the number of columns to be printed per row.
1st row -> print 1 column
2nd row -> print 3 columns
3rd row -> print 5 columns
4th row -> print 7 columns and so on
We observe that the relation between row and column is actually:
column = (row * 2) + 1
Hence, we have: y<(x*2)+1 as a control for the inner loop.
Only odd number is to be printed, so we start at val 1 and increase val be 2 each time to ensure only odd numbers are printed.
(val += 2;)
Test Run:
1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
33 35 37 39 41 43 45 47 49
You can use two nested loops (or streams) as follows: an outer loop through rows with an odd number of elements and an inner loop through the elements of these rows. The internal action is to sequentially print and increase one value.
a loop in a loop
int n = 9;
int val = 1;
// iterate over the rows with an odd
// number of elements: 1, 3, 5...
for (int i = 1; i <= n; i += 2) {
// iterate over the elements of the row
for (int j = 0; j < i; j++) {
// print the current value
System.out.print(val + " ");
// and increase it
val += 2;
}
// new line
System.out.println();
}
a stream in a stream
int n = 9;
AtomicInteger val = new AtomicInteger(1);
// iterate over the rows with an odd
// number of elements: 1, 3, 5...
IntStream.iterate(1, i -> i <= n, i -> i + 2)
// iterate over the elements of the row
.peek(i -> IntStream.range(0, i)
// print the current value and increase it
.forEach(j -> System.out.print(val.getAndAdd(2) + " ")))
// new line
.forEach(i -> System.out.println());
Output:
1
3 5 7
9 11 13 15 17
19 21 23 25 27 29 31
33 35 37 39 41 43 45 47 49
See also: How do I create a matrix with user-defined dimensions and populate it with increasing values?
Seems I am bit late to post, here is my solution:
public static void firstNum() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the odd number: ");
int num = scanner.nextInt();
if (num % 2 == 0 || num < 0) {
firstNum();
}
if (num % 2 == 1) {
int disNum = 1;
for (int i = 1; i <= num; i += 2) {
for (int k = 1; k <= i; k++, disNum += 2) {
System.out.print(disNum + " ");
}
System.out.println();
}
}
}
This question already has answers here:
Pascal's triangle 2d array - formatting printed output
(5 answers)
Closed 1 year ago.
The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed.
public static void triangle(int maxRows) {
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
}
I need to format the values of the triangle such that it looks like a triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
I can't for the life of me figure out how to do that. Please answer keeping in mind that I'm a beginner in Java programming.
public static long pascalTriangle(int r, int k) {
if (r == 1 || k <= 1 || k >= r) return 1L;
return pascalTriangle(r - 1, k - 1) + pascalTriangle(r - 1, k);
}
This method allows you to find the k-th value of r-th row.
This is a good start, where it's homework, I'll leave the rest to you:
int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++) {
num = 1;
r = i + 1;
//pre-spacing
for (int j = maxRows - i; j > 0; j--) {
System.out.print(" ");
}
for (int col = 0; col <= i; col++) {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
}
System.out.println();
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
In each row you will need to print:
n spaces
m numbers
n spaces
Your job is to figure out n (which will be zero in the last line) and m based on row number.
[This is more like a comment but I needed more formatting options than comments provide]
You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function
System.out.printf();
Here is a handy reference guide
Also note that you will need to take into account that some numbers are more than 1 digit long!
import java.util.*;
class Mine {
public static void main(String ar[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i < n; i++) {
int size = 1;
for (int j = 1; j <= i; j++) {
int a[] = new int[size];
int d[] = new int[size];
for (int k = 1; k <= size; k++) {
a[1] = 1;
a[size] = 1;
for (int p = 1; p <= size; p++) {
d[p] = a[p];
}
if (size >= 3) {
for (int m = 2; m < size; m++) {
a[m] = d[m] + d[m - 1];
}
}
}
for (int y = 0; y < size; y++) {
System.out.print(a[y]);
}
System.out.println(" ");
}
++size;
}
}
}
public class HelloWorld {
public static void main(String[] args) {
int s = 7;
int k = 1;
int r;
for (int i = 1; i <= s; i++) {
int num = 1;
r = i;
int col = 0;
for (int j = 1; j <= 2 * s - 1; j++) {
if (j <= s - i)
System.out.print(" ");
else if (j >= s + i)
System.out.print(" ");
else {
if (k % 2 == 0) {
System.out.print(" ");
} else {
if (col > 0) {
num = num * (r - col) / col;
}
System.out.print(num + " ");
col++;
}
k++;
}
}
System.out.println("");
k = 1;
}
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
You can try this code in java. It's simple :)
public class PascalTriangle {
public static void main(String[] args) {
int rows = 10;
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.format("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.format("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
Code perfectly prints pascal triangle:
public static void main(String[] args) {
int a, num;
for (int i = 0; i <= 4; i++) {
num = 1;
a = i + 1;
for (int j = 4; j > 0; j--) {
if (j > i)
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
if (j > 0)
num = num * (a - j) / j;
System.out.print(num + " ");
}
System.out.println();
}
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1