Question

In: Computer Science

Java It would be nice if you use many Functions. rite a method, besides main(), which...

Java

It would be nice if you use many Functions.

  1. rite a method, besides main(), which will calculate the lowest common multiple (LCM) of two numbers. For example, the multiples of 4 are 4, 8, 12, 16, 20, 24 …, and the multiples of 6 are 6, 12, 18, 24 …. The LCM is 12.
  2. Do NOT use any code from the Internet!
  3. Here are some more examples:
    3:5 LCM is 15
    4:8 LCM is 8
    5:7 LCM is 35
  4. Your method will take two parameters, and return the LCM for those numbers.
  5. Demonstrate your function is working by finding the LCM for 16:20 and 13:17

Solutions

Expert Solution

import java.util.*;

public class FindLcm
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter two numbers : ");
       int n=sc.nextInt();
       int m=sc.nextInt();
       int l=LCM(n,m);
       System.out.println("Lcm of "+n+" and "+m+" is "+l);
   }
   public static int LCM(int num1,int num2)
   {
       int lcm=0,m;
       m = (num1 > num2) ? num1 : num2;
for(int i=0;i<m;i++)
   {
   if(m%num1==0 && m%num2==0)
   {
      lcm=m;
   break;
   }
   m++;
   }
   return lcm;
   }
}

OUTPUT:-

// If any doubt please comment


Related Solutions

Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
What are the 4 main methods to organize a paper? Which method would you use to...
What are the 4 main methods to organize a paper? Which method would you use to structure a report on how a company has grown over the years (company growth) and why?
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main,...
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main, it willhave the following 7 functions:public static int triangle( int n )public static int multiply( int a, int b )public static void square( int size )public static void hollowSquare( int size )public static int factorial( int n )public static long fibonacci( int n )public static boolean prime( long n )These 7 functions should work as described below.Your main program should call each of these...
.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
how do you send the values of a method to the main in obj java. im...
how do you send the values of a method to the main in obj java. im having syntax errors and im not sure how to fix them or what the problem is. class Order { private int orderNum; private double orderAmount; private double orderDiscount;    public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double getOrderAmount() { return orderAmount; } public double getOrderDisc() { return orderDiscount; } public static void...
Question 2 D) Which of the following seaborn functions would you use if you want to...
Question 2 D) Which of the following seaborn functions would you use if you want to compare the variations (or standard deviations) of multiple variables? sns.countplot() sns.boxplot() sns.scatterplot() sns.barplot() E) Which of the following seaborn functions can be used to visualize categorical variables? sns.countplot() sns.pieplot() sns.scatterplot() None of the options F) Which of the following statements is not correct regarding the seaborn pairplot? The pairplot automatically filters numerical values and ignore categorical variables The pairplot uses either scatterplots or regression...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use a hash table. You can use either Java HashTable or Java HashMap. Refer to Java API for the subtle differences between HashTable and HashMap. Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion "...
Code using JAVA: must include a "Main Method" to run on "intelliJ". Hint: Use recursion " /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public boolean isSymmetric(TreeNode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT