How to print multiples of a given variable in java? - java

There are 3 numbers that are given to the user. These three numbers define the multiple, and the range. For example if these three numbers are (3, 6 , 17) the program should print 6,9,12,15. The first number is the base multiple and the second and third numbers are the lowest and the highest numbers (the range). I also know that I do not need all the import statements that I have.
This is what I have so far but I'm not sure how to continue.
import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.Math.*;
public class printMultiplesOf{
public static void main (String [] args){
Scanner reader = new Scanner(System.in);
int num1, num2, num3;
System.out.println("Enter the 1st number");
num1 = reader.nextInt();
System.out.println("Enter the 2nd number");
num2 = reader.nextInt();
System.out.println("Enter the 3rd number");
num3 = reader.nextInt();
printMultiplesOf(num1, num2, num3);
}
public static void printMultiplesOf(int num1, int num2, int num3){
int start = num2
int end = num3
for (int i = num1; i <= num1; i++){
System.out.println(i + " ");
}
}
}

The main issue here is finding the loop's start and end point. The start point needs to the first number divisible by num1 equal or larger than num2. You can find it by dividing num2/num1 using floating-point division, ceil the result and multiply it back by num1. Similarly, the loop's end point should be the largest number divisible by num1 which is less than or equal to num3. You can find it by dividing num3/num1 using integer division (which would effectively floor the result and then multiplying it back again by num1. From there on, it's just a matter of looping in steps the size of num1. E.g.:
public static void printMultiplesOf(int num1, int num2, int num3) {
int start = ((int) Math.ceil((double) num2 / num1)) * num1;
int end = (num3 / num1) * num1;
for (int i = start; i <= end; i+= num1) {
System.out.println (i + " ");
}
}

Loop through all the numbers in the range, and check if it's a multiple.
public static void printMultiplesOf(int num1, int num2, int num3) {
for(int i=num2; i<= num3; i++){
if(i % num1 == 0)
System.out.print(i +" ");
}
}

This is your function. You simply add your num1 to i each time loop is executed.
I tried it and it is working ;)
public static void printMultiplesOf(int num1, int num2, int num3){
int multi = num1;
int start = num2;
int end = num3;
for (int i = start; i <= end; i += multi){
System.out.println(i + " ");
}
}

Related

Why does my for loop return only half the elements I expect it to?

I would like to receive user input on n elements for the Fibonacci sequence. The scanner and calculations themselves seem to be working fine -- the input is successfully grabbed and the sequence calculated. It's even able to correctly disqualify inputs of a negative value. However, it is not actually returning 'n' elements. Curiously, I noticed that the loop consistently returns exactly half the elements requested, albeit in the correct order. If n = 20, the first 10 numbers are returned.
Here's the code:
public class fibonacci_input {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myObj = new Scanner(System.in);
int num1 = 0;
int num2 = 1;
int n;
System.out.println("How many elements would you like in your Fibonacci sequence?");
n = myObj.nextInt();
if (n < 0)
System.out.println("Positive numbers only!");
else
System.out.println("Your Fibonacci sequence with " + n + " elements is:");
{ for (int i = 1; i <= n; ++i)
{
System.out.print(num1 + " ");
int num3 = num1 + num2;
num1 = num2;
num2 = num3;
++i;
}
}
}
}
I'm struggling to imagine what the problem could be. I first thought it might be a problem with the test condition statement, but similar (working) solutions online seem to have their loops formatted almost identically. Is it perhaps a problem with my variable declarations? I've tried multiple changes to no avail so would appreciate another set of eyes :)
You could just delete the second i++; statement in your code, this one is making the for loop run half times as expected, since the i variable is being incremented twice on each iteration
public class fibonacci_input {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int num1 = 0;
int num2 = 1;
int n;
System.out.println("How many elements would you like in your Fibonacci sequence?");
n = myObj.nextInt();
if (n < 0)
System.out.println("Positive numbers only!");
else
System.out.println("Your Fibonacci sequence with " + n + " elements is:");
{ for (int i = 1; i <= n; ++i)
{
System.out.print(num1 + " ");
int num3 = num1 + num2;
num1 = num2;
num2 = num3;
//++i;
}
}
}
}

Print data from another method, in a while loop in the main method only once

