I have written a program to print even numbers from a range of numbers(Inputted by the user), what I want the program to do is print 7 even numbers in one row and then next 7 even numbers in the second row and so on, until the specified range is reached.
This is the program I have written, but I want the output as defined above:
import java.util.Scanner;
class EvenNumbersScanner
{
public static void main(String args[])
{
int number,a;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number of range");
a = s.nextInt();
System.out.println("Enter the second numberof range");
number = s.nextInt();
System.out.println("Range is: "+a+" to "+number);
for(int i=a;i<=number;i++)
{
if(i%2==0)
{
System.out.println("Even number is: "+i);
}
}
}
}
Please tell me the code to get 7 even numbers in one row,
You could make use of another variable to keep track of number of even numbers printed.
int count = 0;
for(int i=a;i<=number;i++)
{
if(i%2==0)
{
count = count + 1;
if (count%7==0) {
System.out.println("Even number is: "+i);
} else {
System.out.print("Even number is: "+i);
}
}
}
The output will be the following for a = 1 and number = 40 :
Even number is: 2Even number is: 4Even number is: 6Even number is: 8Even number is: 10Even number is: 12Even number is: 14
Even number is: 16Even number is: 18Even number is: 20Even number is: 22Even number is: 24Even number is: 26Even number is: 28
Even number is: 30Even number is: 32Even number is: 34Even number is: 36Even number is: 38Even number is: 40
If you only want to print the even numbers,
int count = 0;
for(int i=1;i<=40;i++)
{
if(i%2==0)
{
count = count + 1;
if (count%7==0) {
System.out.println(" "+i);
} else {
System.out.print(" "+i);
}
}
}
The output will be the following for a = 1 and number = 40 :
2 4 6 8 10 12 14
16 18 20 22 24 26 28
30 32 34 36 38 40
This works for me:
import java.util.Scanner;
class EvenNumbersScanner
{
public static void main(String args[])
{
int number,a, count;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number of range");
a = s.nextInt();
System.out.println("Enter the second numberof range");
number = s.nextInt();
count = 0;
System.out.println("Range is: "+a+" to "+number);
System.out.println("Even numbers are : ");
for(int i=a;i<=number;i++)
{
if(i%2==0)
{
count++;
System.out.print(i+", ");
if(count >= 7)
{
count = 0;
System.out.println("");
}
}
}
}
}
Produces this output for input of 10
and 100
:
Enter the first number of range
10
Enter the second numberof range
100
Range is: 10 to 100
Even numbers are :
10, 12, 14, 16, 18, 20, 22,
24, 26, 28, 30, 32, 34, 36,
38, 40, 42, 44, 46, 48, 50,
52, 54, 56, 58, 60, 62, 64,
66, 68, 70, 72, 74, 76, 78,
80, 82, 84, 86, 88, 90, 92,
94, 96, 98, 100,
You can put a counter that will check if they are up to 7 and then move content to next line. Try this:
public static void main(String[] args) {
int number, a;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number of range");
a = s.nextInt();
System.out.println("Enter the second numberof range");
number = s.nextInt();
System.out.println("Range is: " + a + " to " + number);
int count = 0;//Changes here
System.out.print("Even number is: ");//Changes here
for (int i = a; i <= number; i++) {
if (i % 2 == 0) {
System.out.print(" "+i);
count++; //Changes here
if(count % 7 == 0){//Changes here
System.out.println();//Changes here
System.out.print("Even number is: ");//Changes here
}//Changes here
}
}
}
Then you should have this as output:
Enter the first number of range
1
Enter the second numberof range
50
Range is: 1 to 50
Even number is: 2 4 6 8 10 12 14
Even number is: 16 18 20 22 24 26 28
Even number is: 30 32 34 36 38 40 42
Even number is: 44 46 48 50
Here is Java 8 IntStream soltuion:
int rangeStart = 1;
int rangeEnd = 50;
AtomicInteger count = new AtomicInteger(0);
IntStream.range(rangeStart, rangeEnd)
.filter(i->i%2==0)
.peek(i->{if(count.getAndIncrement()%7==0)System.out.println("");})
.forEach(i->System.out.print(" " + i));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.