Showing posts with label java. Show all posts
Showing posts with label java. 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");}

}

}

Saturday, February 2, 2013

Java Programming :How to add ten numbers using while loop and arrays.

Java Programming :How to add ten numbers using while loop and arrays.

Step 1:
declare and initialize 3 variables. integer i as counter and integer sum.

Step 2:
declare and initialize array integer type, size of the array is ten.

Step 3: while loop to generate the numbers and add the numbers.

Step 4:
Print out the result.

Full code below :


// How to add 10 numbers using while

public class AddTenNumbers{
public static void main(String[] args){
int i = 0 ; // as counter
int sum = 0 ; // sum up the total
int number[] = new int[10]; // use array to generate ten numbers

// use while to generate the numbers and sum up the numbers

while( i < 10){

// Generate the 10 numbers
number[i] = i + 1 ;

// SUm the numbers
sum = sum + number[i];

i++; // forget this

}

// Print the sum

System.out.println("SUmmation of 1 till 10 is : " + sum);

}
}

How to read input using Java Scanner Class

This tutorial is how you can read inputs for command prompt. You will be using Scanner class to read the input from keyboard.

You import Scanner class using > import java.util.Scanner ; statement

You create scanner object using the statement ;
Scanner myscan = new Scanner(System.in);

myscan can be any name.

To read integer number , you use the following statement.

x = myscan.nextInt();

The full code the program is stated below;


// How to read input from console

import java.util.Scanner;

public class ScannerInput2 {
public static void main(String[] args){
int x ;
Scanner myscan = new Scanner(System.in);

// Ask input
System.out.print("Enter an integer number :");

// Read from console
x = myscan.nextInt();

//Print out the number
System.out.println("Your integer number is :" + x );


}
}