Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

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






Tuesday, November 22, 2011

C Program to Calculate Factorial of a Number

It is a sample c program to calculate factorial of a number.

Program Source Code


#include"stdio.h"
#include"conio.h"
void main()
{
int fact=1,i,n;
clrscr();
printf("\n ***FIND THE FACTORIAL NUMBER OF ANY
NUMBER***");
printf("\n Enter the value of n:--");
scanf("\n %d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n factorialis:-- %d",fact);
}
getch();
}

Output


Calculate Factorial of a Number

Saturday, November 19, 2011

C Program With Recursion: Find Factorial Using Recursion in C Programming

It is a sample program example which demonstrate recursion in C programming. It is a factorial program with recursion. Accept number from the user and generate factorial of accepted number using recursion.

Program Code
#include"stdio.h"
#include"conio.h"
fact(int n);
void main()
{
 int n,r;
 clrscr();
 printf("************* Find Factorial of Number Using Recursion *************");
 printf("\n enter a number ");
 scanf("%d",&n);
 r=fact(n);
 printf("\n Factorial of %d is =  %2d\t",n,r);
 getch();
}

fact(int n)
{
 int f;
 if((n==0)||(n==1))
  return(1);
 else
  f=n*fact(n-1);
 return(f);
}



Output