Hi everyone, first problem, is I need an explaination....see we did this for loo
ID: 3625489 • Letter: H
Question
Hi everyone,
first problem, is I need an explaination....see we did this for loop in class and I know it prints out 4,8,4,5,10 and 5...my question is why? I get lost in the loop and I can't see how it prints that out...please help!! this is the code for the loop: (show values for i, j and n)
int n;
for (int i = 1; i <= 2; i++) {
n = i + 3;
for (int j= n; j <= 10; j+=n) {
System.out.println(j);
}
System.out.println(n);
}
________________________________________________________________________________________________
Other problem....I need help with these java programs...
1)Enter non-zero integers (positive or negative). A zero entered means there is no more data. Find
the average of only the even numbers. Print whether or not a negative number is encountered.
2)
Given a constant N, display a box of N asterisks. For example, if N is 5, you display- there isn't suppose to be spaces in between the stars above and below.
*****
* *
* *
* *
*****
3)Given a constant M, display a checkerboard box of M asterisks. For example, if M is 8, you display - again no spaces between each other...(above and below)
********
********
********
********
********
********
********
********
I really hope you can help! I am having such trouble with for loops --- I would so grateful if you could explain the for loop for each...THANKS!!
Explanation / Answer
please rate - thanks
message me if there is anything you don't understand
int n;
for (int i = 1; i <= 2; i++) { i=1
n = i + 3; n=4
for (int j= n; j <= 10; j+=n) { j=4 j<= 10 yes
System.out.println(j); output 4
} add 4 to j--j=8 <= 10 yes
System.out.println(n); output 8
} add 8 to j, j=12 <= 10 no
exit loop output n (4)
i=2
n=5
j=5 j<10 yes
output 5
add 5 to j j=10 <=10 yes
output 10
add 5 to j, j=15 <=10 no
exit loop output n (5)
output
4
8
4
5
10
5
-------------------------------------------------
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n,sum=0,count=0;
System.out.print("Enter a number: ");
n=in.nextInt();
while(n!=0)
{if(n%2==0)
{count++;
sum+=n;
}
if(n<0)
System.out.println(n+" negative");
System.out.print("Enter a number: ");
n=in.nextInt();
}
System.out.println("Sum of the even numbers is: "+sum);
}
}
---------------------------------------------------
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n,i,j;
System.out.print("Enter a size: ");
n=in.nextInt();
for(i=0;i<n;i++)
System.out.print("*");
System.out.println();
for(i=0;i<n-2;i++)
{System.out.print("*");
for(j=0;j<n-2;j++)
System.out.print(" ");
System.out.print("*");
System.out.println();
}
for(i=0;i<n;i++)
System.out.print("*");
System.out.println();
}
}
---------------------------------------------
import java.util.*;
public class main
{
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
int n,i,j;
System.out.print("Enter a size: ");
n=in.nextInt();
for(j=1;j<=n;j++)
{if(j%2==0)
System.out.print(" ");
for(i=0;i<n;i++)
System.out.print("*");
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.