How to solve Java "Nested Loops" problem? [closed] - java

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 1 year ago.
Improve this question
I want help in solving this problem: I want after run the program get this output:
enter image description here
and this is code:
System.out.println("n\tn^2\tn^3\tn^4");
System.out.println("----------------");
for (int i=1; i<5;i++)
{
for (int j= 1; j<4; j++)
{
System.out.print(Math.pow(i,j)+"\t");
}
System.out.println();
}
}
I don’t know what is wrong with me

the Math.pow return double and you need to cast it to int and you need to change the start number one more because the break loop immediately after the for loop condition is false not after the block execute.
System.out.println("n\tn^2\tn^3\tn^4");
System.out.println("----------------");
for (int i=1; i<6;i++)
{
for (int j= 1; j<5; j++)
{
System.out.print(((int)Math.pow(i,j)+"\t"));
}
System.out.println();
}

Related

Pros and Cons of declaring expression in java? "sum += i" instead of "sum = sum + i" [closed]

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 2 years ago.
Improve this question
I got this question in an interview that what's the impacts of declaring expression sum+=i; or sum = sum+i; inside the loop.
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum = sum + i; //Expression 1
sum += i; //Expression 2
}
In this example, there is no difference whatsoever other than what you consider easiest to read.

from Kotlin to Java [closed]

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 2 years ago.
Improve this question
I am working on the problem. Could you please help me out and show how the below piece of code look like in Java.
(0 until n).forEach { i ->
var itis = sc.nextInt() - 1
allData[itis]!!.add(i)
if (allData[itis]!!.size == k) {
(0 until k).forEach { j ->
result[j].addFirst(allData[itis]!!.poll())
}
}
}
Try this:
for (int i=0; i<n; i++){
int itis = sc.nextInt() - 1;
if(allData[itis] != null){
allData[itis].add(i);
if (allData[itis].size() == k) {
for(int j=0; j<k; j++){
result[j].addFirst(allData[itis].poll());
}
}
}
}

Finding longest array in a three-dimensional array and returning a one-dimensional array that contains the largest array [closed]

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 3 years ago.
Improve this question
A method public static int[] largestArray(int[][][]a)
returns a single dimensional array that contains the largest array in the 3D array
If you wish to get the first element which matches your condition, you can try this code.
for(int i = 0; i < arr.length; i++) {
int[][] arrI = arr[i];
for(int j = 0; j < arrI.length; j++) {
int[] arrJ = arr[j];
if(arrJ.length > saved.length) {
saved = arrJ;
}
}
}
Hope that helps.

How to change values's position in 2d array in java? [closed]

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 4 years ago.
Improve this question
i have given these matrix in java
name class
satish BCA
Neeraj MCA
how to rotate this to:
class BCA MCA
name satish neeraj
You can use this function for the same:
public static String[][] transpose(String[][] m){
String[][] temp = new String[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
temp[j][i] = m[i][j];
return temp;
}
Hope it helps.

Count number of times a string is printed [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a for loop that prints a string multiple times.
I need to know:
How do I get the number of times the string is printed?
Thanks!
int count = 0
for (int i = 0 ; i < stop_criteria; i++){
// print string
count++;
}
printf("Number of times printed %d", count);
or as TheLostMind suggest:
int i;
for (i = 0 ; i < stop_criteria; i++){
// print string
}
printf("Number of times printed %d", i);

Categories

Resources