1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ivanzaharov [21]
3 years ago
10

Write a code that calculates the Greatest Common Divisor (GCD) of two positive integers (user-defined inputs). Include an except

ion such that if their greatest common divisor equals to 1, it prints out the message, saying its GCD is 1.
Computers and Technology
1 answer:
STALIN [3.7K]3 years ago
8 0

Answer:

<em>This program is written using Java programming language</em>

import java.util.*;

public class calcGcd

{

   public static void main (String [] args)

   {

       int num1, num2;

       Scanner input = new Scanner(System.in);

       //Input two integers

       num1 = input.nextInt();

       num2 = input.nextInt();

       //Get least of the two integers

       int least = num1;

       if(num1 > num2)

       {

           least = num2;

       }

       //Initialize gcd to 1

       int gcd = 1;

       //Calculate gcd using for loop

       for(int i=1;i<=least;i++)

       {

           if(num1%i == 0 && num2%i == 0)

           {

               gcd = i;

           }

       }

       if(gcd == 1)

       {

           System.out.print("GCD is 1");

       }

       else

       {

           System.out.print("GCD is "+gcd);

       }

   }

}

Explanation:

To calculate the GCD, the program uses a for loop that iterates from 1 to the smaller number of the user input.

Within this iteration, the program checks for a common divisor of the two user inputs by the iterating element

The GCD is then displayed afterwards;

However, if the GCD is 1; the program prints the message "GCD is 1"

<em>Line by Line Explanation</em>

This line declares two integer numbers

       int num1, num2;

This line allows user the program to accept user defined inputs        

Scanner input = new Scanner(System.in);

The next two line allows gets inputs from the user

<em>        num1 = input.nextInt();</em>

<em>        num2 = input.nextInt();</em>

<em />

To calculate the GCD, the smaller of the two numbers is needed. The smaller number is derived using the following if statement

<em>        int least = num1;</em>

<em>        if(num1 > num2)</em>

<em>        {</em>

<em>            least = num2;</em>

<em>        }</em>

The next line initializes GCD to 1

       int gcd = 1;

The GCD is calculated using the following for loop

The GCD is the highest number that can divide both numbers

<em>        for(int i=1;i<=least;i++)</em>

<em>        {</em>

<em>            if(num1%i == 0 && num2%i == 0)</em>

<em>            {</em>

<em>                gcd = i;</em>

<em>            }</em>

<em>        }</em>

The following is printed if the calculated GCD is 1

       if(gcd == 1)

       {

           System.out.print("GCD is 1");

       }

Otherwise, the following is printed

       else

       {

           System.out.print("GCD is "+gcd);

       }

You might be interested in
Who is the first computer programmer​
enot [183]

Answer:

Ada Lovelace was the first computer programmer

8 0
2 years ago
Read 2 more answers
The geographic coordinate system is used to represent any location on Earth as a combination of latitude and longitude values. T
Pachacha [2.7K]

Answer:

Here is the script:  

function dd = functionDMS(dd)  

prompt= 'Enter angle in DD form ';

dd = input(prompt)

while (~checknum(dd))

if ~checknum(dd)

error('Enter valid input ');

end

dd = input(prompt)

end  

degrees = int(dd)

minutes = int(dd - degrees)

seconds = ( dd - degrees - minutes / 60 ) * 3600  

print degrees

print minutes

print seconds

print dd

Explanation:

The script prompts the user to enter an angle in decimal degree (DD) form. Next it stores that input in dd. The while loop condition checks that input is in valid form. If the input is not valid then it displays the message: Enter valid input. If the input is valid then the program converts the input dd into degrees, minutes and seconds form. In order to compute degrees the whole number part of input value dd is used. In order to compute the minutes, the value of degrees is subtracted from value of dd. The other way is to multiply remaining decimal by 60 and then use whole number part of the answer as minutes. In order to compute seconds subtract dd , degrees and minutes values and divide the answer by 60 and multiply the entire result with 3600. At the end the values of degrees minutes and seconds are printed. In MATLAB there is also a function used to convert decimal degrees to degrees minutes and seconds representation. This function is degrees2dms.

Another method to convert dd into dms is:

data = "Enter value of dd"

dd = input(data)

degrees = fix(dd);

minutes = dd - degrees;

seconds = (dd-degrees-minutes/60) *3600;

8 0
4 years ago
Which of the following groups would an animal with an exoskeleton, segmented body, and jointed appendages belong to???
Annette [7]
All arthropods have exoskeletons, like insects, spiders and crustaceans.
7 0
3 years ago
Read 2 more answers
Please help!! thank you so much!! &lt;3
Zepler [3.9K]

Answer:

D. 4x as much

Explanation:

The answer is pretty simple. The question is asking about 10m/s and 20m/s right? Take a look at the energy at each point on the graph. At 10m/s the kinetic energy is 100 on the graph. At 20m/s the kinetic energy is 400. You'll notice that 100 x 4 is 400 - That's the answer.

You can also prove this mathematically. The formula for kinetic energy is

\frac{1}{2}mv^2

The question says: constant mass so we can ignore that for now.

(1/2) * 10^2 is (1/2) * 100 = 50

for the 20m/s ball it would be

(1/2) * 20^2 = 400 * 1/2 = 200

Once again, you see that it is 4x

3 0
3 years ago
Read 2 more answers
7. Little Big Planet advanced the platforming genre of games by __________.
patriot [66]

Answer:

Explanation:

c because it will hook the person

5 0
3 years ago
Other questions:
  • Write an original idiom that uses the word dog or dogs im in 4th
    5·2 answers
  • Which searching method requires that the list be sorted?
    5·1 answer
  • How long does it take to design and program a character?
    14·1 answer
  • Sonora wants to extend the cells to be added in her formula. what is the quickest way to add more sells?
    10·2 answers
  • I have an LG phone. Since I just bought my phone, my bill to text message isn't paid yet. I texted a couple of things playfully
    5·2 answers
  • Which computer company was named after an orchard? dell, apple, or ibm
    11·1 answer
  • A user has just reported that he downloaded a file from a prospective client using IM. The user indicates that the file was call
    11·1 answer
  • Write a multi-way if statement that compares the double variable pH with 7.0 and makes the following assignments to the bool var
    11·1 answer
  • Which option is created specifically for giving a slide presentation in front of an audience and works when the speaker is utili
    14·1 answer
  • Consider the following implementation of a search method:
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!