Monday, 30 January 2012

Computing factorial using method

  1. Start main body with starting curly braces and declare two variables lets say a and b.
  2. Take input inside a using easyin method.
  3. choose a proper name of method lets say factii and call the method and assign value of it to b. like b=factii(a);
  4. When compiler will read the caller it will find the method factii from the code and will reach there.
  5. write output statement that prints the final value. like ..............println(" "+b);
  6. Write ending curly braces.
  7. Make a method have name factii and one parametre of int type and should have int return type like public static int factii(int x). This x will take the value of a from the caller in the 3rd point.
  8. Initialize a variable lets say c =1
  9.  Write starting curly braces and then Start a loop from 1 to less then x and increment it by 1.
  10. Write starting curly braces of this loop.
  11. Inside loop write a statement like c=c*x;
  12. Write ending curly braces of method factii.
  13. Write return statement like return c; so it will return  the final value of c to the b from the caller and then on the fourth line value of b will be showed.
Note: Method main and user defined method should be inside the body of class.

No comments:

Post a Comment