I am trying to output 12 times tables. Where it will go through all the times tables from 1 to 12. And each times table will go up to *12. For examples, the output should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
...
12 24 36 48 60 72 84 96 108 120 132 144
Here is the code I have so far, I seem to be very close. But I am struggling with perfecting it.
public class Tables {
public void generateTable()
{
//Put the code for your times-table here
int i;
int j;
for(i=1; i<=12; i++)
{
for(j=i; j<=i*12; j = j+1)
{
System.out.print(j*i + " ");
}
System.out.println();
}
}
public static void main(String args[])
{
Tables t = new Tables();
t.generateTable();
}
}
I am receiving the following output for this code:
1 2 3 4 5 6 7 8 9 10 11 12
4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108
...
In the inner loop, you only want to go from 1 to 12 instead of from i to i*12.
You could also declare i and j more local in the respective loop where the initialization is done.
Related
I have a data file which consist of Marks of two subjects.
The file look like that
Sl_No: Marks1 Marks2
1 10 20
2 10 20
3 10 20
4 10 20
5 10 10
6 20 10
7 20 10
8 20 10
9 20 30
10 20 22
11 21 22
12 21 22
13 21 23
14 10 20
15 10 20
Now my objective is that whenever it get same pair of value add this marks. So as per my example sl_no 1 to 4 have same pairs of values (10,20). So it return 40 for Marks1 and 80 for marks2. Sl_No: 5 doesnot match with sl_no: 4 so it is remain same. Serial no: 6 to 8 have a matching pair so it return 60 and 30. Sl_no: 9.0 and 10.0 has no match so it remain same as it is. Sl_no: 11 and 12 had a match so return 42 and 44.and sl_no: 14 and 15 had a match so return 20 and 40.
Desired output:
4 40 80
5 10 10
8 60 30
9 20 30
10 20 22
12 42 44
13 21 23
15 20 40
My Code:
public class Marksmatch {
public static void main(String args[]){
int []sl_no={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int []Marks1={10,10,10,10,10,20,20,20,20,20,21,21,21,10,10};
int []Marks2={20,20,20,20,10,10,10,10,30,22,22,22,23,20,20};
int addMarks1=0,addMarks2=0;
for(int i=0;i<sl_no.length-1;i++){
if(Marks1[i]== Marks1[i+1]&&Marks2[i]==Marks2[i+1]){
addMarks1=addMarks1+Marks1[i+1];
addMarks2=addMarks2+Marks2[i+1];
System.out.println(addMarks1);
System.out.println(addMarks2);
}
}
}
}
Output:
10
20
20
40
30
60
50
70
70
80
91
102
101
122
It did not give me the desire output. How could I proceed?
Your logic is wrong. You should keep track of the sums of the current group, and only print them once you are done with the group:
int addMarks1=Marks1[0],addMarks2=Marks2[0]; // initialize the sums of the first group
for(int i=0;i<sl_no.length-1;i++){
if(Marks1[i]== Marks1[i+1] && Marks2[i]==Marks2[i+1]) {
// add to the current group
addMarks1=addMarks1+Marks1[i+1];
addMarks2=addMarks2+Marks2[i+1];
} else {
// print the previous group and start a new group
System.out.println(sl_no[i] + " " + addMarks1 + " " + addMarks2);
addMarks1=Marks1[i+1];
addMarks2=Marks2[i+1];
}
}
// print the last group
System.out.println(sl_no[sl_no.length-1] + " " + addMarks1 + " " + addMarks2)
Output:
4 40 80
5 10 10
8 60 30
9 20 30
10 20 22
12 42 44
13 21 23
15 20 40
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In order to prevent duplicates in my random number generator, I want to make an if statement that tests if my random number (x) is the same as any other previous values of x. If it is, then it returns and restarts the loop.
How do I have my code remember all of the previous values of x?
Pseudocode:
if (x == previous value of x) {
return;
}
Explanation: For example, you can make a list that contains 8 numbers and after that shuffle the list in order to be randomized what you want.
Code:
Integer[] array = new Integer[100];
for (int i = 0; i < 100; i++) {
array[i] = i+1;
}
List<Integer> list = Arrays.asList(array);
Collections.shuffle(list);
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
Output:
88 25 24 15 92 87 14 100 68 98 91 32 28 22 74 37 10 90 44 77 94 52 34
86 72 99 46 71 36 26 69 55 82 79 19 78 60 49 70 85 33 66 45 57 61 9 51
76 3 42 16 29 84 93 58 2 53 97 6 17 13 43 12 56 35 96 83 40 38 59 50 5
41 65 11 23 73 4 47 89 1 81 21 80 63 48 20 27 64 54 18 75 30 62 39 31
7 8 67 9
Use a list to record the previous values of x. List are better than arrays for this purpose, because you don't need to resize them if you want to generate more random numbers.
List<int> randomNumberList = new ArrayList<int>();
randomNumberList.add(randomNumberThatYouGenerated);
then just use an if:
if (randomNumberList.contains(randomNumberThatYouGenerated)) {
//code
}
Use a array to record the previous value of x..... and check if the currently value inside that array..
Hello i got one task to do. output should be like this:
{
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
}
I tried to do it: here is my code:
public class ContnueUzOznakoMojNacin
{
public static void main(String args[])
{
int k=0, v=0;
int j;
for(int i=0;i<10;i++)
{
for(j=10-i;j<10;j++)
{
System.out.print(v+" ");
v+=k;
}
System.out.println();
v=0;
k++;
}
}
}
And the output i get is wrong and i dont get it why.
here it is:
{
0
0 2
0 3 6
0 4 8 12
0 5 10 15 20
0 6 12 18 24 30
0 7 14 21 28 35 42
0 8 16 24 32 40 48 56
0 9 18 27 36 45 54 63 72
}
When i follow these loops and increments from my program i cant find mistake.
first line is ok output should be 0;
but second line, output should be 0 1; not 0 2 ?
I dont need you to give me code for this task i need you to help me to do it, to tell me where i made mistake so i do it on my own. Thanks :)
Change this line:
for(j=10-i;j<10;j++)
to this:
for(j=9-i;j<10;j++)
And here's an explanation:
So i starts at 0, right? What will be i's maximum? 9, because in for(int i=0;i<10;i++), i cannot get to 10.
Let's look at how that affects for(j=10-i;j<10;j++). If i is 9 (the last row), then the j loop will only run 9 times. j will equal 1,2,3,4,5,6,7,8,9. That's only 9 loops. If you look at the bottom of the upper triangle, you can see that 0 9 18 27 36 45 54 63 72 has only 9 numbers.
We want j to run 10 times, as you can see by the base of the correct triangle: 0 9 18 27 36 45 54 63 72 81. How do we do this? We make j run one more time on every i loop, by decreasing the starting number (10-i) by one (which equals 9-i). That is how you arrive at
for(j=9-i;j<10;j++)
Have a problem with my output when creating a multiplication table 1-12 inclusive. below is my code and output
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d\n",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
}
}
}
and my output is
Below is a multiplication table from 1-12 inclusive
1
1 2 3 4 5 6 7 8 9 10 11 12 2
2 4 6 8 10 12 14 16 18 20 22 24 3
3 6 9 12 15 18 21 24 27 30 33 36 4
4 8 12 16 20 24 28 32 36 40 44 48 5
5 10 15 20 25 30 35 40 45 50 55 60 6
6 12 18 24 30 36 42 48 54 60 66 72 7
7 14 21 28 35 42 49 56 63 70 77 84 8
8 16 24 32 40 48 56 64 72 80 88 96 9
9 18 27 36 45 54 63 72 81 90 99 108 10
10 20 30 40 50 60 70 80 90 100 110 120 11
11 22 33 44 55 66 77 88 99 110 121 132 12
12 24 36 48 60 72 84 96 108 120 132 144
My problem is in getting the far right column to be on the far left. I've tried researching why only the number '1' appears and then it jumps but cant find anything
Your problem is the first of your two printf statements. You should just be able to replace it with System.out.printf("\n");.
(The "jump" to which you refer is a newline, which is represented by the \n character.)
Try this:
public class Multiplication
{
public static void main(String[]args)
{
int row,column;
System.out.println("Below is a multiplication table from 1-12 inclusive");
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
System.out.printf("\n");
}
}
}
for (row=1;row<=12;row++)
{
System.out.printf("%4d",row);
for (column=1;column<=12;column++)
System.out.printf("%6d",row*column);
if(column==12) System.out.printf("\n");
}
I have a set of numbers {'1','13','25','32','49',...}, i want to calculate all possible combinations of this numbers of order k.
Esample1:
set = {'1','5','23','41,'54','63'};
k = 4;
Output1:
1 5 23 41
1 5 23 54
1 5 23 63
1 5 41 54
1 5 41 63
1 5 54 63
1 23 41 54
1 23 41 63
1 23 54 63
1 41 54 63
5 23 41 54
5 23 41 63
5 23 54 63
5 41 54 63
23 41 54 63
Example2:
set = {'a','v','f','z'};
k=3;
Output2:
a v f
a v z
a f z
v f z
in Java plaese.
Thank you!
You should be able to find an appropriate algorithm in D.Knuth's The Art of Computer Programming, Volume 4, fascicle 3 - Generating All Combinations, which can be downloaded from his website.