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;
CodePudding user response:
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 == 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.
CodePudding user response:
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 == 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