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. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string....
2. Create ACCESSOR FUNCTIONS in JAVA a) String moneyToString(int[] money); // Returns a nice looking string. Ex, "$6.25", "$0.21", "$4.01", "$2.00". MAKE SURE TO CONSIDER ALL EXAMPLES! b) *String moneyToText(int[] money); // Returns the Money as words. Ex,{123,51} => "one hundred and twenty three dollars and fifty one cents." YOU MAY ASSUME money <$1000. context: this is what I have so far package com.company; import java.util.*; public class Main { public static void main(String[]args) { int money[] = createMoney(12, 34);...
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...
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...
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...
Use JAVA language. Using the switch method concept create a program in which you have an...
Use JAVA language. Using the switch method concept create a program in which you have an artist (singer) and the list of his or her songs. Add 18 songs. Then alter the script to achieve the following in each test run: a) Print all the songs. b) Print only song 15. c) Print only song 19.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT