In: Computer Science
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
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);
}
}