Kyle
Kyle

Reputation: 57

Create a java program to display multiples of a number

I'm trying to Write the main method of a Java program that has the user enter two integers, i and n. If either integer is less than 2, output “Please enter numbers above 1.” Otherwise, output the n positive multiples of i, separated by spaces.

I'm close but can't figure out how to do display the multiples.

Here's what a sample run should look like:

Enter i: 4

Enter n: 6

6 multiples of 4 are: 8 12 16 20 24 28

import java.util.*;
public class HW5Problem3 {
   public static void main (String [] args) {

      int i = 0;
      int n = 0;

      Scanner input = new Scanner(System.in);

         System.out.print("Enter i: ");
         i = input.nextInt();
         System.out.print("Enter n: ");
         n = input.nextInt();

         if ((i <= 1) || (n <= 1)) {
            System.out.println("Please enter numbers above 1");
            System.exit(1);
         }

         System.out.print(n + " multiples of " + i + " are: ");

   }
}

Upvotes: 1

Views: 50461

Answers (4)

Cedric Achi
Cedric Achi

Reputation: 78

import java.util.Scanner;

public static void main(String[] args){
  Scanner input = new Scanner(System.in);

  int i = 0;
  int n = 0;

  //It's use to verify the inputs (i and n).
  do{

    System.out.print("Enter i :");
    i = input.nextInt();

    System.out.print("\nEnter n :");
    n = input.nextInt();

    if(i >= 1 || n <= 1){
      System.out.println("Please enter numbers above 1 \n");
    }    

  }while(i <= 1 || n <= 1);

  System.out.print(n + " multiples of " + i + " are: ");

  for (int counter = 0 ; counter < n ; counter++) {
    System.out.print(i*(2 + counter) + " ");
  }

}

Upvotes: 1

W&#252;rgspa&#223;
W&#252;rgspa&#223;

Reputation: 4820

n is the multiplier, i is the factor, right? In programming the multiplier is the loop maximum:

System.out.print(n + " multiples of " + i + " are: ");
for (int inc=1; inc<=n; inc++) {
    System.out.print(" " + i*inc);
}

This prints out: 4 8 12 16 20 24

If you really want to have this as output: 8 12 16 20 24 28 copy/paste this line:

for (int inc=2; inc<=(n+1); inc++)

Upvotes: 1

sherl
sherl

Reputation: 21

U can use following method in that class

   public static void mult(int i,int n){
   int[] arr=new int[n];
   int count=2;
   for(int x=0;x<n;x++){
       arr[x]=i*count++;
   }
   for(int y=0;y<arr.length;y++){
   System.out.print(arr[y]+" ");

}

and now your final code looks like

import java.util.*;

public class HW5Problem3 {
    private int i = 0;
    private int n = 0;

    public static void main(String[] args) {

        int i = 0;
        int n = 0;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter i: ");
        i = input.nextInt();
        System.out.print("Enter n: ");
        n = input.nextInt();

        if ((i <= 1) || (n <= 1)) {
            System.out.println("Please enter numbers above 1");
            System.exit(1);
        } else {
            System.out.print(n + " multiples of " + i + " are: ");
            mult(i, n);
        }

    }

    public static void mult(int i, int n) {
        int[] arr = new int[n];
        int count = 2;
        for (int x = 0; x < n; x++) {
            arr[x] = i * count++;
        }
        for (int y = 0; y < arr.length; y++) {
            System.out.print(arr[y] + " ");
        }
    }
}

Upvotes: 1

Paul Ostrowski
Paul Ostrowski

Reputation: 1966

You'll need to create a loop (for loop or while loop) to iterate from 2 to n+1, and multiply i by your loop variable, outputting each value inside the loop

Upvotes: 1

Related Questions