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
How to delay sending an email in office 365
expeople1 [14]
You can’t it’s impossible unless your a hackers
3 0
3 years ago
Frankie used the ps command to find the process id of an application that he needs to stop. What command-line tool should he use
ASHA 777 [7]

In the case above, the command-line tool should he use to stop the application based on its process id is e option

What is the use of ps command?

The ps command is known to be one that helps enables a person to be able to look at the status of any kind of active processes on a system, and also that of display technical information in regards to the processes.

Note that In the case above, the command-line tool should he use to stop the application based on its process id is e option

Learn more about command-line tool from

brainly.com/question/3737699

#SPJ1

7 0
2 years ago
The NSA could be hiding small snooping programs in, let's just say, a picture of a cute kitten or a fun Android game." So how ca
grin007 [14]
One way could be to check the code of the program.
But of course this might not be possible... Not everyone knows a programming language. And even if you did, you might not know the programming language they used.

Another way is to download a program that monitors your incoming connections and outgoing connections from your computer/android/iphone.

When someone or something connects to your device, the program should alert you. Or when the computer makes a connection to a website/device.
8 0
3 years ago
Create a file named homework_instructions.txt using VI editor and type in it all the submission instructions from page1 of this
KATRIN_1 [288]

Answer:

mkdir homeworks    // make a new directory called homeworks.

touch homework_instructions.txt //create a file called homework_instruction

sudo -i    // login as root user with password.

chmod u+rwx homework_instructions.txt  // allow user access all permissions

chmod go-wx homework_instructions.txt // remove write and execute permissions for group and others if present

chmod go+r homework_instructions.txt  // adds read permission to group and others if absent.

grep POINTS homework_instructions.txt | ls -n

Explanation:

The Linux commands above first create a directory and create and save the homework_instructions.txt file in it. The sudo or su command is used to login as a root user to the system to access administrative privileges.

The user permission is configured to read, write and execute the text file while the group and others are only configured to read the file.

6 0
3 years ago
HELP:
Andrews [41]

Answer:

Id say its about 21

Explanation:

4 0
3 years ago
Other questions:
  • Which commercial email provider is most closely associated with Apple devices?
    9·1 answer
  • The What is the pathname that starts from the working directory. directory?
    7·1 answer
  • A colleague asks you to research a CPU for a new server that will run Windows Server 2019. He explains the server roles that he
    7·1 answer
  • Project 4: Strictly Identical arrays
    7·1 answer
  • How do you optimize a website using JavaScript?
    10·1 answer
  • Your company wants to use an IoT device but wants to make sure it does not interfere with other RF devices that use the 2.4 GHz
    13·1 answer
  • To apply the StartSafe philosophy to preventing workplace violence, what are the three steps to follow? A. Plan for your job req
    12·2 answers
  • _______ allows you to add formatting such as shapes and colors to text.
    14·1 answer
  • What is the name of the top-level parentless folder in a digital file system?
    8·1 answer
  • A list cannot use appendleft but a _____ can.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!