Question

In: Computer Science

read in firstNum. 2) read in secondNum. 3)  run the loop,  If secondNum is 0 then...

read in firstNum.

2) read in secondNum.

3)  run the loop,  If secondNum is 0 then print out firstNum, otherwise, temp =firstNum % secondNum, firstNum = secondNum, secondNum = temp.

Your code MUST have your name, and date, and description of the algorithms as comments.

Submit GCD.java file and the screen shoot of compile and run result (image file or PDF file) from JGRASP.

 

//This program demonstrates parts of greatest common divisor
// It shows how to calculate the remainder of two integers and also
// how a while loop works with a place holder (temp) variable

// Algorithm
// Read in two positive integer
// Using a while loop, print the remainder value, and then decrement the value
// Finally, print out the final value of gcd

import java.util.Scanner;

public class GCD {
 
    public static void main(String[] args) {
      
       int firstNum;
       int secondNum;
       int gcd =0;
      
      
       Scanner input = new Scanner( System.in );
       //user input the first number

       ……………………………………

       ……………………………………

       ……………………………………
      

      //user input the second number

        ………………………………………

        ………………………………………..

         ………………………………………..
      
       //if the second number is 0

      
        //while loop   
       while ( secondNum != 0){ 
          int temp = firstNum%secondNum;

           ……….............................................

          ........................................................


           System.out.println(temp);        
       }
             
      System.out.println("And finally gcd has the value of  " + gcd);

     }// of main
} // of class GCD

Solutions

Expert Solution

import java.util.*;
public class Main
{
   public static void main(String[] args) {
       int firstnum,secondnum,gcd=0,temp=0;
       Scanner s=new Scanner(System.in);
       firstnum=s.nextInt();//read the first Number
       secondnum=s.nextInt();//read the second value
       if(secondnum==0)//if second number is zero then gcd is first number
       {
       gcd=firstnum;
       }
       else{
       while(secondnum!=0)//we will loop until the num2 becomes zero
       {
       temp=secondnum;//assigm secondnum to temporary variable
       secondnum=firstnum%secondnum;//calculate the remainder
       firstnum=temp;//again assign the firstnum to secondnum i.e temporary value
       System.out.println("Divisor "+temp);
       }
       gcd=firstnum;//the final divisor is the gcd
       }
       System.out.println("And finally gcd has the value of " + gcd);
   }
}


Related Solutions

exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data.
Consider the matrix A given by [ 2 0 0 ] [ 0 2 3 ]...
Consider the matrix A given by [ 2 0 0 ] [ 0 2 3 ] [ 0 3 10 ] (20) Find all its eigenvalues and corresponding eigenvectors. Show your work. (+5) Write down the entire eigendecomposition (i.e. the matrices X, Lambda, and X inverse) explicitly.
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve...
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve using an annihilator 2) Please solve using the Method of Variation of Parameters Thank you.
Consider the following reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0,...
Consider the following reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 Find the number of Page Faults with FIFO, Optimal Page Replacement, and LRU with four free frames that are initially empty. Which algorithm gives the minimum number of page faults?
Python Create Loop 1 = 1 1 + 2 = 3 1 + 2 + 3...
Python Create Loop 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Create a loop in python that makes this pattern
0. 0. 0. 0.0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 2. 2. 2. 3. 4.
0. 0. 0. 0.0. 0. 0. 0. 0.   1. 1. 1. 1. 1. 1. 2. 2. 2. 3.   4. A.)MEAN – B.)MEDIAN - C.)MODE - D.)STANDARD DEVIATION – E.)5 NUMBER SUMMARY – F.)BOX AND WHISKERS PLOT – G.) OUTLIERS-
0. 0. 0. 0.0. 0. 0. 0. 0. 1. 1. 1. 1. 1. 1. 2. 2. 2. 3. 4.
0. 0. 0. 0.0. 0. 0. 0. 0.   1. 1. 1. 1. 1. 1. 2. 2. 2. 3.   4. A.)5 NUMBER SUMMARY – B.)BOX AND WHISKERS PLOT – C.) OUTLIERS-
MATLAB PROBLEM convert the for loop to a while loop. vec= [1 2 3 4 5]...
MATLAB PROBLEM convert the for loop to a while loop. vec= [1 2 3 4 5] newVec= [] for i=vec if i>5 new vec=[newvec, i] end end end
Consider the given matrix. 3    0    0 0    2    0 16   ...
Consider the given matrix. 3    0    0 0    2    0 16    0    1 Find the eigenvalues. (Enter your answers as a comma-separated list.) λ = 1,2,3 Find the eigenvectors. (Enter your answers in order of the corresponding eigenvalues, from smallest eigenvalue to largest.)
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3};...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3}; (NOTE: please consider braces {} here as usual square brackets for array index). Assume that i is stored in register $s1, and the base address of the array A is in $s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop: loop: sll $t0, $s1,                add $t0, $t0,                lw $t1, 20($s2)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT