English

Given below is the program with errors in it. Identify the errors. Rewrite the program and predict the output. import java.util.*; class ProGram { void main() { int n=0, tn = 0, p = 0; Scanner

Advertisements
Advertisements

Question

Given below is the program with errors in it. Identify the errors. Rewrite the program and predict the output.

import java.util.*;
class ProGram
{
      void main()
      {
         int n=0, tn = 0, p = 0 ; Scanner sc = new Scanner (System.in);
         System.out.print("\n Enter total terms (n) :");n = sc.nextInt();
         for(tn = n; tn<=1; tn--)
         {
             p =p*tn;
             System.out.print("Factorial of n =" + p );
         }
      }
}
Code Writing
Advertisements

Solution

import java.util.*;
class ProGram
{
    void main()
    {
      int n=0, tn = 0, p = 1; Scanner sc=new Scanner(System.in);
      System.out.print("\n Enter total terms (n):");n=sc.nextInt();
      for(tn = n; tn>= 1; tn--)
      {
          p=p*tn;
      }
      System.out.print("Factorial of n ="+p);
    }
}

Factorial of n

shaalaa.com
  Is there an error in this question or solution?
Chapter 1: Revision of Class 9 Syllabus - Exercises [Page 48]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 1 Revision of Class 9 Syllabus
Exercises | Q 6. (v) | Page 48
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×