मराठी

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

प्रश्न

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.

कोड लेखन
Advertisements

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 3: User-Defined Methods - Exercises [पृष्ठ ९४]

APPEARS IN

रूपा पंडित Computer Applications [English] Class 10 ICSE
पाठ 3 User-Defined Methods
Exercises | Q 8. (ii) | पृष्ठ ९४
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×