java get even or odd number - java

I have a assignment for school: I need to get even or odd numbers and I made something to work for numbers that are above zero, but I need to find out how a negative number is even or odd.
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
int x = i;
while (x > 1) x = x - 2;
System.out.println(i + evenodd[x]);
}

simply use module % operator
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
int x;
if(i%2==0){
x=0;/*pass 0 to print even number */
}else{
x=1;/*pass 1 to print odd number*/
}
System.out.println(i + evenodd[x]);
}

Even and Odd numbers:
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
System.out.println(i + evenodd[i % 2]);
}
OR
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i < 100; i++)
{
if(i % 2 == 0)
System.out.println(i + evenodd[i % 2]);
else
System.out.println(i + evenodd[i % 2]);
}
For negative numbers is even or odd?:
String[] evenodd = {" number is even", " number is odd"};
for(int i = 0; i > -100; i--)
{
if(i % 2 == 0)
System.out.println(i + evenodd[0]);
else
System.out.println(i + evenodd[1]);
}

Try and use the % operator.
Usage : x % y -> This will give you the remainder when y is divided by x.
In your case use y = 2. Hence if the remainder = 0, the number is even else the number will be odd.
I am not giving you the exact code, since this is your assignment. Try it yourself based on the suggestion.

To find out odd and even number. Follow the logic
divide your number by 2 if the reminder is 0 then it is even
else it is odd
this similar for both positive and negative numbers
to find out reminder use modulo division operator (%). if your_number%2=0 your_number is even else your_number is odd.
Sample code:
for(int i=1;i<100;i++){
if(i%2==0){//for positive number
System.out.println(i+ " is Even Number");
}else{
System.out.println(i+ " is Odd Number");
}
if((-1)*i%2==0){//for negative number
System.out.println((-1)*i+" is Even Number");
}else{
System.out.println((-1)*i+" is Odd Number");
}
}

Related

Printing with delimiter only between values

