Sunday, January 15, 2012

Factorial Program in Java Using Class

It is a factorial program in java using class and method.
Public class is fact_main where main method is defined. Another class Factorial where fact method is defined.

Aim: Write a java program to calculate factorial of a number using class concept

Java Program Code
import java.util.*;
class Factorial
{
 private int i=1; 
 public int fact(int n)
 { 
  while(n>0)
  {
   i=i*n; 
   n--;
  }
  return i;
 }
}

public class fact_main
{
 public static void main(String[] args)
 {
  Factorial fact1=new Factorial();
  Scanner object1= new Scanner(System.in);
  System.out.println("******** FACTORIAL PROGRAM ********");
  System.out.print("Enter number to find factorial: ");
  int num=object1.nextInt();
  int k=fact1.fact(num);
  System.out.println("Factorial of "+num+" is:  "+k);
 }
}

Output
Program name: fact_main.java






Friday, January 6, 2012

Generate Pascal Triangle in Java - Java Code for Pascal Triangle

It is a basic program to generate pascal triangle in Java language.

Pascal Triangle
It is a triangular array of the binomial coefficients in a triangle. Named Pascal comes from French mathematician, Blaise Pascal. In the program user input number. It generate rows for input number. In first row there is only the number 1. From all next rows, add the number directly above and to the left with the number directly above and to the right to find the new value.

Program Code
//Java Program to print Pascal Triangle
import java.util.*;
class Pascal 
{ 
 public static void main(String[] args) 
 { 
    System.out.print("Enter no of rows for pascal triangle: ");
    Scanner console=new Scanner(System.in);
    int num=console.nextInt();
    for (int i = 0; i < num; i++)
                  {
                        int c = 1;
 
                        for(int j = 0;j < num-i; j++)
                        {
                              System.out.print("   "); //blank space
                        }
 
                        for(int k = 0; k <= i; k++)
                        {
                              System.out.print("   ");
                              System.out.print(c); //print c value 
                              System.out.print(" ");
                              c = c * (i - k) / (k + 1);
                        }
    System.out.println();
                        System.out.println();
                  }
                  System.out.println();       
    }
}

Input/Output
Save Program at your system by name "Pascal.java".

Input
Enter value for number of rows you want in pascal triangle

Output

Hope, you like this post. :)








Saturday, December 31, 2011

C Program For Pascal's Triangle - Pattern Pascal's Triangle in C Programming

It is a triangle array of the binomial coefficients. In this program input value for rows of pascal's triangle.
To get element of particular location by adding upper level(above) left value and above right value.


Program Code
//C Program to generate pascal triangle
#include"stdio.h"
#include"conio.h"
void main()
{
 int num,c,i,j,k;
 clrscr();
 printf("\nenter number of rows for pascal triangle: ");
 scanf("%d",&num);
 printf("\n\t********PASCAL TRIANGLE********\n\n");
 for(i=0;i<num;i++)
 {
  c = 1;
  for(j=0;j<num-i;j++)
  {
   printf("   ");
  }
  for(k = 0;k<=i;k++)
  {
   printf("   ");
   printf("%d",c); 
   printf(" ");
   c = c * (i - k) / (k + 1);
  }
  printf("\n");
  printf("\n");
 }
 printf("\n");
 getch();
}

Output











Wednesday, December 28, 2011

Fibonacci Series Program in Java Programming

It is a java program to generate Fibonacci series. In this program user input a number and prints the Fibonacci series for accepted number.

Java Fibonacci Series Program

Source Code
import java.lang.*;
import java.util.*;
class fibonacci
{
 public static void main(String arg[])
 {
        int a=0,b=1,c=0,i,num;
 Scanner scan = new Scanner(System.in);
 System.out.print("enter any number for fibonacci series:  ");
 num = scan.nextInt();
 System.out.println("Fibonacci Series is:"); 
 for(i=0;i<num;i++)
 {
  System.out.print(c+"\t");       
  a=b;
  b=c;
  c=a+b;
  }
 }
}

Output

In this java program create a class Fibonacci and use concept of Scanner class to accept a number from user. This program generate Fibonacci series for input.