Java Multiplication Table - java

I'm making a java program that shows the multiplication table that looks like this:
1
But I can only get the results from 1 to 5th column. How do I make the rest appear below ?
The program must only contain one for() nested loop.
This is my code so far:
import java.util.Scanner;
public class Table{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 1 ;i<=10;i++) {
for(int j=1;j<=inputi && j <= 5;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
if(i >= 5)
for(int j = 6; j <= inputi && j <= 10; j++){
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
}
System.out.println();
System.out.println();
}
}
Can anyone help ?
Thanks.
Edit: Sample input added.
Inputi = 7
Expected output:
Actual output:

I would suggest to
Create separate variable with number of required columns
Calculate how many rows will be printed (see iterations)
Print rows one by one
Please, see the code snippet below:
int inputi = 12;
int columns = 5;
int iterations = inputi / columns + (inputi % columns > 0 ? 1 : 0);
for (int iter = 0; iter < iterations; iter++) {
for (int i = 1; i <= 10; i++) {
for (int j = iter * columns + 1; j <= Math.min(inputi, (iter + 1) * columns); j++) {
System.out.print(j + " x " + i + " = " +(i * j) + "\t\t");
}
System.out.println();
}
System.out.println();
}
Note, in case inputi is huge, you may have to fill outputs with additional spaces, to avoid layout issues, when you have statements like 1 x 1 = 1 and 1 x 10000000 = 1000000

This algorithm with only 2 layers of for loops will give you the correct output.
import java.util.Scanner;
public class Table {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 0; i < Math.ceil(inputi / 5.0) * 10; i++) {
if ( i > 0 && i % 10 == 0 ) System.out.println();
int t = inputi - 5 * (i / 10);
for(int j = 0; j < (t > 5 ? 5 : t); j++) {
int a = 5 * (i / 10) + j + 1;
int b = i % 10 + 1;
System.out.print(a + " x " + b + " = " + (a * b) + "\t");
}
System.out.println();
}
}
}
Here is the sample output for input=11:
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
1 x 10 = 10 2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = 10
6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18 10 x 2 = 20
6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27 10 x 3 = 30
6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36 10 x 4 = 40
6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45 10 x 5 = 50
6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54 10 x 6 = 60
6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63 10 x 7 = 70
6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72 10 x 8 = 80
6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81 10 x 9 = 90
6 x 10 = 60 7 x 10 = 70 8 x 10 = 80 9 x 10 = 90 10 x 10 = 100
11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110

This will give you the desired output:
import java.util.Scanner;
public class Table{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int inputi = s.nextInt();
for(int i = 1 ;i<=10;i++) {
for(int j=1;j<=inputi && j <= 5;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
}
System.out.println();
System.out.println();
for(int i = 1 ;i<=10;i++) {
for(int j=6;j<=inputi && j <= 10;j++) {
System.out.print(j + " x " + i + " = " +(i*j) + "\t");
}
System.out.println();
}
}
}

Related

Pascaline triangle logic not working in java

I am trying to make a java program to print the Pascaline triangle. But it is not working properly. The code is provided below :
int rows=10;
int[] array=new int[10], temp=new int[10];
array[0]=1;
temp[0]=1;
System.out.println(1);
for(int i=1;i<rows;i++)
{
for(int j=1;j<=i;j++)
{
temp[j]=array[j-1]+array[j];
}
for(int term:temp)
{
System.out.print(term+"\t");
}
System.out.println();
array=temp;
}
It is giving the following output :
1
1 1
1 2 3
1 3 5 5
.....
Please tell what's wrong with the code.
Pascaline triangle is not factorial serial
A proposal is (warning I am not a Java programmer, please don't be rude with me if something is stupid / can be improved easily) :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
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
and to be a little prettier :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j < n-i; j++)
System.out.print(" ");
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
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

Swapping 2D matrix diagonal with a middle line