I have a slight problem with the output of my code and been searching for such topics same as with this but I don't find any.
while (true) {
System.out.print("Enter a positive integer: ");
n = sc.nextInt();
System.out.print(n + "! = ");
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
System.out.printf("%d x " , i);
}
System.out.println("");
}
The output must be. Whenever I type integer. e.g 5.
Enter a positive integer: 5
5! = 1 x 2 x 3 x 4 x 5
But the slight problem is that the output goes like this 5! = 1 x 2 x 3 x 4 x 5 x
There's extra x on the last number which should not be there
StringJoiner
Others already answered how to fix your code but I would like to provide you with a more professional solution, namely by using a StringJoiner.
With this, you can give a delimiter, prefix and suffix, then just add all your elements and the StringJoiner will make sure that the delimiter is only added in between. It takes all the work from you. Here is the code:
StringJoiner sj = new StringJoiner("x ", n + "! = ", "");
for (int i = 1; i <= n; i++) {
sj.add(Integer.toString(i));
}
System.out.println(sj);
Streams
If you prefer streams:
String result = IntStream.rangeClosed(1, n)
.mapToObj(Integer::toString)
.collect(Collectors.joining("x ", n + "! = ", ""));
System.out.println(result);
Replace this line:
System.out.printf("%d x " ,i);
by this:
if (i == n)
System.out.printf("%d" ,i);
else
System.out.printf("%d x " ,i);
This way you avoid printing "x" when you print the last number in the factorization.
Perhaps you can do this:
while (true) {
System.out.print("Enter a positive integer: ");
n = sc.nextInt();
String result = "";
System.out.print(n + "! = ");
for (int i = 1; i <= n; i++) {
factorial = factorial * i;
result += i + " x ";
}
System.out.println(result.substring(0, result.length() - 3);
}

What kind of loop bug does this code have and how to fix it?

Just wondering where exactly is the bug? Is the for statement formatted incorrectly?
int n;
sum = 0;
for (n = 1; n < 10; n++) {
sum = sum + n;
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You need to close your for loop with a } and declare the sum variable as an int. So the code should look like:
int n;
int sum = 0;
for (n = 1; n < 10; n++){
sum = sum + n;
}
System.out.println("1 + 2 + ...+ 9 + 10 == " + sum);
You should research using an IDE like Eclipse. Its a text editor for your code that will point out where the compilation errors are.

JOptionPane Looping

I have an issue. My lecturer wants me to make a loop, with an input of JOptionPane and an output of console. How can I use loop for JOptionPane and send an output through console.
Here's my code:
int even = 0;
int odd = 0;
int e_e = 0;
int o_o = 0;
String a1 = JOptionPane.showInputDialog(null, "Type in 10 integer");
for (int counter = 0; counter < 10; counter++){
int a = Integer.parseInt(a1);
if (a % 2 == 0) {
even++;
e_e += a;
} else {
odd++;
o_o += a;
}
}
System.out.println("\n\nNumber of even numbers : " + even);
System.out.println("Number of odd numbers : " + odd);
System.out.println("Total of even numbers : " + e_e);
System.out.println("Total of odd numbers : " + o_o);
I would try using a DO-WHILE loop with and an int[], example:
int size = 10;
int count = 0;
int[] yourNumbers = new int[size];
do {
yourNumbers[count] = Integer.parseInt(JOptionPane.showInputDialog(null,
"Your message here."));
count++;
} while (count < 10);
This way you can loop through and grab all the numbers. Then you can use a FOR-LOOP to cycle through and print what you need
System.out.println("Even Numbers are: ");
for(int i = 0; i < yourNumbers.length; i++) {
if (yourNumbers[i] % 2 == 0) {
System.out.println(yourNumbers[i]);
}
}
System.out.println("Odd Numbers are: ");
for(int i = 0; i < yourNumbers.length; i++) {
if (yourNumbers[i] % 2 != 0) {
System.out.println(yourNumbers[i]);
}
}
The problem with your current code is that you only ask the user one time to input a number but you actually seem to want 10 values. So you parse ten times the same value.
The solution is simple, put the dialog inside the loop (only changed the lines with comments):
int even = 0;
int odd = 0;
int e_e = 0;
int o_o = 0;
// No return type, just a message
JOptionPane.showMessageDialog(null, "Type in 10 integer");
for (int counter = 0; counter < 10; counter++) {
// Dialog inside the loop, asking to
// input a number in every iteration
String value = JOptionPane.showInputDialog(null, "Type in "
+ (counter + 1) + ". value");
int a = Integer.parseInt(value);
if (a % 2 == 0) {
even++;
e_e += a;
} else {
odd++;
o_o += a;
}
}
System.out.println("\n\nNumber of even numbers : " + even);
System.out.println("Number of odd numbers : " + odd);
System.out.println("Total of even numbers : " + e_e);
System.out.println("Total of odd numbers : " + o_o);

For loop printing an unexpected number of times

public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[5];
System.out.print("Please enter five numbers. \na=");
array[0] = input.nextInt();
System.out.print("\nb=");
array[1] = input.nextInt();
System.out.print("\nc=");
array[2] = input.nextInt();
System.out.print("\nd=");
array[3] = input.nextInt();
System.out.print("\ne=");
array[4] = input.nextInt();
boolean totalIsZero = false;
for (int i=0;i<array.length ;i++) {
for (int j=1;i>j ;j++ ) {
if ((array[i] + array[j])==0) {
System.out.println("The numbers " + array[i] + " and " + array[j] + " have a total sum equal to 0.");
totalIsZero = true;
}
}
}
if (!totalIsZero) {
System.out.print("None of the numbers have a total sum of 0 with each other. ");
}
}
Here is some simple code I just wrote. Its task is to check if the sum between every two numbers in an array (consisting of five numbers) is equal to zero.
The problem I have is that when there are two pairs of numbers, both equal to 0, at the end of the program there is a message for one of the pairs only, not for both, as I expected.
How can I fix that, so the user can read that there are two pairs of numbers equal to 0?
Not sure if this will work perfectly because I haven't tested it and I haven't used java in a while, but just create the array the same way you do it in your post, but try the rest for the actual bulk of the function.
// various input calls above^ to create array
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = i + 1; j < array.length; j++)
{
if(array[i] + array[j] == 0)
{
System.out.println("The numbers " + array[i] + " and " +
array[j] +
" have a sum equal to zero.");
count++;
}
}
}
if(count == 0)
{
System.out.println("No sum between any numbers is equal to 0");
}

How to compare the given integer with possible roots

I could not name the question in the best way... My aim is to write a program that takes integer n from user. Then compare with the third power of two integers a and b.
If a^3 + b^3 is smaller than or equal to the given input, I want to print out every single possible calculation to the user.
My code is as follows:
System.out.println("Hi. Please insert an integer: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
double root = Math.cbrt(n);
int rootInt = (int) root;
for (int i = 0; i < rootInt; i++){
for (int j = 0; j < rootInt; j++){
if ((Math.pow(i, 3)) + (Math.pow(j, 3)) <= n){
double t = (Math.pow(i, 3) + (Math.pow(j, 3)));
int totalInt = (int) t;
System.out.println(i + "^3" + " + " + j + "^3" + " = " + totalInt );
} else {
}
j++;
}
i++;
}
When I run this and give an input as 30, it prints
0^3 + 0^3 = 0
0^3 + 2^3 = 8
2^3 + 0^3 = 8
2^3 + 2^3 = 16
What am I doing wrong?
You increase j and i twice. That's why you test only the even values for i, j.
If you want to correct the code, remove the i++, j++ from the end and use only the increments from the 2 fors.

Categories