Saturday, 22 March 2014

Define a thread using Thread class to generate the factorials of first 10 natural numbers. Create an instance for this thread & then activate it.

import java.io.*;
import java.util.*;
class Factorial extends Thread
{
    public void run()
    {
        int fact=1;
        for(int i=1;i<=10;i++)
        {
            fact=fact*i;
            System.out.println("Factorial of "+i+" is "+fact);
        }
    }

    public static void main(String args[])
    {
        Factorial f1=new Factorial();
        f1.start();
    }
}

No comments:

Post a Comment