The goal of this assignment is to read two numbers from the user, compute the distance between those numbers, then continue to ask the user for two numbers until they are equal, each time competing the distance, and the goal is to find the minimum distance between two numbers. We have to create a method that finds the minimum distance, and once the two numbers are equal, to print that minimum distance (only once). And the distance between the numbers that are equal does not count as a minimum distance. How do I do this? Thank you.
This is the code that I currently have.
double myMin = Double.MAX_VALUE;
while ( !(num1==num2)) {
double dist;
pairsMin(dist,myMin,num1,num2);
// re-ask for user input of num1 and num2
Min1 = pairsMin(dist,myMin,num1,num2);
}
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
} // end of main method
public static double pairsMin( double dist, double myMin, double num1,double num2){
dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
} // end of pairsMin method
You have to make minor changes to your logic. Try below
public static int pairsMin(int min, int num1, int num2) {
int dist = 0;
dist = Math.abs(num1 - num2);
if (dist != 0 && dist < min) {
min = dist;
}
return min;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num1;
int num2;
int mymin = Integer.MAX_VALUE;
do {
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
System.out.print("Enter number 2: ");
num2 = kb.nextInt();
mymin = pairsMin(mymin, num1, num2);
} while (!(num1 == num2));
System.out.print("\nThe minimum distance is: " + mymin + "\n");
} // end of main method
This is what I know have, using your suggestions, and it is working. I didn't think that my if statement in my while loop would execute because I thought once num1==num2, that the while loop would end, but it worked. Thank you!
import java.util.Scanner;
public class hw5_pairs {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number 1: ");
double num1 = in.nextDouble();
System.out.print("Enter number 2: ");
double num2 = in.nextDouble();
double Min1;
double myMin = Double.MAX_VALUE;
double dist = Math.abs(num1-num2);
while ( !(num1==num2)) {
pairsMin(dist, num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
Min1 = pairsMin(dist,num1, num2, myMin);
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
}
}
}
public static double pairsMin( double dist, double num1, double num2, double myMin){
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
My new problem is that myMin is equalling the last distance before the numbers are equal instead of the actual minimum. e.g. say the first two numbers I enter are 1 and 2, and the next are 1 and 3, and then 1 and 1. It is saying my minimum is 2.0.
This is what I'm supposed to get for the assignment.
Enter number 1: 9
Enter number 2: 1
Enter number 1: 7
Enter number 2: 2
Enter number 1: 4
Enter number 2: 4
The minimum distance is: 5.0.
Enter number 1: 20
Enter number 2: 3
Enter number 1: 23
Enter number 2: 23
5.0 + 17.0 = 22.0
MY CODE:
double myMin = Double.MAX_VALUE;
double Min1,Min2;
while ( !(num1==num2) ) {
pairsMin( num1, num2, myMin);
Min1 = pairsMin( num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n\n");
myMin = Double.MAX_VALUE;
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
while ( !(num1==num2)) {
pairsMin( num1, num2, myMin);
Min2 = pairsMin(num1,num2,myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if(num1==num2) {
double totMin = Min1+Min2;
System.out.print("\n" + Min1 + " + " + Min2 + " = " + totMin + "\n");
}
}
}
} // end while loop
} // end main method
public static double pairsMin( double num1, double num2, double myMin){
double dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}

Fibonnaci sequence explanation

Can someone explain to me some parts of this code:
public static void sequence(int nterms){
int num1 = 0;
int num2 = 1;
int num = 2;
if (nterms <= 0){
System.out.println("Enter a positive integer");
}
else if (nterms == 1){
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1 );
System.out.println(num2);
while (num < nterms){
int nth = num1 + num2;
System.out.println(nth);
num1 = num2;
num2 = nth;
num++;
}
}
}
The number sequence that's in the output is correct. So the code works. But why do you do num++ in the end? I know nth is previous two numbers added together but why do you nterms == 1 and print "" +num1? I don't get that.
Here it is
public static void sequence(int nterms){
/*instancation of needed variables : nth = num1+numb2 later in the code*/
int num1 = 0;
int num2 = 1;
/*num is the number of term you've encoutered, as the first two are in
the initialization, you start at 2*/
int num = 2;
if (nterms <= 0){ //check wether the function has a correct input or not*/
System.out.println("Enter a positive integer");
}
else if (nterms == 1){ /*if it's the first term we know in num1*/
System.out.println(" fibonacci sequence: " + num1);
}
else{
System.out.println(num1);/*we print the first two terms*/
System.out.println(num2);
while (num < nterms){ /*we loop till we reached the wanted number*/
/*we know a term is equal to the two terms before it, at least I hope you do*/
int nth = num1 + num2;
System.out.println(nth); /*we've got our number so we print it*/
num1 = num2; /*we make sure num1 and num2 are the last two encountered, so num1 becomre num2 and num2 become the current term (nth)*/
num2 = nth;
num++;/*we increment the variable as we've met printed a new term*/
}
}
}

