Wednesday, August 30, 2017

Java example for generating a table of a number and for loop

This example is for Java beginners to let them learn two basic concepts of the language
1. for loop
2. Using Scanner class to get input from keyboard

 
import java.util.Scanner;

public class TableOfInteger {

     // start of main() function

        public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        // get an object of Scanner class for getting input from keyboard

        System.out.println("enter an integer for generate table ");

        int table = keyboard.nextInt();

        // for loop

        for(int i=1;i<=10;i++)

                                System.out.println(table + "X" + i +"=" +table*i);

}

//end of main function()

}

Monday, August 28, 2017

If-else statement, read input from keyboard (by java.util.Scanner) and Find if a number is Even Or Odd example in Java

this example explains three important topics of Java
1. read input from keyboard
2. if-else statement
3. calculate if the number is even or odd

import java.util.Scanner;

public class FindEvenOrOddNumber {

        public static void main(String[] args) {

        // main() function

        Scanner keyboard = new Scanner(System.in);

        // Scanner object to read input from keyboard

        System.out.println("enter an integer");

        int even_odd = keyboard.nextInt();

         if(even_odd%2 == 0) //check if number is odd or even

                                System.out.println(even_odd + " is even number.");

                         else

                                System.out.println(even_odd + " is odd number.");

}

}

Sunday, August 27, 2017

Hello world example in Java

This Example is for the beginners to start programming in Java


public class Hello{

//main() function start

  public static void main(String args[]){

    System.out.println("Hello World !");

//command to print on the console 

  }

// main function finish

}