मराठी

Write a program by using a class with the following specifications: Class name : Hcflcm Data members/Instance variables : int a,b Member Methods: Hcflcm(int x, int y): - Computer Applications

Advertisements
Advertisements

प्रश्न

Write a program by using a class with the following specifications:

Class name : Hcflcm
Data members/Instance variables : int a,b
Member Methods:
Hcflcm(int x, int y): constructor to initialize a=x and b=y. 
void calculate(): to find and print hcf and km of both the numbers.

कोड लेखन

उत्तर

import java.util.*;
class Hcflcm{
inta, b;

Hcflcm(int x, int y)
{a = x;
b = y;
}

void calculate()
{int p, hcf = 0, lcm;
p = Math.min(a, b);
for (int i = 1; i <= p; i++)
{ if (a % i == 0 && b % i == 0)
hcf = i;
}
lcm = (a * b) / hcf;
System.out.println("HCF = " + hcf);
System.out.println("LCM = " + lcm);
}

public static void main(String args[])
{Scanner ob = new Scanner(System.in);
System.out.println("Enter the two numbers");
Hcflcm H = new Hecflcm(ob.nextInt(),ob.nextInt());
H.calculate();
}}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 7: Constructors - EXERCISES [पृष्ठ ४१९]

APPEARS IN

अविचल Computer Applications [English] Class 10 ICSE
पाठ 7 Constructors
EXERCISES | Q VI. 1. | पृष्ठ ४१९
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×
Our website is made possible by ad-free subscriptions or displaying online advertisements to our visitors.
If you don't like ads you can support us by buying an ad-free subscription or please consider supporting us by disabling your ad blocker. Thank you.