Program is exiting at the wrong time

I've gotten my loop to work and exit when the value '999' is entered.
I'm not sure what to do from here as my program isn't functioning exactly how I want it to.
Currently, the loop is exiting after the values 'num1' 'num2' and 'num3' have been fulfilled. I'm wanting the program to exit as soon as the value '999' is entered; not when num1, 2 and 3 have all been entered.
Here's my code:
import java.util.Scanner;
public class SumAverage {
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
System.out.printf("Please Enter the first number%n");
float num1 = keyboard.nextFloat();
System.out.printf("Please enter the second number%n");
float num2 = keyboard.nextFloat();
System.out.printf("Please enter the third number%n");
float num3 = keyboard.nextFloat();
float sum = (num1 + num2 + num3); //calculates sum
float average = (num1 + num2 + num3) / 3; //calculates average
for(int x = 0; x < 1; x++) {
if (num1 == 999 || num2 == 999 || num3 == 999) {
break;
}
System.out.printf("Sum: %.0f %nAverage: %.2f", sum, average);
}
}
}
If you really want the program to end as soon as a value of 999 is entered, you can change your main() method to this:
public static void main(String[] args) {
System.out.printf("Please Enter the first number%n");
float num1 = keyboard.nextFloat();
if (num1 == 999) {
System.exit(0);
}
System.out.printf("Please enter the second number%n");
float num2 = keyboard.nextFloat();
if (num2 == 999) {
System.exit(0);
}
System.out.printf("Please enter the third number%n");
float num3 = keyboard.nextFloat();
if (num3 == 999) {
System.exit(0);
}
float sum = (num1 + num2 + num3); // calculates sum
float average = (num1 + num2 + num3) / 3; // calculates average
System.out.printf("Sum: %.0f %nAverage: %.2f", sum, average);
}
If we look at the way your code is currently written, the user will always be required to input 3 values due to not having any kind of conditionals attached.
I recommend using "System.exit" with conditionals to test if the value the user is inputting is 999. Try going from there to see what you can make happen.
Here is some documentation regarding "System.exit" to help you gain a better understanding of how it works.
Your for loop is looping only once so what I think you'll need to do is change some aspects of code
public class Calculator{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
float num1;
float num2;
float num3;
while (true){ //This will run forever until we break the loop
num1 = keyboard.nextFloat();
if (num1 == 999){
break;
}
num2 = keyboard.nextFloat ();
if (num2 == 999){
break;
}
num3 = keyboard.nextFloat ();
if (num3 == 999){
break;
}
System.out.println("The average is: " + (num1 +num2+num3)/3);
}
}
}

Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers

I'm getting the sum, average and the product. The real difficulty I am facing is with the smallest and the largest number.
I can do it with two numbers, but three numbers is not making any sense to me. Ask me if my question isn't clear or if its not making sense.
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
double largest
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
if (largest =num1 > num2 & num2 > num3)
System.out.println(sum);
System.out.println(average);
System.out.println(product);
System.out.println("The biggest number is " + largest);
}
}
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println("The product is " + product);
System.out.println("Largest of three integers is " + largest + " and the smallest is "+ smallest + ".");
}
}
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println("The product is " + product);
System.out.println("Largest of three integers is " + largest + " and the smallest is "+ smallest + ".");
}
}
import java.util.Scanner;
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.printf("The sum is %d%n " , sum);
System.out.printf("The average is %d%n " , average);
System.out.printf("The product is %d%n " , product);
System.out.printf("Largest of three integers is %d%n " , largest);
System.out.printf("Smallest of three integers is %d%n " , smallest);
}
}
Answer is given below:
Enter First Integer: 60
Enter Second Integer: 90
Enter Third Integer: 30
The sum is 180
The average is 60
The product is 162000
Largest of three integers is 90
Smallest of three integers is 30
...Program finished with exit code 0
Press ENTER to exit console.

Categories