If statement inside for loops - java

for (int i = 0; i < 150; i++) {
if (i % 13 == 0) {
System.out.println("#: " + i);
}
}
I just started learning java yesterday and I'm stuck with for loops statement.
I'm confused about part
if (i % 13 == 0)
variable i is initialized to zero int i = 0 and if you divide zero by 13 the result is 0. There's no remainder. I tried on calculator.
But when I run the program. I get the result like this it keeps adding by 13 how?
#: 0
#: 13
#: 26
#: 39
#: 52
#: 65
#: 78
#: 91
#: 104
#: 117
#: 130
#: 143

What you're seeing is correct; it does appear to be adding 13 every time because your if statement is effectively saying, in plain english:
Display the value of i whenever 13 divides i evenly (leaving no remainder)
So indeed, each of the numbers you're seeing divides evenly, leaving no remainder:
#: 0 // 0 / 13 = 0, no remainder
#: 13 // 13 / 13 = 1, no remainder
#: 26 // 26 / 13 = 2, no remainder
and so on...

The results you are seeing are all of the numbers between 0 and 150 (which you specified on this line: for (int i = 0; i < 150; i++) {) that are multiples (have no remainder) of 13

The first loop adds 1 to the int i for each iteration. It will then check if there is a remainder when divided by 13:
(i % 13 == 0)
Finally, it will print that number if it doesn't have a remainder when divided by 13:
System.out.println("#: " + i);
So the result is essentially all the numbers that are multiples of 13 between 0 and 150.

Related

How can I print out 1-50 even numbers only, but the it start a new line every multiples of 10. (JAVA)

I'm a newbie, but I'm willing to learn how to code.
I tried using this code:
int n = 50;
int counter = 0;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
counter++;
if (counter == 2) {
System.out.println(i + " ");
counter = 0;
%10== 0
to find all even numbers between 1 to 50 and make a new line at multiples of 10 just follow these steps -
Make one loop which will go 1 to 50
Check if the number is even by checking the remainder after diving it with 2, if YES print that number.
Check if the number is a multiple of 10 by checking the remainder after dividing it by 10, if YES make a new line
The code will look something like this -
int i = 1;
while(i<=50){
if(i%2 == 0) System.out.print(i + " ");
if(i%10 == 0) System.out.println();
i++;
}
Output -
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
It's up to you which looping method you want to use for me While loop looks cleaner.
I hope this solves all your queries.
PFB Snippet:
public class Main
{
public static void main(String[] args) {
for(int i=1;i<=50;i++){
if (i%2 == 0) //Check whether number is even
{
System.out.print(i+" ");
if (i%10 == 0) // Check if it is multiple of 10
{
System.out.print("\n");
}
}
}
}
}
Output:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
"\n" is a Escape Sequence which means new line

I don't understand the solution of this exercise

I am doing an exercise on Practiceit.edu and I have some trouble. The exercise is writing of the following codes:
int total = 25;
for (int number = 1; number <= (total / 2); number++ ) {
total = total - number;
System.out.println(total + " " + number );
}
My output is
24 1
22 2
19 3
15 4
10 5
4 6
-3 7
-11 8
-20 9
-30 10
-41 11
-53 12
because i think that number starts at 1 and finish at 12 (number <= (total / 2)). However, the result is
24 1
22 2
19 3
15 4
10 5
I don't understand this result, so can you help me explain it?
The problem is that you are changing the value of total which will be re-evaluated every-time in your loop
try
int total = 25;
int total2 = total;
for (int number = 1; number <= (total / 2); number++ ) {
total2 = total2 - number;
System.out.println(total2 + " " + number );
}
output
24 1
22 2
19 3
15 4
10 5
4 6
-3 7
-11 8
-20 9
-30 10
-41 11
-53 12
The condition used in the for loop says the number is less than or equal to (half of) total
number <= (total / 2)
The last line where that is true is 10 5
Everything after that doesn’t satisfy the condition.
This is because your total is reducing more and more each time you iterate.
total = total - number;
I.e.:
//1st iteration
25 - 1 = 24; // outputs 24 1
// 2nd iteration
24 - 2 = 22 // outputs 22 2
// 3rd iteration
22 - 3 = 19 // outputs 19 3
// 4th iteration
19 - 4 = 15 // outputs 15 4
// 5th iteration
15 - 5 = 10 // outputs 10 5
Etc.
What are you trying to do?
During the final iteration that is printed, total is 10 and number is 5. Once the for loop continues, number increases by 1 and total decreases to 4. The comparison is made between total = 4 / 2 = 2 and number = 6. This result is not printed since the comparison is already made. Thus, you exit the for loop.

Scanner and while loop addition

I just posted yesterday but I am back with another question, I know the answer to this problem but I'm stuck on how they got it.
import java.util.*;
public class test {
public static void main(String[] args) {
String s = "12 13 14 15 18 16 17 20 2";
Scanner c = new Scanner(s);
int sum = 0;
while(c.nextInt()% 2 == 0)
sum += c.nextInt();
System.out.println(sum);
}
}
The output is 44 but I keep getting 46 on my own. I may be doing it wrong since I am not too familiar on how c.nextInt() works.
I am thinking it goes:
12 % 2 = 0, so add 13
14 % 2 = 0, so add 15
18 % 2 = 0, so add 16
17 % 2 =/= 0, so skip
20 % 2 = 0, so add 2
for a total of = 46
am I missing something?
EDIT: Solved, forgot that loops don't just "skip". I'm dumb
Yes , here answer is 44. Here you are repeating while loop until ( number%2 == 0 ) became FALSE.
in your case :
you are adding 13 + 15 + 16 and the next number to check the while condition is 17
here ( 17%2 == 0 ) is FALSE so condition fails and is not entering into the loop .
here comes the total 44 ( 13 + 15 + 16 )
I hope this will help!
Here is a breakdown going through the loop each time:
c.nextInt() gets 12, 12 % 2 == 0, so this condition is TRUE
sum = 0 and c.nextInt() = 13 so sum += 13 equals 13
Second c.nextInt() gets 14, 14 % 2 == 0, so this condition is TRUE
sum = 13 and c.nextInt() = 15 so sum += 15 equals 28
c.nextInt() gets 18, 18 % 2 == 0, so this condition is TRUE
sum = 0 and c.nextInt() = 16, so sum += 16 equals 44
c.nextInt() gets 17, 17 % 2 == 1, so this condition is FALSE, the loop exits
Print 44
Java while loop is used to execute statement(s) until a condition holds true.If the condition holds false , this while loop will be break out.So
17 % 2 != 0, this while loop will be break out.So sum=44.

Array Printf issue

I'm working on simple program that is supposed to list the numeric values in an array; a certain way. This is how I would like to have the output look like:
Printing Array:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22
It has to be lined as seen above and line must only contain 10 numbers.
I seem to have everything formatted correctly but my output doesn't look like that.
Here is what I am getting:
Printing Array:
1
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22
I'm not sure exactly what I'm doing wrong but here's my code:
//disregard the name 'Juice', I like to give my programs weird names
public class Juice
{
public static void main(String[] args)
{
//sets up the array
int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};
//title
System.out.println("Printing Array: ");
//counting the elements
for (int i = 0; i < numbers.length; i++)
{
//prints each element value with 4 spaces in between
System.out.printf("%4d", numbers[i]);
//once line reaches ten values; print new line
if (i % 10 == 0)
{
System.out.printf("\n");
}
}
}
}
if ((i+1) % 10 == 0)
{
System.out.printf("\n");
}
Your code does what you asked it to.
On first loop, i=0, but i % 10 == 0 is also true, so it prints new line.
You can use many different approaches to fix this, but probably easiest one will be to replace this condition to (i+1) % 10 == 0 or to i % 10 == 9.
you almost did it
public class Juice
{
public static void main(String[] args)
{
//sets up the array
int[] numbers = {1,2,3,4,5,6,7,8,9,10,12,11,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
//title
System.out.println("Printing Array: ");
//counting the elements
for (int i = 0; i < numbers.length; i++)
{
//prints each element value with 4 spaces in between
System.out.printf("%4d", numbers[i]);
//once line reaches ten values; print new line
if (i % 10 == 9)
{
System.out.printf("\n");
}
}
}
}
i've modified a conditions to if (i % 10 == 9)
OUTPUT
Printing Array:
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 26 27 28 29 30
31 32
Alternatively to avoid the confusion between the index to the array element and the element count switch to using a foreach loop.
//counting the elements
int i = 1;
for (int number : numbers) {
//prints each element value with 4 spaces in between
System.out.printf("%4d", number);
//once line reaches ten values; print new line
if (i % 10 == 0) {
System.out.println();
}
i++;
}

How to format an array of integers in java

Currently I have the following method:
public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
System.out.println();
for (int y = 0; y < doubleIntArray[x].length; y++) {
System.out.print(doubleIntArray[x][y]);
}
}
}
It works perfectly when the parameter "doubleIntArray" is only numbers from 0 - 9 like in the following print out:
0000000
0000300
0033332
0023323
0022223
0023233
0003332
However, if the integers in each element of the array become larger than 9 then I get something like the following:
0000000
000121797
001717171716
0101617171617
001616161617
081617161717
001417171716
What I would like to know is how do I make the above example print out like so:
0 0 0 0 0 0 0
0 0 0 12 17 9 7
0 0 17 17 17 17 16
0 10 16 17 17 16 17
0 0 16 16 16 16 17
0 8 16 17 16 17 17
0 0 14 17 17 17 16
You could try using java.text.NumberFormat and a pattern that displays every number with a fixed width.. Then concatenate them all in a single line...
System.out.print(doubleIntArray[x][y] + "\t");
\t prints a tab
but in this case it will print something like this: (but i guess thats okay for you)
0 0 0 0 0 0 0
0 0 0 12 17 9 7
0 0 17 17 17 17 16
0 10 16 17 17 16 17
0 0 16 16 16 16 17
0 8 16 17 16 17 17
0 0 14 17 17 17 16
Or use String.format("%4d", myinteger) to have each integer occupy 4 chars, and be properly right padded.
System.out.printf("%4d", doubleIntArray[x][y]);
4 represents the minimum space that the number will print.
The other options for this method are explained here
You have 2 options. Firstly, you can use \t in the second for loop. But I think you could add \t and whitespace character to avoid deterioration. Can provide this too, adding if-else structure in second for loop. I mean
if(doubleIntArray[y].length<10){
System.out.print(doubleIntArray[x][y] + "\t ");
//Tab+two whitespace.
} else {
if(doubleIntArray[y].length>10) {
System.out.print(doubleIntArray[x][y] + "\t ");
//Tab+one whitespace
} else {
System.out.print(doubleIntArray[x][y] + "\t");
//Tab+NO whitespace
}
}
Logic is that I think. Sorry for my answers design. I am on the bus now and I cannot write smoothly. If I had a mistake sorry about that again.
public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
System.out.println();
for (int y = 0; y < doubleIntArray[x].length; y++) {
System.out.print(doubleIntArray[x][y],"\t");
}
System.out.print("\n");
}
}

Categories