Write a code that calculates the Greatest Common Divisor (GCD) of two positive integers (user-defined inputs). Include an exception such that if their greatest common divisor equals to 1, it prints out the message, saying its GCD is 1.

Answers

Answer 1

Answer:

This program is written using Java programming language

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"

Line by Line Explanation

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

       num1 = input.nextInt();

       num2 = input.nextInt();

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

       int least = num1;

       if(num1 > num2)

       {

           least = num2;

       }

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

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

       {

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

           {

               gcd = i;

           }

       }

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);

       }


Related Questions

what does the U in fun stand for?​

Answers

Answer:

You and me

Explanation:

F is for friend and N is for anytime and anywhere

Martha is developing a software program in C++ and has a question about how to implement a particular feature. She performs an online search and finds a link to a site written by a programmer with posts about tips and tricks in using the C++ language. What kind of resource did she find? a forum a blog a tutorial an online help site

Answers

Answer:

A blog

Explanation:

A blog is a regularly updated website or web page, typically one run by an individual or small group, that is organised by posts.

Answer:

Martha found a blog

Trace en su cuaderno una hoja de cálculo de A1 a D15, escriba en cada celda un valor entre 100.000 y 500.000, luego escriba las fórmulas para hallar halle los siguientes valores: Sumar celdas columna B Restar celdas A12 menos D3 Multiplique Celdas columna C por celdas columna D Divida las celdas A11 hasta A15 entre celdas D11 a D15

Answers

Answer:

Vincent is a digital media professional. His bosses always praise his artwork. His company has just taken on a new project. Vincent has some thoughts and ideas about this new project that he wants to discuss with the senior employees. What kind of skills should he use to pitch his ideas?

Explanation   i had this on a quiz and past

Write the program code to find the cube of numbers from 1-5. Also do the dry run by using the trace table. (Javascript)

Answers

Answer:

Following are the JavaScript program

< html >

< body >

<  script >

var k,t; // variable declaration

for (k = 1; i < 6; k++)  // iterating the loop

{  

t=k*k*k;// find the cube of number

document.write(k);   // display the cube root

document.write(" < / br > " );  // moves to the next line

}

< / script >

< / body >

< / html >

Output:

1

8

27

64

125

Explanation:

We used the script tag for creating the JavaScript program. Declared the variable "k" in the script tag.Iterating the for loop .In the for loop the k variable is initialized by the 1 This loop is executed less then 6 .In each iteration we find the cube of number in the "t" variable and display that number by using document .write ().

Other Questions
Find the values of a and b such that x^2 + 3x + 4 = (x + a)^2 + bURGENT! *silly answers will be reported If HK is an altitude of triangle HIJ, IKH = (4x + 50), HIK = 43, and KI = x - 7, find Kl.Plz help now plz What do microbiologist do ? how many 3/8s are in 3 The fraction bars represent which equation? A fraction bar is labeled 1. Underneath the bar, 4 boxes are labeled one-fourth. Underneath the 4 boxes, 8 boxes are labeled one-eighth. 6 of those boxes are circled. One-fourth divided by one-eighth = 6 Three-fourths divided by one-eighth = six-eighths One-fourth divided by one-eighth = StartFraction 6 Over 8 EndFraction Three-fourths divided by one-eighth = 6 usesKatherine has 27 cups of yogurt to make smoothies. Each smoothie1 cup of yogurt. What is the maximum number of smoothies thatKatherine can make with the yogurt? In a film production, where should the camera focus if the director wants to emphasize the mens emotions? The computer hardware company requires all of its chips purchased from its supplier of computer chips to meet specifications of 1.2 cm with the margins of error of plus and minus 0.1 cm. Based on the computer chip supplied last month, the mean length of a computer chip was 0.9 cm. What are the elements that the production manager should consider in determining his company's ability to produce chips that meet specifications The next term in a sequence Comes from adding theprevious two terms. Find the missing terms below.2,,9, 25,66 the area of polygon MNOPQR = area of a rectangle that is 15 square units + area of a rectangle that is ___ square units. (input whole numbers only, such as 8)im not understanding too well so...please help :/ Can somebody help me with this ?? :) What point is the boy making when he talks about Christmas trees? People would not like Jeremy's grandfather is four times as old as he is. If you add their ages together, you will get ninety. How old is Jeremy's grandfather? A. 18 B. 19 C. 70 D. 72 The ____ is the person telling the story in a narrative. Please help me ture or false faith in the life and death of Jesus Christ incomplete without faith in the resurrection To solve the system given below using substitution, it is best to start bysolving the second equation for y.5x + 2y = 336y + x = 3 true or false Which statement is true about the polynomial 3j4k2jk3+jk32j4k+jk3 after it has been fully simplified? Please answer this correctly Please I need help, DONT SKIPP