Sunday, August 23, 2015

Java program to find odd or even

This java program finds if a number is odd or even. If the number is divisible by 2 then it will be even, otherwise it is odd. We use modulus operator to find remainder in our program.

Java programming source code


import java.util.Scanner;
 
class OddOrEven
{
 public static void main(String args[])
 {
 int x;
 System.out.println("Enter an integer to check if it is odd or even ");
 Scanner in = new Scanner(System.in);
 x = in.nextInt();
 
 if ( x % 2 == 0 )
 System.out.println("You entered an even number.");
 else
 System.out.println("You entered an odd number.");
 }
}

Output of program:


Another method to check odd or even

import java.util.Scanner;
 
class EvenOdd
{
 public static void main(String args[])
 {
 int c;
 System.out.println("Input an integer");
 Scanner in = new Scanner(System.in);
 c = in.nextInt();
 
 if ( (c/2)*2 == c )
 System.out.println("Even");
 else
 System.out.println("Odd");
 }
}

There are other methods for checking odd/even one such method is using bitwise operator.

import java.util.Scanner;
public class EvenOrOdd { 
public static void main(String args[]) { 
Scanner obj = new Scanner(System.in);
int no;
System.out.println("Enter any number to check if it is Even or Odd number:");
no = obj.nextInt();
isOddOrEven(no); 
}
public static void isOddOrEven(int number){ 
if((number & 1) == 0){ 
System.out.println("Using bitwise operator: " + number + " is Even number"); 
}
else{ 
System.out.println("Using bitwise operator: " + number + " is Odd number"); 
} 
} 
}

Output of program:

Java program to add two numbers

Given below is the code of java program that adds two numbers which are entered by the user.

Java programming source code


import java.util.Scanner;
 
class AddNumbers
{
 public static void main(String args[])
 {
 int x, y, z;
 System.out.println("Enter two integers to calculate their sum ");
 Scanner in = new Scanner(System.in);
 x = in.nextInt();
 y = in.nextInt();
 z = x + y;
 System.out.println("Sum of entered integers = "+z);
 }
}

Output of program:


Above code can add only numbers in range of integers(4 bytes), if you wish to add very large numbers then you can use BigInteger class. Code to add very large numbers:

import java.util.Scanner;
import java.math.BigInteger;
 
class AddingLargeNumbers {
 public static void main(String[] args) {
 String number1, number2;
 Scanner in = new Scanner(System.in);
 
 System.out.println("Enter first large number");
 number1 = in.nextLine();
 
 System.out.println("Enter second large number");
 number2 = in.nextLine();
 
 BigInteger first = new BigInteger(number1);
 BigInteger second = new BigInteger(number2);
 BigInteger sum;
 
 sum = first.add(second);
 
 System.out.println("Result of addition = " + sum);
 }
}

In our code we create two objects of BigInteger class in java.math package. Input should be digit strings otherwise an exception will be raised, also you cannot simply use '+' operator to add objects of BigInteger class, you have to use add method for addition of two objects.

Output of program:
Enter first large number
11111111111111
Enter second large number
 99999999999999
Result of addition = 111111111111110

Saturday, August 22, 2015

How to get input from user in java

This program tells you how to get input from user in a java program. We are using Scanner class to get input from user. This program firstly asks the user to enter a string and then the string is printed, then an integer and entered integer is also printed and finally a float and it is also printed on the screen. Scanner class is present in java.util package so we import this package in our program. We first create an object of Scanner class and then we use the methods of Scanner class. Consider the statement:

Scanner a = new Scanner(System.in);

Here Scanner is the class name, a is the name of object, new keyword is used to allocate the memory and System.in is the input stream. Following methods of Scanner class are used in the program below:-
1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string
Java programming source code

import java.util.Scanner;
 
class GetInputFromUser
{
 public static void main(String args[])
 {
 int a;
 float b;
 String s;
 
 Scanner in = new Scanner(System.in);
 
 System.out.println("Enter a string");
 s = in.nextLine();
 System.out.println("You entered string "+s);
 
 System.out.println("Enter an integer");
 a = in.nextInt();
 System.out.println("You entered integer "+a);
 
 System.out.println("Enter a float");
 b = in.nextFloat();
 System.out.println("You entered float "+b); 
 }
}

Output of program:


There are other classes which can be used for getting input from user and you can also take input from other devices.

Java program to print multiplication table


This java program prints multiplication table of a number entered by the user using a for loop. You can modify it for while or do while loop for practice.

Java programming source code


import java.util.Scanner;
 
class MultiplicationTable
{
 public static void main(String args[])
 {
 int n, c;
 System.out.println("Enter an integer to print it's multiplication table");
 Scanner in = new Scanner(System.in);
 n = in.nextInt();
 System.out.println("Multiplication table of "+n+" is :-");
 
 for ( c = 1 ; c <= 10 ; c++ )
 System.out.println(n+"*"+c+" = "+(n*c));
 }
}

Output of program:



Using nested loops we can print tables of number between a given range say a to b, For example if the input numbers are 3 and 6 then tables of 3, 4, 5 and 6 will be printed. Code:

import java.util.Scanner;
 
class Tables
{
 public static void main(String args[])
 {
 int a, b, c, d;
 
 System.out.println("Enter range of numbers to print their multiplication table");
 Scanner in = new Scanner(System.in);
 
 a = in.nextInt();
 b = in.nextInt();
 
 for (c = a; c <= b; c++) {
 System.out.println("Multiplication table of "+c);
 
 for (d = 1; d <= 10; d++) {
 System.out.println(c+"*"+d+" = "+(c*d));
 }
 }
 }
}

Friday, August 21, 2015

Java program to print alphabets


This program print alphabets on screen i.e a, b, c, ..., z. Here we print alphabets in lower case.

Java source code


class Alphabets
{
 public static void main(String args[])
 {
 char ch;
 
 for( ch = 'a' ; ch <= 'z' ; ch++ )
 System.out.println(ch);
 }
}

You can easily modify the above java program to print alphabets in upper case.

Output of program:


Printing alphabets using while loop (only body of main method is shown):
char c = 'a';
 
while (c <= 'z') {
 System.out.println(c);
 c++;
}

Using do while loop:
char c = 'A';
 
do {
 System.out.println(c);
 c++;
} while (c <= 'Z');