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
Sierra owns a small business and handles many responsibilities, from logistics to marketing. She's seen a lot of success with Go
Elenna [48]

Answer:

A Smart Display campaign

Explanation:

She can go for Smart Display Campaign.

4 0
4 years ago
When are numbered lists generally used
tatuchka [14]

C. When listing items that have an order of priority.

Explanation:

There are a lot of ways to list items such as alphabets and Roman numerals.

And the all have their respective reasons.

When numbering in order of priority, it is more appropriate to use numbers instead of alphabets and Roman numerals.

8 0
3 years ago
Which two computer peripherals are connected to the computer through a port?
chubhunter [2.5K]
Most computer devices are connected to the computer through port
Keyboard through usb port
Printer through usb port
Hand point device through usb port
Also computer equipped with LPT port for printers and COM port for additional devices like external modems e.t.c
7 0
4 years ago
Were is the hype house
balandron [24]

Answer:

LA

Explanation:

where the rich people live

8 0
3 years ago
Read 2 more answers
Compare using a compiler with using an interpreter to translate high-level programming code
uranmaximum [27]
You can translate high level programming code by using java script.
6 0
3 years ago
Read 2 more answers
Other questions:
  • Modern operating systems decouple a process address space from the machine’s physical memory. List two advantages of this design
    15·1 answer
  • The first thing to do when your computer gives you a error message is
    7·2 answers
  • Ryan wants to ensure that all the words in this document are evenly spaced so that they look neat and readable . Which option in
    12·1 answer
  • Which loan type requires you to make loan payments while you’re attending school?
    9·1 answer
  • DOS was the most common operating system for Microsoft-based computers before the introduction of Windows. DOS required the user
    5·1 answer
  • I have answered 22 questions and my messages are still not working what is it that i'm doing wrong?
    6·1 answer
  • Why might you use a navigation form instead of tab pages? The navigation form allows for several levels and sublevels to be coll
    7·2 answers
  • Points! taga pilipinas ba kayo?​
    10·1 answer
  • Discuss the role of the concept behind the "Internet of Things (IoT)" in today's digitally connected society.
    11·1 answer
  • Problem Statement − Suppose the problem statement at hand is to contain the attrition that happens in companies worldwide. High
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!