Wednesday, 23 December 2015

Java Program For Find a Prime Number in different approches


 1st approach:

/*
 *
 * To Find the Prime Numbers 1 to 100 Numbers
 */

package com.onlinetutorialspoint.javaprograms;

public class PrimeNumberDemo1 {

    public static void main(String[] args) {
        int i = 0;
        int num = 0;
       
        String primeNumbers = "";

        for (i = 1; i <= 100; i++) {
            int counter = 0;
            for (num = i; num >= 1; num--) {
                if (i % num == 0) {
                    counter = counter + 1;
                }
            }
            if (counter == 2) {
               
                primeNumbers = primeNumbers + i + " ";
            }
        }
        System.out.println("Prime numbers from 1 to 100 are :");
        System.out.println(primeNumbers);
    }

}

output:
Prime numbers from 1 to 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97


2nd approach:



/*
 * To find the PrimeNumber by using Scanner Class
 */


package com.onlinetutorialspoint.javaprograms;

import java.util.Scanner;

public class PrimeNumberDemo2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        int num = 0;
        // Empty String
        String primeNumbers = "";
        System.out.println("Enter the value of n:");
        int n = scanner.nextInt();
        for (i = 1; i <= n; i++) {
            int counter = 0;
            for (num = i; num >= 1; num--) {
                if (i % num == 0) {
                    counter = counter + 1;
                }
            }
            if (counter == 2) {
                // Appended the Prime number to the String
                primeNumbers = primeNumbers + i + " ";
            }
        }
        System.out.println("Prime numbers from 1 to n are :");
        System.out.println(primeNumbers);
    }

}

output:

Enter the value of n:
34
Prime numbers from 1 to n are :
2 3 5 7 11 13 17 19 23 29 31

3rd approach:

/*
 *
 *  To check the Number is either Prime or Not
 *
 */


package com.onlinetutorialspoint.javaprograms;

import java.util.Scanner;

public class PrimeNumberDemo3 {
   
   
    public static void main(String args[])
       {       
        int temp;
        boolean isPrime=true;
        Scanner scan= new Scanner(System.in);
        System.out.println("Enter a number for check:");
        //capture the input in an integer
        int num=scan.nextInt();
        for(int i=2;i<=num/2;i++)
        {
               temp=num%i;
           if(temp==0)
           {
              isPrime=false;
              break;
           }
        }
        //If isPrime is true then the number is prime else not
        if(isPrime)
           System.out.println(num + " is Prime Number");
        else
           System.out.println(num + " is not Prime Number");
       }

}

output:

Enter a number for check:
67
67 is Prime Number








Java Program for find a given number is Factorial or Not ?



In this tutorial we are going to implement the factorial of a given number in 2 different ways.
  1. Taking input from the user and find the factorial of the number.
  2. Taking input from the user and find the factorial of given number using recursion. 

1. Finding the Factorial of a Given Number : 

package com.javaprogrampoint.factorial;

import java.util.Scanner;


public class FactorialDemo {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number whose factorial is to be found: ");
        int n = scanner.nextInt();
        int result = findFactorial(n);
        System.out.println("The factorial of " + n + " is : " + result);
    }

    public static int findFactorial(int n) {
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result = result * i;
        }
        return result;
    }
}
 
Output:

Enter the number whose factorial is to be found: 6
The factorial of 6 is 720

2. Factorial of Given Number Using Recursion :

package com.javaprogrampoint.factorialrecursion;


public class FactorialRecursion {


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number whose factorial is to be found: ");

        int n = scanner.nextInt();

        int result = findFactorial(n);

        System.out.println("The factorial of " + n + " is : " + result);

    }


    public static int findFactorial(int n) {

        if (n == 0) {

            return 1;

        } else {

            return n * findFactorial(n - 1);

        }

    }

}

Output:

Enter the number whose factorial is to be found: 5
The factorial of 5 is : 120


Saturday, 19 December 2015

Java Program for Adding Two Numbers


public class AddingTwoNumbers {

    public static void main(String[] args) {
        System.out.println("Addtion of Two Numbers : " + add(2, 5));
    }

    public static int add(int number1, int number2) {
        return number1 + number2;
    }
}
Output :
Addtion of Two Numbers : 7

Friday, 18 December 2015

Java Helloworld Example




In this session, We are going to implement our first example on Java Hello World Example


public class HelloWorld{

 public static void main(String args[]){

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

 }

}

Output :
C:/> javac HelloWorld.java

C:/> java HelloWorld
Hello World !