English

Write a program as per the given specification. Design a class to overload a method Series() given that (a) long Series (int n) – Returns the product of first n natural numbers (1*2*3 * ... * n)

Advertisements
Advertisements

Question

Write a program as per the given specification.

Design a class to overload a method Series() given that

  1. long Series (int n) – Returns the product of first n natural numbers (1*2*3 * ... * n)
  2. long Series (int n1, int n2) – Returns the sum of natural numbers from n1 to n2 10 + 11 + 12 + 13 + 14 + 15, here assuming n1 = 10, n2 = 15

Write the above methods and the main () method to call the two overloaded methods.

Code Writing
Advertisements

Solution

import java.util.*;
class clOver
{
   long Series(int n)
   {
      long p = 1;
      for(int k = 1; k<=n;k++)
       {
           p = p * k;
       }
       return p;
   }
   long Series(int n1, int n2)
   {
       int sum = 0;
       for(int k = n1; k <= n2; k++)
       {
           sum = sum + k;
       }
       return sum;
   }
   void main()
   {
       clOver obr = new clOver();
       long result1 = obr.Series(7);
       System.out.println(" Result1 = " + result1);
       long result2 = obr.Series(100, 200);
       System.out.println(" Result2 = " + result2);
   }
}

Output:

Result1 = 5040

Result2 = 15150

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 8. (ii) | Page 94
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×