Monday, September 14, 2015

Java program print prime numbers


This java program prints prime numbers, number of prime numbers required is asked from the user. Remember that smallest prime number is 2.

Java programming code


import java.util.*;
 
class PrimeNumbers
{
 public static void main(String args[])
 {
 int n, status = 1, num = 3;
 
 Scanner in = new Scanner(System.in);
 System.out.println("Enter the number of prime numbers you want");
 n = in.nextInt();
 
 if (n >= 1)
 {
 System.out.println("First "+n+" prime numbers are :-");
 System.out.println(2);
 }
 
 for ( int count = 2 ; count <=n ; )
 {
 for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
 {
 if ( num%j == 0 )
 {
 status = 0;
 break;
 }
 }
 if ( status != 0 )
 {
 System.out.println(num);
 count++;
 }
 status = 1;
 num++;
 } 
 }
}

Output of program:


We have used sqrt method of Math package which find square root of a number. To check if an integer(say n) is prime you can check if it is divisible by any integer from 2 to (n-1) or check from 2 to sqrt(n), first one is less efficient and will take more time.

Thursday, September 10, 2015

Java program to find factorial

This java program finds factorial of a number. Entered number is checked first if its negative then an error message is printed.

Java programming code

import java.util.Scanner;
class Factorial { 
public static void main(String args[]) { 
int n, c, fact = 1;   
System.out.println("Enter an integer to calculate it's factorial"); 
Scanner in = new Scanner(System.in);   
n = in.nextInt();   
if ( n < 0 ) 
System.out.println("Number should be non-negative."); 
else { for ( c = 1 ; c <= n ; c++ ) 
fact = fact*c;   
System.out.println("Factorial of "+n+" is = "+fact); 
} 
} 
}
Output of program:


You can also find factorial using recursion, in the code fact is an integer variable so only factorial of small numbers will be correctly displayed or which fits in 4 bytes. For large numbers you can use long data type.

Java program for calculating factorial of large numbers


Above program does not give correct result for calculating factorial of say 20. Because 20! is a large number and cant be stored in integer data type which is of 4 bytes. To calculate factorial of say hundred we use BigInteger class of java.math package.

import java.util.Scanner; 
import java.math.BigInteger; 
class BigFactorial { 
public static void main(String args[]) { 
int n, c; 
BigInteger inc = new BigInteger("1"); 
BigInteger fact = new BigInteger("1"); 
Scanner input = new Scanner(System.in); 
System.out.println("Input an integer"); 
n = input.nextInt(); 
for (c = 1; c <= n; c++) { 
fact = fact.multiply(inc); 
inc = inc.add(BigInteger.ONE); 
} 
System.out.println(n + "! = " + fact); 
} 
}
We run the above java program to calculate 100 factorial and following output is obtained.
Input an integer
100
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Enhanced for loop java

Enhanced for loop java: Enhanced for loop is useful when scanning the array instead of using for loop. Syntax of enhanced for loop is:

for (data_type variable: array_name)

Here array_name is the name of array.

Java enhanced for loop integer array

class EnhancedForLoop { 
public static void main(String[] args) { 
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 
for (int t: primes) { 
System.out.println(t); 
} 
} 
}
Output of program:

Java enhanced for loop strings

class EnhancedForLoop { 
public static void main(String[] args) { 
String languages[] = { "C", "C++", "Java", "Python", "Ruby"}; 
for (String sample: languages) { 
System.out.println(sample); 
} 
} 
}

Wednesday, September 9, 2015

Java program to find largest of three numbers

This java program finds largest of three numbers and then prints it. If the entered numbers are unequal then "numbers are not distinct" is printed.

Java programming source code

import java.util.Scanner;
class LargestOfThreeNumbers { 
public static void main(String args[]) { 
int x, y, z; 
System.out.println("Enter three integers "); 
Scanner in = new Scanner(System.in);   
x = in.nextInt(); 
y = in.nextInt(); 
z = in.nextInt();   
if ( x > y && x > z ) 
System.out.println("First number is largest."); 
else if ( y > x && y > z ) 
System.out.println("Second number is largest."); 
else if ( z > x && z > y ) 
System.out.println("Third number is largest."); 
else System.out.println("Entered numbers are not distinct."); 
} 
}

Output of program:


If you want to find out largest of a list of numbers say 10 integers then using above approach is not easy, instead you can use array data structure.

Tuesday, September 8, 2015

Java program to swap two numbers

This java program swaps two numbers using a temporary variable. To swap numbers without using extra variable see another code below.

Swapping using temporary or third variable


import java.util.Scanner;   
class SwapNumbers { 
public static void main(String args[]) { 
int x, y, temp; 
System.out.println("Enter x and y"); 
Scanner in = new Scanner(System.in);   
x = in.nextInt(); 
y = in.nextInt();   
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);   
temp = x; 
x = y; 
y = temp;   
System.out.println("After Swapping\nx = "+x+"\ny = "+y); 
} 
}
Output of program:


Swapping without temporary variable

import java.util.Scanner;   
class SwapNumbers { 
public static void main(String args[]) { 
int x, y; 
System.out.println("Enter x and y"); 
Scanner in = new Scanner(System.in);   
x = in.nextInt(); 
y = in.nextInt();   
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);   
x = x + y; 
y = x - y; 
x = x - y;   
System.out.println("After Swapping\nx = "+x+"\ny = "+y); 
} 
}
Swapping is frequently used in sorting techniques such as bubble sort, quick sort etc.