I have a Java 2D array
String arraylist numberSeq[][];
inside the array list, there is number from 1 to 25,
numberSeq[0][0] = 1, [0][1] = 2, [0][2] = 3 , [0][3] = 4 , [0][4] = 5
numberSeq[1][0] = 6, [1][1] = 7, [1][2] = 8 , [1][3] = 9 , [1][4] = 10
......
numberSeq[4][0] = 21,[4][1] = 22,[4][2] = 23, [4][3] = 24, [4][4] = 25
So the number will be like
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
After doing a diagonals swap, I wish the output to be like
25 20 15 10 5
24 19 14 9 4
23 18 13 8 3
22 17 12 7 2
21 16 11 6 1
How can I achieve that when I can only declare one local variable?
If one local variable is not achievable, how much is the minimum number of local variables I need?
This should help. so yes, a swap with a single local variable is possible.
public swapDiagonally(int[][] mtx) {
for(int i = 0 ;i< mtx.length; i++){
for(int j = 0; j < mtx[0].length - i; j++){
int temp = mtx[i][j];
mtx[j][i]; = mtx[mtx.length-1-i][mtx[0].length-1-j];
mtx[mtx.length-1-i][mtx[0].length-1-j] = temp;
}
}
}
I am essentially traversing 'total rows - N' for every Nth column, thus, helping me travers this structure:
1 2 3 4
1 2 3
1 2
1
for a 4x4 array!!!
the following loop will do the diagonal swap as in your question:
int n=5;
int[][] newmat = new int[5][5];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newmat[(n-1) - j][(n-1)- i] = matrix[i][j];
}
}
You can do this by making another teo dimensional array, iterating through your first in the right order and post it to a new one.
public class MyClass {
public static void main(String args[]) {
int dimension = 5;
int[][] numberSeq = new int[dimension][dimension];
numberSeq[0][0] = 1; numberSeq[0][1] = 2;numberSeq[0][2] = 3;numberSeq[0][3] = 4;numberSeq[0][4] = 5;
numberSeq[1][0] = 6; numberSeq[1][1] = 7;numberSeq[1][2] = 8;numberSeq[1][3] = 9;numberSeq[1][4] = 10;
numberSeq[2][0] = 11;numberSeq[2][1] = 12;numberSeq[2][2] = 13;numberSeq[2][3] = 14;numberSeq[2][4] = 15;
numberSeq[3][0] = 16;numberSeq[3][1] = 17;numberSeq[3][2] = 18;numberSeq[3][3] = 19;numberSeq[3][4] = 20;
numberSeq[4][0] = 21;numberSeq[4][1] = 22;numberSeq[4][2] = 23;numberSeq[4][3] = 24;numberSeq[4][4] = 25;
int[][] flippedSeq = new int[dimension][dimension];
// This is the flipping part
for(int i = 0; i < dimension; i++)
{
for(int j = 0; j < dimension; j++)
{
flippedSeq[i][j] = numberSeq[dimension-1-j][dimension-1-i];
}
}
PrintMatrix(numberSeq);
System.out.println();
PrintMatrix(flippedSeq);
}
public static void PrintMatrix(int[][] mat)
{
for(int i = 0; i < mat.length; i++)
{
for(int j = 0; j < mat[i].length; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
}
}
I hope thats what you meant by "1 local variable".
Output is:
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
25 20 15 10 5
24 19 14 9 4
23 18 13 8 3
22 17 12 7 2
21 16 11 6 1

How can I print these numbers in correspoding location with array?

import java.util.*;
import java.io.*;
public class PracticeFinal {
public static void main (String[] args) throws FileNotFoundException{
Scanner input = new Scanner (new File("altitude.txt"));
int[] x = new int [400];
int[] y = new int [400];
float[] altitude = new float [400];
x[0] = 0;
y[0] = 0;
altitude[0] = 0;
int i = 0;
System.out.println("x\ty\taltitude");
while (input.hasNextFloat()) {
int line = input.nextInt();
x[i]=line;
int line2=input.nextInt();
y[i]=line2;
float line3 = input.nextFloat();
altitude[i]=line3;
System.out.println(x[i]+"\t" + y[i]+"\t"+ altitude[i]+"\t");
i+=1; }
printMax(altitude,x,y);
printMin(altitude,x,y);
printAverage(altitude);
printContour(altitude,x,y);
}
public static void printMax(float []altitude, int []x, int []y) {
float max=altitude[0];
int ind=0;
for(int i=0;i<100;i++)
{
if (altitude[i]>max)
{
max = altitude[i];
ind=i;
}
}
System.out.println("The maximum altitude is "+"("+x[ind]+","+y[ind]+","+max+")");
}
public static void printMin(float []altitude, int []x, int []y){
float min=altitude[0];
int ind=0;
for(int i=0;i<100;i++)
{
if (altitude[i] < min)
{
min = altitude[i];
ind=i;
}
}
System.out.println("The minimum altitude is "+"("+x[ind]+","+y[ind]+","+min+")");
}
public static void printAverage(float []altitude){
float total = 0;
for(int i=0;i<100;i++)
{
total += altitude[i];
}
float avg = total / 100;
System.out.printf("The aveage altitude of the region is " + "%.2f", avg);
System.out.println();
}
public static void printContour(float []altitude, int[]x, int[]y){
for (int i=0;i<100;i++) {
System.out.print (altitude[i] + " ");
}
}
}
I need help with the last part of this program there is an x and y chart with altitudes ex, 0,0 = 109.1 1,0= 200
So for the last method i need to print out the altitude in its corresponding location starting from 1,1 to 2,1 to 3,1 and so on
Dont know what to do next after the for loop
This is the file im reading first number is x second is y and third is the altitude.
1 1 101.0
2 2 97.2
3 3 112.3
4 4 114.2
5 5 100.2
6 6 97.5
7 7 97.8
8 8 81.3
9 9 105.4
10 10 108.7
3 1 107.8
3 2 115.4
3 4 118.3
3 5 120.3
3 6 122.3
3 7 90.3
3 8 81.7
3 9 87.4
3 10 113.2
1 2 102.3
1 3 104.5
1 4 109.8
1 5 99.8
1 6 88.9
1 7 89.3
1 8 100.1
1 9 110.8
1 10 98.3
2 1 98.8
2 3 80.5
2 4 85.1
2 5 83.2
2 6 92.3
2 7 94.3
2 8 199.3
2 9 104.3
2 10 105.2
4 1 120.5
4 2 87.3
4 3 82.3
5 1 83.2
5 2 84.5
5 3 96.9
5 4 86.7
4 5 115.3
4 6 101.3
4 7 102.6
4 8 106.9
4 9 109.8
4 10 93.4
6 7 93.2
6 8 92.4
6 9 80.9
6 10 81.2
5 6 102.3
5 7 101.1
5 8 106.7
5 9 105.3
5 10 88.9
6 1 80.8
6 2 81.3
6 3 84.5
6 4 90.8
6 5 99.8
8 1 98.6
8 2 101.1
8 3 103.5
8 4 104.6
8 5 105.8
8 6 80.9
8 7 80.1
7 1 98.3
7 2 96.4
7 3 95.1
7 4 83.4
7 5 95.1
7 6 96.3
7 8 99.9
7 9 100.0
7 10 102.3
10 3 99.9
10 4 98.3
10 5 91.3
10 6 94.3
10 7 95.3
10 8 103.2
8 9 90.2
8 10 91.3
9 1 93.2
9 2 81.5
9 3 91.0
9 4 95.3
9 5 122.3
9 6 119.8
9 7 118.7
9 8 117.3
9 10 107.8
10 1 108.8
10 2 109.9
10 9 104.2
public static void printContour(float []altitude, int[]x, int[]y){
for (int i = 0; i < 100; i++) {
for (int a = 0; a < 100; a++) {
if (x[i] < x[a]) {
int temp_for_x = x[i];
x[i] = x[a];
x[a] = temp_for_x;
int temp_for_y = y[i];
y[i] = y[a];
y[a] = temp_for_y;
float temp_for_alt = altitude[i];
altitude[i] = altitude[a];
altitude[a] = temp_for_alt;
}
if(y[i] < y[a]){
int temp_for_x = x[i];
x[i] = x[a];
x[a] = temp_for_x;
int temp_for_y = y[i];
y[i] = y[a];
y[a] = temp_for_y;
float temp_for_alt = altitude[i];
altitude[i] = altitude[a];
altitude[a] = temp_for_alt;
}
}
}
for (int incr=0;incr<100;incr++) {
System.out.print(x[incr]+ " " + y[incr] + " " + altitude[incr]+ " ");
if(incr%10 == 9)
System.out.println();
}
}
}
There are better sorting algorithms you can use Here

Printing out a multiplication table

I'm going through the Java Tutorial on HackerRank using Java 8. The goal is to print out a multiplication table of 2 from 1 - 10.
Here is what I came up with
public static void main(String[] args) {
int x = 2;
int y = 0;
int z;
while (y < 10) {
z = x * y;
y++;
System.out.println(x + " x " + y + " = " + z);
}
Here is the output I get from the code above
2 x 1 = 0
2 x 2 = 2
2 x 3 = 4
2 x 4 = 6
2 x 5 = 8
2 x 6 = 10
2 x 7 = 12
2 x 8 = 14
2 x 9 = 16
2 x 10 = 18
I've also tried while <= 10 instead of while < 10 as shown in my code above and for that my result was:
2 x 1 = 0
2 x 2 = 2
2 x 3 = 4
2 x 4 = 6
2 x 5 = 8
2 x 6 = 10
2 x 7 = 12
2 x 8 = 14
2 x 9 = 16
2 x 10 = 18
2 x 11 = 20
Neither of this outputs is what I'm looking for. Logically I am confident my code makes sense and should work so I'm looking for someone to give me tips as to something I may have missed or maybe I've made a mistake and I'm not aware of it. I am not looking for the code to the right answer, but rather advice and/or pointers which will allow to come up with a working solution on my own.
Start your y value at 1
Don't increment your y value until after the print statement
public static void main(String[] args) {
int x = 2;
int y = 1; //starts at 1
int z;
while (y < 10) {
z = x * y;
System.out.println(x + " x " + y + " = " + z);
y++; // increment y after the print statement
}
}
Assign value of y = 1 and increment it after your system.out.println();

java nested for loops to get numbers triangle [duplicate]

This question already has answers here:
Java - creating a triangle with numbers using nested for-loops [closed]
(2 answers)
Closed 8 years ago.
my output should look like in image 1, but my output looks like in image 2.
I am not suppose to print out ... there I have to print out same thing with 32 then 64. int
That is what i have so far, I got half of the triangle correct. I don't know how to reverse it though.
k = 1;
int j;
int l = 1;
for(int i=1; i <= 8; i++){
for(j=8; j>i; j--){
System.out.print(" ");
}
for(j=1; j<=k; j=j*2){
System.out.print(j + " ");
}
for (j = 1; j<k; j=j*2) {
System.out.print(j + " ");
}
k = k * 2;
System.out.println();
}
}
}
Your problem is, in the 2nd loop, you still go from j=1 -> k. You can simply do a k -> 1 loop to get a reversed sequence.
Also java has printf method, you may want to take a look..
Some example codes:
int rows = 8;
for (int r = 0; r <= rows; r++) {
System.out.print(new String(new char[rows - r]).replace("\0", " "));
int c = 0;
for (int i = 0; i <= r; i++)
System.out.printf("%s%s", 1<<i, r == 0? "\n" : " ");
if (r > 0)
for (int i = r-1; i >= 0; i--)
System.out.printf("%s%s", 1<<i, i == 0? "\n" : " ");
}
just adjust the rows to the value you like.
I did a test with rows=8, it prints:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1

Categories