Monday, July 2, 2012

Java logical programs

Fibonacci Series

This Program generates Fibonnaci Series for a given number of times.
Output: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
public class FibonnaciSeries {
/*
* Generates the Fibonnaci Series
*/
public void generateSeries(int num) {
int f1, f2 = 0, f3 = 1;
System.out.println(“fib(0) = ” + f2);
for (int i = 1; i <= num; i++) {
System.out.println("fib(" + i + ") = " + f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
public static void main(String[] args) {
System.out.println("*****Fibonnaci Series*****");
FibonnaciSeries fb = new FibonnaciSeries();
fb.generateSeries(10);
}
}

Highest Prime Number

Given a number this program generates the highest prime number.
Output: If the input is 25, the highest prime number close to 25 is 23. It prints 23.

public class PrimeNumber {
/*
* Given a number, finds the highest prime number.
*/
public static int highestPrime(int n) {
int value = 1;
for (int i = 2; i <= n; i++) {
// System.out.println("n: " + n + " i: " + i + " mod: " + n%i);
if (i == n) {
value = n;
break;
}
if (n % i == 0) {
n--;
}
}
return value;
}
public static void main(String[] args) {
int value = highestPrime(25);
System.out.println("Prime:" + value);
}
}

Triangle Printing

Given a number, this program prints the numbers in the right angle manner.
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14

public class TrianglePrinting {
/*
* Prints the numbers in the right angle manner.
*/
public static void trianglePrinting(int n) {
int counter = 0, i = 1;
while (i <= n) {
counter++;
for (int j = 0; j < counter; j++) {
if (i > n)
break;
System.out.print(i + ” “);
i++;
}
System.out.println();
}
}
public static void main(String[] args) {
trianglePrinting(14);
}
}
Print Reverse number example 123 as 321

public class ReverseTest
{

public static void main(String[] args)
{
int n = 345897;
int sum = 0, rev = 0;
while(n!=0)
{
rev= n%10;
sum = (sum*10)+rev;
n = n/10;
}
System.out.println("Reverse number is "+sum);
}

}

Prime Number program

public class PrimeTest
{


public static boolean isPrime(int num)
{
boolean prime = true;
int limit = (int)Math.sqrt(num);
for(int i=2; i<=limit;i++)
{
if(num%i==0)
{
prime = false;
break;
}

}
return prime;
}
public static void main(String[] args)
{
for(int i=2;i<1000;i++)
{
if(isPrime(i))
{
System.out.print(" "+i);
}
}
}
}

Fibonacci Series

public class FibTest
{
public static void main(String[] args)
{
int f1=0,f2=0, f3 =1;
for (int i=1;i<20;i++)
{
f1 = f2;
f2= f3;
f3 = f1+f2;
System.out.print(" "+f3);
}

}

}


SumTest


public class SumTest
{
public static void main(String args[])
{
int n = 4566;
int sum =0;
while(n!=0)
{
sum = sum + (n%10);
n = n/10;
}
System.out.println("Sum is "+sum);
}
}

// Recursion

public class StringReverseTest
{
public static void main(String[] args)
{
String str = "Gangadhararao Bommasani";
str = reverse(str, str.length());
System.out.println(str);
}
public static String reverse(String input, int index)
{
if(index==0) return "";
return input.charAt(index-1)+ reverse(input, index-1);
}

}

Write a java Program to display Pyramid.
public class Pyramid {
    public static void main(String...strings){
        int i,j,k;
        for(i=1;i<=5;i++){
            for(j=5;j>=i;j--){
                System.out.print(" ");
            }
            for(k=1;k<=i;k++){
                System.out.print(" @");
            }
            System.out.println("");
        }
    }
}
Output :
           @
        @ @
      @ @ @
    @ @ @ @
  @ @ @ @ @

Check wheather String is palindrome or not?

import java.util.Scanner;
public class Practical1 {
public static void main(String argv[]){
System.out.print("Enter String ");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
StringBuffer sb = new StringBuffer(input);
String revStr = sb.reverse().toString();
if(revStr.equals(input)){
System.out.println("String is Palindrome");
}
else {
System.out.println("String is Not Palindrome");
}
}
}
/*Write a program to Find Factorial of Given no. */
class Factorial{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);                 //take argument as command line
          int result = 1;
          while(num>0){
                result = result * num;
                num--;
          }
          System.out.println("Factorial of Given no. is : "+result);
   }
}
/* Write a program to find sum of all integers greater than 100 and
   less than 200 that are divisible by 7 */
class SumOfDigit{
      public static void main(String args[]){
      int result=0;
      for(int i=100;i<=200;i++){
           if(i%7==0)
              result+=i;
      }
      System.out.println("Output of Program is : "+result);
   }
}
/* Write a program to Swap the values */
class Swap{
      public static void main(String args[]){
      int num1 = Integer.parseInt(args[0]);
      int num2 = Integer.parseInt(args[1]);
      System.out.println("\n***Before Swapping***");
      System.out.println("Number 1 : "+num1);
      System.out.println("Number 2 : "+num2);
      //Swap logic
      num1 = num1 + num2;
      num2 = num1 - num2;
      num1 = num1 - num2;
      System.out.println("\n***After Swapping***");
      System.out.println("Number 1 : "+num1);
      System.out.println("Number 2 : "+num2);
      }
}
/*Write a program to find whether given no. is Armstrong or not.
  Example :
           Input - 153
           Output - 1^3 + 5^3 + 3^3 = 153, so it is Armstrong no. */
class Armstrong{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      int n = num; //use to check at last time
      int check=0,remainder;
      while(num > 0){
           remainder = num % 10;
           check = check + (int)Math.pow(remainder,3);
           num = num / 10;
      }
      if(check == n)
            System.out.println(n+" is an Armstrong Number");
      else
            System.out.println(n+" is not a Armstrong Number");
   }
}
/* Write a program to Find whether number is Prime or Not. */
class PrimeNo{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
         int flag=0;
         for(int i=2;i<num;i++){
             if(num%i==0)
              {
                 System.out.println(num+" is not a Prime Number");
                 flag = 1;
                 break;
              }
         }
         if(flag==0)
             System.out.println(num+" is a Prime Number");
    }
}
/* Write a program to generate Harmonic Series.
   Example :
           Input - 5
           Output - 1 + 1/2 + 1/3 + 1/4 + 1/5 = 2.28 (Approximately) */
class HarmonicSeries{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      double result = 0.0;
      while(num > 0){
            result = result + (double) 1 / num;
            num--;
      }
      System.out.println("Output of Harmonic Series is "+result);
  }
}
/* Display Triangle as follow
    0
    1 0
    1 0 1
    0 1 0 1 */
class Output2{
      public static void main(String args[]){
           for(int i=1;i<=4;i++){
              for(int j=1;j<=i;j++){
                            System.out.print(((i+j)%2)+" ");
                    }
                    System.out.print("\n");
                 }
     }
}       

Advance Java Blogging

Java New Articles

Javas Latest News

Java Web Services and XML

Ajax Latest News

Mac OS Java Features

Advance Spotlights

Patterns Features