Write a program that asks the user to input an integer, i, and determines the largest consecutive pair of numbers whose product is divisible by 19 from the first i numbers in the series.
Note: You may need to declare some of variables as long instead of int in order to make sure they can get large enough.
1
Expert's answer
2016-02-16T02:59:42-0500
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter an integer value: "); int s = sc.nextInt(); System.out.print("Please enter i: "); int i = sc.nextInt(); long result = -1; for (long l = i + s - 1; l >= s; l--) { if ((l * (l+1)) % 19 == 0) { result = l; break; } } if (result == -1) System.out.println("No consecutive pair has been found withing the range"); else System.out.println("The largest consecutive pair whose product is divisible by 19 within the range is: " + result + ", " + (result+1)); } }import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter an integer value: "); int s = sc.nextInt(); System.out.print("Please enter i: "); int i = sc.nextInt(); long result = -1; for (long l = i + s - 1; l >= s; l--) { if ((l * (l+1)) % 19 == 0) { result = l; break; } } if (result == -1) System.out.println("No consecutive pair has been found withing the range"); else System.out.println("The largest consecutive pair whose product is divisible by 19 within the range is: " + result + ", " + (result+1)); } }
Comments
Leave a comment