0

I'm trying to create my fist java application with an if statement that will take an integer (e.x 22) and find out if its sum is equal when multiplied and subtract (e.x 2*2=4 and 2+2=4) or if its not.

Though i can't figure out how to do the if decision. can someone point out how to do that?

thank u

package 1;

import java.util.Scanner;

public class 1 
{

    public static void main(String[] args) 
    {
      Scanner input = new Scanner( System.in );

    int x;
    int y;   
    System.out.print( "Enter a number from 10 to 99: " ); 
    x = input.nextInt();

    if ( x >= 10 && x <= 99 ) 
    {
            x= x % 10;
            x= x / 10;

    }
    else
    {
        System.out.println( "you must enter a number from 10 to 99" );
    }


   } 

}
1
  • 3
    Start from identifiers and variables topic in any Java tutorial. Commented Oct 19, 2013 at 16:48

2 Answers 2

1

You just need to assign them to different variable and check for the condition

if (x >= 10 && x <= 99) {
    int first = x / 10; // Take out the first digit and assign it to a variable first
    int second = x % 10; // Take out the second digit and assign it to a variable second

    if (first * second == first + second) { // Check for your condition, which you mentioned in your question
        System.out.println("Yep, they match the condition"); // If it satisfies the condition
    } else {
        System.out.println("Nope, they don't match the condition"); // If it doesn't satisfy the condition
    }

}

P.S: You question said multiplied and subtract but the example just after the example was (e.x 2 x 2=4 and 2+2=4). I went with the ex.

Sign up to request clarification or add additional context in comments.

Comments

0

try

import java.util.Scanner;
public class One {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int x;
        int y;
        System.out.print("Enter a number from 10 to 99: ");
        x = input.nextInt();        
        if (x >= 10 && x <= 99) {
            y = x % 10;
            x = x/10 ;
        if(x* y== x+ y)){
            System.out.println("Sum and product are equal" );
        }
        else
            System.out.println("Sum and product are not equal" );

        } else {
            System.out.println("you must enter a number from 10 to 99");
        }
      input.close();
    }
}

2 Comments

if(x==y){ this condition is totally wrong. Read the question again.
@ R.J yes , yours is correct Answer +1 , Thanks for pointing it out, have updated the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.