मराठी

Write a program as per the given specification. Design a class to overload a method, Verify(), given that (a) boolean Verify (int n) - return true if n is a Prime number, false otherwise.

Advertisements
Advertisements

प्रश्न

Write a program as per the given specification.

Design a class to overload a method, Verify(), given that

  1. boolean Verify (int n) - return true if n is a Prime number, false otherwise.
  2. boolean Verify (int v, int f) - return true if f is a factor of v, false otherwise.

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

कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clOver
{
    boolean Verify(int n)
    {
       boolean p = true;
       for (int k = 2; k <= n/2; k++)//looking for a factor
       {
         //in range 2 to its half
         if(n%k =0)
         {
            p = false;
            break;
         }
       }
       return p;
    }
    boolean Verify (int v, int f)
    {
      boolean r = false;
      if(v% f=0)
      {
        r = true;
      }
      return r;
    }
    void main()
    {
      clOver ov = new clOver();
      boolean r1 = ov.Verify(17);
      ystem.out.println(" Result1 ="+ r1);
      boolean r2 = ov.Verify(6279, 15);
      System.out.println(" Result2 = " + r2);
    }
}

Output:

Result1 = true

Result2 = false

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 3: User-Defined Methods - Exercises [पृष्ठ ९४]

APPEARS IN

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

Englishहिंदीमराठी


      Forgot password?
Use app×