public class testing
{
public void show() {
int num = 0;
int n = 5;
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) {
System.out.print(num++);
System.out.print(" ");
}
System.out.println(" ");
}
}
}
This question came up in one of our previous exams and I don't understand it. The answer is
0
1 2
3 4 5
6 7 8 9
But I have no clue how they got it. I kind of understand the 2nd to 4th line but have no clue how they got 0 on the first line. Any explanation would be highly appreciated, thanks!
but have no clue how they got 0 on the first line ?
int num = 0; --> it is 0 initially
For the first iteration your inner loop executes only 1 time
for (int i=1; i<n; i++) {
for (int j=0; j<i; j++) { ---> for(int j=0;j<1;j++) // for 1st time
So that is why the below line
System.out.print(num++); //printed 0
Note : there is a tool known as debugger , Use it !!
Maybe the following amended code could help to understand
int num = 0;
int n = 5;
for (int i=1; i<n; i++) { // loop from 0 to 4
System.out.printf("num=%d i=%d : ", num, i);
for (int j=0; j<i; j++) { // loop from 0 to i
System.out.print(num++); // print num then increment num
System.out.print(" ");
}
System.out.println(" ");
}
output
num=0 i=1 : 0
num=1 i=2 : 1 2
num=3 i=3 : 3 4 5
num=6 i=4 : 6 7 8 9
I believe your problem lies in this line
System.out.print(num++);
in a more verbose way it does the following
System.out.print(num);
num = num + 1;
Related
Below is the code I'm messing with, pretty basic. I was looking for a way to print only specific elements of the array. For example, if I wanted to print only the element at index 1 of array[i], as well as the element at index 1 of array[j] when its value is 1. See below for the output I'm looking for.
Expected output :
1 3 5
4
7 8 9
Code:
public class multiDimensional {
public static void main(String args[]){
int arr[][] = {{1,3,5}, {2,4,6}, {7,8,9}};
for(int i=0; i < 3; i++){
for(int j=0; j < 3; j++){
System.out.print(arr[i][j]+" ");
//System.out.println();
}
System.out.println();
}
}
}
Actual output :
1 3 5
2 4 6
7 8 9
You can produce your expected output by using an if statement to decide when to write a value:
public static void main(String args[]){
int arr[][] = {{1,3,5}, {2,4,6}, {7,8,9}};
for(int i=0; i < 3; i++){
for(int j=0; j < 3; j++){
if (i == 0 // in the first row
|| i == 2 // in the last row
|| j == 1) { // in the middle column of the middle row
System.out.print(arr[i][j]+" ");
} else {
System.out.print(" "); // this is here to keep the spacing right
}
//System.out.println();
}
System.out.println();
}
}
Note: There are many other ways of coding this, however the approach I am showing is merely to demonstrate how the if statement works.
I was having trouble with this program that asks me : Write a program that generates 10 integers between 0-9 and displays the the count for each number. I am having trouble on the count occurrence for each of the single digit numbers. It seems my program dosen't add the counter right. Here is my program:
I've tried to read other code to help fix it but nothing worked.
import java.util.*;
import java.util.Arrays;
public class CountsArray {
public static void main(String[] args) {
int[] list = new int[10];
int[] num = new int[10];
for(int i = 0; i< list.length;i++) {
list[i] = (int)(Math.random()*10+1);
}
for(int i = 0; i< list.length;i++) {
System.out.print(list[i] + " ");
}
System.out.println();
int temp = 0;
for(int i = 0; i <num.length;i++) {
temp = num[i];
list[temp]++;
}
for(int i = 0;i < list.length;i++) {
if(list[i]>0 && list[i]==1) {
System.out.printf("%d occurs %d time\n",i, list[i]);
}
else if(list[i] >= 2) {
System.out.printf("%d occurs %d times \n", i, num);
}
}
}}
If the random numbers is : 1 1 2 2 2 3 3
The output should be 1 occured 2 times, 2 occured 3 times, 3 occured 2 times.
Could anyone point me in the right direction?
You can do it this way , first since random integers are 0 to 9 so adding 1 to Math.random() will also produce ten which will be wrong as per the question
then increment places in num (that is if you encounter 3 in list then increment num[3] by one as count of 3 is now 1) as you traverse the list and then print num if num[i]>0 as there might be integers which have not occured at all.
`int[] list = new int[10];
int[] num = new int[10];
for(int i = 0; i< list.length;i++) {
list[i] = (int)(Math.random()*10);
}
for(int i = 0; i< list.length;i++) {
System.out.print(list[i] + " ");
}
System.out.println();
for (int i = 0; i < list.length; i++) {
++num[list[i]];
}
for (int i = 0; i < num.length; i++) {
if(num[i]>0){
System.out.printf("%d occurs %d times\n",i,num[i]);
}
}`
One way to do this would be to make sure that the numbers were in order (have a look at Arrays.sort), and then loop over the array - if the current number is the same as the last, add to a count, or else start a new count for the new number.
1
333
55555
7777777
999999999
Program to print number pyramid. I want to print this pattern in Java.
My code:
private static void pyramid() {
System.out.println("Please Enter any number less than 10 : ");
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int temp = num;
for (int row = 0; row <= num; row++) {
for (int column = 0; column < temp; column++) {
System.out.print(" ");
}
temp--;
for (int k = 0; k <= row-1; k++) {
if (row % 2 != 0) {
System.out.print(row);
}
System.out.println();
}
}
}
And I am getting following output:
Please Enter any number less than 10 :
9
1
3
3
3
5
5
5
5
5
7
7
7
7
7
7
7
9
9
9
9
9
9
9
9
9
System.out.println(" 1");
System.out.println(" 333");
System.out.println(" 55555");
System.out.println(" 7777777");
System.out.println("999999999");
Your teacher probably wants you to use loops.
So you should note that the bulk standard for loop which programmers can bang out with their eyes closed:
for (int i = 0; i < someNumber; i++) {
//..
}
Is fairly configurable. For instance the i++ at the end means to increment i, but we could increment by bigger amounts (or decrement, or do some other funky things like stepping through a list of objects and so on and so forth).
e.g.
i += 3;
Will increase i by three.
You can also nest loops inside one another, e.g.
for (int i = 1; i < 10; i+=2) {
String s = "";
for (int j = 0; j < i; j++) {
s += i;
}
System.out.println(s);
}
The padding at the front I leave as an exercise to the reader.
Note that this pattern (one loop inside another, and the inner loop being bounded by the outer loops counter) is actually quite common 'out in the wild', and so is worth investing the time to understand.
I want to create a matrix of size N by N where N is a constant value defined globally, for now I just want to create a matrix where N=6. Where I fall short is I want to make it diagonally, like so:
0 1 2 3 4 5
1 0 1 2 3 4
2 1 0 1 2 3
3 2 1 0 1 2
4 3 2 1 0 1
5 4 3 2 1 0
Currently I have this method:
public static void drawMatrix(){
for (int line = 0; line < N; line++){
for (int j = 0; j < N; j++){
System.out.print(j + " ");
}
System.out.println();
}
}
Unfortunately it's only able to print 0 1 2 3 4 5 in every line, so I suppose I need another nested for-loop, however I'm not sure how to set it up.
j is the column number, so it will be the same for all rows. What you need to do is to add or subtract j from the line number, depending on the line number, in order to make a "shift". Since the result could become negative, you will need to add N and mod by N:
if (j > line) {
System.out.print((N-line+j)%N + " ");
} else {
System.out.print((line-j+N)%N + " ");
}
Demo.
You can also rewrite it without an if using a conditional expression:
int sign = j > line ? -1 : 1;
System.out.print((N+sign*(line-j))%N + " ");
Demo.
A little change in your code works
public static void drawMatrix() {
for(int line = 0; line < N; line++) {
for(int j = 0; j < N; j++) {
System.out.print(Math.abs(line - j) + " ");
}
System.out.println();
}
}
I would do something like :
int n=6;
for(int row=0;row<n;row++)
{
for(int col = 0;col<n;col++)
{
System.out.print(abs(col-row) +" ");
}
System.out.println();
}
assuming you can use abs().
I hoped that help your purpose.
This also works :
public static void main(String[] args) {
int N = 6;
int column = 0;
for (int row = 0; row < N; row++) {
for (column = row; column >= 0; column--) //prints till row number reaches 0
System.out.print(column + " ");
for (column = 1; column < N - row; column++)//from 1 to N-row
System.out.print(column + " ");
System.out.println();
}
}
Here is the series :
12345
22345
33345
44445
I tried to solve this but it is not coming correct...
Here is the code :
class q14
{
public static void main ( )
{
int i,j,k;
for (i=1;i<=5;i++)
{
for (j=i;j<=5;j++)
{
for (k=1;k<=i;k++)
{
System.out.print (i + " ");
}
System.out.print (j + " ");
}
System.out.println();
}
}
}
The following block should generate the series as you described it.
int numberOfLines = 4;
int numberOfDigitsPerLine = 5;
for (int i=1; i<numberOfLines+1; i++){
for(int j=1; j<=numberOfDigitsPerLine; j++) {
if(j>=i) {
System.out.print(j);
} else {
System.out.print(i);
}
}
System.out.println();
}
Change numberOfLines and numberOfDigitsPerLine as necessary.
Elaboration:
First you must analyze the series, by the looks of it the first number starts with 1 and goes onward for 5 digits, the second line goes along 5 digits as well up to 5 as previously but it replaces the first digit with 2.
Moving down the numbers we can see a pattern of which the N-th number will have N amount of N digits followed by consecutive digits up to the number 5.
So in my code above I chose max N to be 4 as you described it, and the numbers go up to 5, these are represented by the variables numberOfLines and numberOfDigitsPerLine respectively.
The block itself checks what is N at that point (in my block it is represented by i) and then proceeds to go towards the max number 5, this is done within the j for loop. If j is larger or equal to N then we print j, otherwise we haven't finished printing all of the N's yet so we print N instead.
Here it is:
for (int i = 1; i <= 5; i++)
{
for(int k = 1; k <= i;k++)
System.out.print(i);
for (int j = i + 1; j <= 5; j++)
System.out.print(j);
System.out.print("\n");
}
You dont need a third loop for your series
for (int j=1;j<=5;j++) {
for (int k=1;k<=5;k++){
if(k<=j)
System.out.print (j + " ");
else
System.out.print (k + " ");
}
System.out.println();
}
output
1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
Demo
Try this:
for(int i=1;i<=4;i++)
{
for(int j = 1; j<=5;j++)
{
if(i>j)
{
for(int x= 1 ; x<=i;x++)
{
System.out.print(i);
j++;
}
}
System.out.print(j);
}
System.out.println("\n");
}