English

Observe the following code. It may contain Syntax, Logical or runtime errors. import java.io.* ; class Paper } int fnBox(int limit) { for(int sum = 0, t= 1; t <= limit;t++) sum = sum + t; return sum;

Advertisements
Advertisements

Question

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.io.*;
class Paper
}
   int fnBox(int limit)
   {
      for(int sum = 0, t= 1; t <= limit;t++)
              sum = sum + t;
      return sum;
   }
   void main()
   {
      int N;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter limit:");
      N = sc.nextInt();
      System.out.print("Result=" + fnBox(N));
   }
}
Code Writing
Advertisements

Solution

import java.io.*;
import java.util.*;
class Paper
}
   int fnBox(int limit)
   {
      for(int sum = 0, t= 1; t <= limit;t++)
              sum = sum + t;
      return sum;
   }
   void main()
   {
      int N;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter limit:");
      N = sc.nextInt();
      System.out.print("Result=" + fnBox(N));
   }
}
shaalaa.com
  Is there an error in this question or solution?
Chapter 3: User-Defined Methods - Exercises [Page 94]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 3 User-Defined Methods
Exercises | Q 7. (x) | Page 94
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×