Can someone please help. I have to write a program to find the numbers for A, B,
ID: 3544197 • Letter: C
Question
Can someone please help.
I have to write a program to find the numbers for A, B, & C. So far I've gotten my A & B; but I need help completing my C so that my program prints out the odd numbers first, then the even ones, then all of the numbers together from 1-20.
Okay A=odd numbers 1-19, B=even numbers 2-20, and C= both odd and even such as 1,2,3,4.....20.
Here's what I have so far.
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.text.*;
import java.lang.Math.*;
import java.text.DecimalFormat;
import java.lang.String;
public class array1
{
static Scanner scan = new Scanner(System.in);
static DecimalFormat twoDec = new DecimalFormat ("0.00");
public static void main(String[] args)
{
int [] A=new int[10];
int i;
int [] B=new int[10];
int p;
int [] C=new int[20];
int r;
for (i=0;i<10;i++)
A[i]=i+i+1;
for (p=0;p<10;p++)
B[p]=2*p+2;
for (i=0;i<10;i++)
System.out.println(A[i]);
for (p=0;p<10;p++)
System.out.println(B[p]);
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
public class evenodd{
public static void main(String[] args) {
List<Integer> C = new ArrayList<Integer>();
for (int i = 1; i <= 20; i++){
C.add(i);
}
List<Integer> A = new ArrayList<Integer>();
List<Integer> B = new ArrayList<Integer>();
for (int i : C) {
if ((i & 1) == 1) {
A.add(i);
} else {
B.add(i);
}
}
System.out.println("A:" + A);
System.out.println("B:" + B);
System.out.println("C:" + C);
}
}
MODIFYING ur PROG
import java.util.ArrayList;
import java.util.List;
public class evenodd{
public static void main(String[] args) {
int [] A=new int[10];
int i;
int [] B=new int[10];
int p;
int [] C=new int[20];
int k;
int r;
for (i=0;i<10;i++)
A[i]=i+i+1;
for (p=0;p<10;p++)
B[p]=2*p+2;
for(k=0;k<20;k++)
C[k]=k;
for (i=0;i<10;i++)
System.out.println(A[i]);
for (p=0;p<10;p++)
System.out.println(B[p]);
for(k=0;k<20;k++)
System.out.println(C[k]);
}}