हिंदी

Write a program with overloaded methods to implement the following: Overload a method fnSeries(), given that: (a) void fnSeries(int n) − to print all the natural numbers from 1 to n.

Advertisements
Advertisements

प्रश्न

Write a program with overloaded methods to implement the following:

Overload a method fnSeries(), given that:

  1. void fnSeries(int n) − to print all the natural numbers from 1 to n.
  2. void fnSeries (int n1, int n2) − to print all the natural numbers from n1 to n2 where n1must be greater than n2, otherwise print "Invalid".
  3. void fnSeries (double d1, double d2, int d) − to print d number of whole numbers between d1 and d2.
कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clOver
{
    void fnSeries(int n)
    {
        System.out.print("\n Series1 :: ");
        for(int k = 1; k <= n; k++)
        {
            System.out.print(" " + k);
        }
    }
    void fnSeries(int n1, int n2)
    {
        System.out.print("\n Series2 :: ");
        if(n1 > n2)
        {
            for(int k = n1; k >= n2; k--)
            {
                System.out.print(" " + k);
            }
        }
        else
        {
            System.out.print(" Invalid");
        }
    }
    void fnSeries(double d1, double d2, int d)
    {
        double max = Math.max(d1, d2);
        double min = Math.min(d1, d2);
        double dif = (max-min-1)/d; 
        int k = 1;
        
        System.out.println("\n Series3 :: dif = " + dif);
        while(k <= d)
        {
            System.out.print(" " + (min + dif));
            min = min + dif;
            k++;
        }
    }
    void main()
    {
        fnSeries(12);
        fnSeries(78, 71);
        fnSeries(20. 16, 10);
    }
}

Output:

Series1::1 2 3 4 5 6 7 8 9 10 11 12 
Series2:: 78 77 76 75 74 73 72 71
Series3 :: dif = 0.3
16.3 16.6 16.9 17.2 17.5 17.8 18.1 18.4 18.7 19.0

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 11: Programs Overall - Method Overloading [पृष्ठ २५३]

APPEARS IN

रुपा पंडित Computer Applications [English] Class 10 ICSE
अध्याय 11 Programs Overall
Method Overloading | Q 3. | पृष्ठ २५३
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×