Tuesday 31 July 2012

Pattern Programming

 Pattern Programming:
           Pattern programming is a series of program which may contain curtain pattern having an output into the form like shapes of triangle,diamond,kites,square,rectangle and many more.

       Below is some of the example of Pattern :




//Pattern Programming : Program of Kites from Java
class Pattern
{
    public static void main(String args[])
    {
        int no=4;
        for(int i=0;i<no;i++)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
        for(int i=no;i>=0;i--)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
        for(int i=1;i<no;i++)
        {
            for(int k=no;k>i;k--)
            {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
    }
}


Save this program as Pattern.java

Compile  Java program by using command     javac Pattern.java

Run this program by using command  java Pattern.java

See the Output :


If you want some more pattern output then just mail me @   nihalcse@gmail.com

Friday 27 July 2012

Armstrong Number

What is Armstrong Number ?
   An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.

for example:
  Lets consider the following number:
                                                         153
                                                         370
                                                         371
                                                         407

153=1^3+5^3+3^3
      =153

370=3^3+7^3+0^3
      =370

371=3^3+7^3+1^3
      =371

407=4^3+0^3+7^3
      =407

//Program to check Whether given number is armstrong number or not by using Java.
class ArmstrongNumber
{
   public static void main(String args[])
   {
     int a,b,c=0,n=153;
     a=n;
     while(n>0)
     {
        b=n%10;
        c=c+(b*b*b);
        n=n/10;
     }
    if(a==c)
    {
      System.out.println("This number is Armstrong number:"+c);
     }
    else
     {
       System.out.println("This number is not Armstrong number:"+c);
     }
  }
}


Save this program as      ArmstrongNumber.java

Compile it
                                     javac ArmstrongNumber.java

Run it 
                                     java ArmstrongNumber

Output
                                     
                                    This number is Armstrong number:153

If you have any trouble or wants some more program then just reach @ nihalcse@gmail.com

Wednesday 25 July 2012

Harmonic series

Harmonic Series

Harmonics means wavelengths of the overtones of a vibrating string are 1/2, 1/3, 1/4, etc., of the string's fundamental wavelength.  the phrase harmonic mean likewise derives from music.

Diagram indicate Harmonic series:

Program for Harmonic Series

class Harmonic
{
    public static void main(String args[])
    {
        int i,no;
        double j=0;
        no=5;                        //It shows length of Harmonic series
        while(no>0)
        {
           j=j+(double) 1 / no;
           no--;
        }
        System.out.println("Output of Harmonic Series:"+j);
    }
}


Save this program as                       Harmonic.java

Compile it by using command
                                                      javac Harmonic.java

run this program by using command
                                                
                                                       java Harmonic
OUTPUT will look like:

                                                       Output of Harmonic Series:2.28333333

If you want it some more program then paste your problem statement below on comment line.


Tuesday 24 July 2012

program for Fibonacci Series

What is fibonacci Series?
  Fibonaccci Series means the addition of last two adjecent number starting from 0&1.

Fibonacci Series is look like this,
     0 1 1 2 3 5 8 13 21 .....

//PROGRAM FOR FIBONNACI SERIES

class Fibonacci
{
   public static void main(Strings args[])
   {
      int a,b,c,no;
      a=0;
      b=1;
      no=5;                                 
      System.out.println(a);
      System.out.println(b);
     while(no>0)
      {
        c=a+b;
        System.out.println(c);
        a=b;
        b=c;
        no--;
    }
}


Compile your program by using command
 javac Fibonacci.java

if complied succesfully
 java Fibonacci

OUTPUT
0
1
1
2
3
5
8

ShareThis

Related Posts Plugin for WordPress, Blogger...