हिंदी

Observe the following code. It may contain Syntax, Logical or runtime errors import java.util.*; public class cl Factorial { void fn Factorial(int num) { for(int k = 1; k <= num; k++) f = f*k;

Advertisements
Advertisements

प्रश्न

Observe the following code. It may contain Syntax, Logical or runtime errors. Execute it in Java and identify the error, if any. Rewrite the code without the error. Also write the aim of the code.

import java.util.*;
public class cl Factorial
{
   void fn Factorial(int num)
   {
      for(int k = 1; k <= num; k++)
              f = f*k;
   }
   public void main()
   {
      int number = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the number:");
      number = sc.nextInt();
      int cpy = number, dg = 0, facdg = 0;
      while(cpy > 0)
      {
              dg = cpy % 10;
              facdg = factorial(dg);
              System.out.println("Factorial of "+dg+"="+facdg);
      }
   }
}
कोड लेखन
Advertisements

उत्तर

import java.util.*;
public class Factorial
{
    int fnFactorial(int num)
    {
        int f = 1;
        for(int k = 1; k <= num; k++)
        {
            f = f * k;
        }
        return f;
    }
    public static void main(String[] args)
    {
        int number = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number:");
        number = sc.nextInt();
        int cpy = number, dg = 0, facdg = 0;
        Factorial obj = new Factorial();
        while(cpy>0)
        {
            dg = cpy% 0;
            facdg = obj.fnFactorial(dg);
            System.out.println("Factorial of" + dg + "=" + facdg);
            cpy = cpy/10;
        }
    }
}
shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 3: User-Defined Methods - Exercises [पृष्ठ ९३]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 3 User-Defined Methods
Exercises | Q 7. (ix) | पृष्ठ ९३
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×