Showing posts with label if else. Show all posts
Showing posts with label if else. Show all posts

Monday, February 11, 2013

Tutorial 10 : If and Else, How to determine the number is even or odd

Java Tutorial For Beginners, Tutorial 9 : If and Else, How to determine the number is even or odd?

Program uses if and else condition. If the number when divided by 2, the remainder is 0 then is an even number. Else the number is an odd number.

Step 1 : Read the number. Use Scanner class to read the input.

Step 2 : Use if and else condition. If number % 2 == 0, then the number is an even number. Else it is an odd number.

Step 3 : Print out the statement.

Code below :


//Program to determine whether a number is even or odd
// Use if else condition
// It is even if the number remainder is 0 when divided by 2
// If the remainder is 1, then the number is odd

//use scanner class for read input

import java.util.Scanner;

public class TestIfTwo{
public static void main(String[] args){
int number = 0;

Scanner myscan = new Scanner(System.in);

//Read input
System.out.print("Enter a number : ");
number = myscan.nextInt();

//if and else condition
if (number % 2 == 0) {System.out.println(number + " is even");}
else {System.out.println(number + " is odd");}

}

}

Java Tutorial For Beginners, Tutorial 9 : If and Else

Java tutorial for beginners, tutorial 9 : If and Else. This program calculates the retail profit, if the value of retail price is more than 250 than the profit is 30 %, else the profit is 40 %.

Step 1 : Read the retail price

Step 2 : If else condition, calculate the profit rate. If it is more than 250, then profit is 30 % of the retail price. Else the profit is 40 % of the retail price.

Step 3 : Print out the profit .

Code below :


// Tutorial on if condition
// For example, if your retail price is more than 250, the profit is 30 %
// Else, the profit rate is fix at 40 %

//use scanner class to input retail price
import java.util.Scanner ; // forgot to add this line

public class TestIf {
public static void main(String[] args){

//declare and initialize the variables;
int retailPrice = 0;
int profit = 0;

Scanner myscan = new Scanner(System.in);

System.out.print("Enter your retail price : ");
retailPrice = myscan.nextInt();


//Your condition at here
if (retailPrice > 250) {
profit = retailPrice * 30 / 100 ; // more than 250 the profit rate is only 30 %
}
else {
profit = retailPrice * 40 / 100 ; // more than 250 the profit rate is only 30 %
}

//Print out the output

System.out.println("Your profit is : " + profit);

}
}