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
harina [27]
3 years ago
10

First write a method to calculate the greatest common divisor (GCD) of two positive integers using Euclid’s algorithm (also know

n as the Euclidean algorithm). Then write a main method that requests two positive integers from the user, validates the input, calls your method to compute the GCD, and outputs the return value of the method (all user input and output should be done in main). Check Wikipedia to find more information about GCDs1 and Euclid’s algorith
Computers and Technology
1 answer:
MAVERICK [17]3 years ago
7 0

Answer:

import java.util.Scanner;

public class Gcd

{

public static void main(String[] args) {

 Scanner input = new Scanner(System.in);

 System.out.print("Enter two numbers to find their GCD: ");

 int number1 = input.nextInt();

 int number2 = input.nextInt();

 

 if(number1 > 0 && number2 > 0)

     System.out.println("GCD of " + number1 +" and " + number2 +" is: " + findGCD(number1, number2));

       else

           System.out.println("Invalid Input!");

}  

public static int findGCD(int number1, int number2) {

    if(number2 == 0){

        return number1;

    }

return findGCD(number2, number1 % number2);

}

}

Explanation:

To be able to find GCD, we need to factorize the numbers and multiply common factors.

- <u>Inside the function:</u>

- Recursively divide <em>number1</em> by <em>number2</em> until <em>number2</em> is equal to zero.

- Return <em>number1</em>  when the <em>number2</em> is equal to zero. That means we found all the divisors of <em>number1</em>

- <u>Inside the main:</u>

- Ask user to input numbers

- Check if both numbers are greater than zero

- If they satisfy the condition, call the method and print the result.

You might be interested in
Flexplace and telecommuting are similar in that both allow you to _____. work in a location away from the office work different
viva [34]
Flexplace and telecommuting are similar in that both allow you to work in a different location four days a week. 
3 0
3 years ago
Write a class called Course that represents a course taken at a school. Represent each student using the modified Student class
Vinvika [58]

Answer:

The code is given below with its appropriate output

Explanation:

//Student.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Student {

public String firstName;

public String lastName;

public float[] marks;

Scanner scanner = new Scanner(System.in);

public Student(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}

public void getdetail() {

System.out.println("enter only five marks");

marks = new float[5];

int count = 0;

for (int i = 0; i < 5; i++) {

float mark = 0;

++count;

if (count <= 5) {

System.out.println("enter score " + count);

mark = scanner.nextFloat();

if (mark >= 0.0 && mark <= 100.0)

marks[i] = mark;

else {

System.out.println("you entered mark is invalid");

marks[i] = 0;

}

} else

System.out.println(" maximum is five only");

}

}

public void tostring() {

System.out.println("firstname :" + "\t" + firstName + "\t" + "lastname :"

+ "\t" + lastName + "\n" + "the marks are");

int count = 0;

for (float mark : marks) {

count++;

System.out.println("the mark of " + count + " is " + mark);

}

}

public void Averagecal() {

float sum = 0.0f;

for (float mark : marks) {

sum = sum + mark;

}

float avg = sum / 5;

System.out.println("average of a student is " + avg);

}

}

//Course.java

import java.io.*;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Course {

public String courseName;

public int number;

Student[] students;

Scanner scanner = new Scanner(System.in);

public Course(String courseName) {

this.courseName = courseName;

}

public void addStudents() {

System.out

.println("how many no of students you want to add for this particular course");

int count = scanner.nextInt();

students = new Student[count];

for (int i = 0; i < count; i++) {

System.out.println("enter the first name");

String fname = scanner.next();

System.out.println("enter the last name");

String lname = scanner.next();

Student s = new Student(fname, lname);

s.getdetail();

s.tostring();

s.Averagecal();

students[i] = s;

}

}

public void roll() {

int count = 0;

for (Student stud : students) {

count++;

}

System.out.println("the no of students in this " + courseName

+ " course is " + count);

}

}

//Driver.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.Scanner;

public class Driver {

public static void main(String[] args) {

Course course = new Course("java");

course.addStudents();

course.roll();

}

}

<u>output: </u>

C:\Users\Daniel\Desktop\New folder>javac Student.java

C:\Users\Danie;\Desktop\New folder>javac Course.java

C:\Users\Daniel\Desktop\New folder>javac Driver.java

C:\Users\Daniel\Desktop\New folder>java Driver

how many no of students you want to add for this particular course ?

1

enter the first name

john

enter the last name

rich

enter only five marks

enter score 1

89

enter score 2

91

enter score 3

85

enter score 4

78

enter score 5

96

firstname : john lastname : rich

the marks are

the mark of 1 is 89.0

the mark of 2 is 91.0

the mark of 3 is 85.0

the mark of 4 is 78.0

the mark of 5 is 96.0

average of a student is 87.8

the no of students in this java course is 1

C:\Users\Ravi\Desktop\New folder>

3 0
3 years ago
Write passage on computer virus​
const2013 [10]
In more technical terms, a computer virus is a type of malicious code or program written to alter the way a computer operates and is designed to spread from one computer to another. A virus operates by inserting or attaching itself to a legitimate program or document that supports macros in order to execute its code.
3 0
2 years ago
Read 2 more answers
Some websites are dedicated to cataloguing information. Since these sites contain so much data, these sites are usually organize
swat32
B databooks...........


....
3 0
3 years ago
Does single quote and double quote work same in javascript
Lemur [1.5K]

Answer:

yes!

Explanation:

the only difference is that some companies prefer one over the other as a standard

https://stackoverflow.com/questions/242813/when-should-i-use-double-or-single-quotes-in-javascript

but they work the same

5 0
2 years ago
Other questions:
  • Indicate the proper order (1-4) of the following PR strategic planning 4-step process. 1 Defining the problem 2 Evaluating the p
    10·1 answer
  • Which option allows you to customize the order of your data ?
    8·2 answers
  • In JAVA please:
    15·1 answer
  • Whoever understands this first and replies will be the brainliest.<br><br><br><br> Road work ahead?
    9·2 answers
  • What are digital forensic techniques? A. Identifying, extracting, and evaluating evidence obtained from digital media such as co
    11·1 answer
  • Describe an energy problem a city in 2050 will face
    6·1 answer
  • A RISC processor has 186 total registers, with 18 global registers. There are 12 register windows, each with 10 locals. How many
    5·1 answer
  • Hydraulic pressure is the same throughout the inside of a set of brake lines. What determines the amount of resulting mechanical
    7·1 answer
  • Which one you choosing? PS5 OR THE XBOX SERIES X???
    14·2 answers
  • What is malware? What are some signs that malware may be impacting the performance of your computer? How can you avoid malware?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!