Create a java program to display multiples of a number - java

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: ");
}
}

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) + " ");
}
}

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

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] + " ");
}
}
}

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++)

Related

Java sum from 1 to n with output showing work

I have a homework problem that I've almost finished, but I'm just stuck on how to output it correctly.
Write a program that reads a positive integer n and prints the sum of all integers from 1 to n as follows:
1+2+…+n=n(n+1)/2
The output does not contain any spaces.Example of input: 5 Corresponding output: 1+2+3+4+5=15
Here's my code:
import java.util.Scanner;
public class Homework2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for (int i = 0;i <= n; i++) {
sum = sum + i;
}
System.out.printf("the sum of %d is %d%n", n, sum);
}
}
What I have in the printf command is just a placeholder until I can figure out the correct output.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1;i <= n; i++) {
if (i != n) {
System.out.print(i + "+");
} else {
System.out.print(i + "=");
}
}
System.out.print(n*(n+1)/2);
}
the above will work. You must be strict to the expected output.
You need to print within the loop.
And you should start at 1, like the instructions say
for (int i = 1;i < n; i++) {
sum = sum + i;
System.out.printf("%d+", i);
}
sum += n;
System.out.printf("%d=%d\n", n, sum);
You can deal with the case of printing n and = separately like so:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Print the sum of all integers from 1 to n program");
System.out.println("=================================================");
System.out.print("Please enter n: ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1; i < n; i++) {
System.out.print(i + "+");
sum += i;
}
sum += n;
System.out.print(n + "=" + sum);
}
}
Try it here!
Example output:
Print the sum of all integers from 1 to n program
=================================================
Please enter n: 5
1+2+3+4+5=15

Print out array user entered

i am trying to printout the number the user entered, but i got an error. Here's my code:
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int size=0,score=0;
int [] a=new int[size];
int len=a.length;
do
{
System.out.print("Please enter a number between 1 to 5: ");
size=input.nextInt();
}
while ((size<1) || (size>5));
for (int i=1;i<=size;i++)
{
do
{
System.out.print("Enter your "+i+" score (1-100):");
score=input.nextInt();
}
while((score<1) || (score>100));
}
for (int i=1;i<=size;i++)
System.out.println(a[i]+ " ");
}
}
Here's my output and the error:
Please enter a number between 1 to 5: 2
Enter your 1 score (1-100):54
Enter your 2 score (1-100):64
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Week08.BigArray2.main(BigArray2.java:27)
There are four mistakes in your code
array initialized with zero size
user input not stored in the array
iterating from index 1 but array starts with 0
Scanner not closed
Scanner input = new Scanner(System.in);
int size = 0, score = 0;
do {
System.out.print("Please enter a number between 1 to 5: ");
size = input.nextInt();
} while ((size < 1) || (size > 5));
int[] a = new int[size]; //1
for (int i = 0; i < size; i++) {
do {
System.out.print("Enter your " + (i + 1) + " score (1-100):");
score = input.nextInt();
a[i] = score; //2
} while ((score < 1) || (score > 100));
}
for (int i = 0; i < size; i++) //3
System.out.print(a[i] + " ");
input.close(); //4
output
Please enter a number between 1 to 5: 4
Enter your 1 score (1-100):99
Enter your 2 score (1-100):98
Enter your 3 score (1-100):97
Enter your 4 score (1-100):96
99 98 97 96
Few issues with you code:
Array declared with zero size. In java, size of array is declared at the time of initialization and it can altered later.
You never stored score into an array. Hence array was empty.
Always start loop index with zero in java while iterating array.
Here it is the modified working program:
import java.util.Scanner;
public class ArrayPrint {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int size = 0, score = 0;
int[] a;
do {
System.out.print("Please enter a number between 1 to 5: ");
size = input.nextInt();
} while ((size < 1) || (size > 5));
//Allocating space to array
a = new int[size];
int len = a.length;
for (int i = 0; i <size; i++) {
do {
System.out.print("Enter your " + i + " score (1-100):");
score = input.nextInt();
a[i] = score;
} while ((score < 1) || (score > 100));
}
for (int i = 0; i <size; i++)
System.out.println(a[i] + " ");
}
}
Output
Please enter a number between 1 to 5: 2
Enter your 0 score (1-100):22
Enter your 1 score (1-100):11
22
11
Please have a look at your 4th line of code int [] a=new int[size];, size=0 at this time and you have initialized the integer array "a" with size 0 but you are trying to iterate array "a" at last with code for (int i=1;i<=size;i++)
System.out.println(a[i]+ " ");
} which is causing the array index out of bound exception.
Move below lines of code after first do while loop-:
int[] a = new int[size];
int len = a.length;
And now correct your last for loop as below-:
for (int i = 0; i < size; i++)
System.out.println(a[i] + " ");
}
Index of array starts at 0 and ends at size-1.

Not printing perfect numbers

Here is my code :-
package javaapplication;
import java.util.Scanner;
public class perfect02 {
public static void main(String args[]) {
int i = 1, sum = 0;
System.out.println("Enter maximum range : ");
Scanner kb = new Scanner(System.in);
int a = kb.nextInt();
System.out.println("Enter minimum range : ");
Scanner kb2 = new Scanner(System.in);
int b = kb2.nextInt();
System.out.println("perfect number in the given range are :");
for (int n = b; n <= a; n++) {
while (i < n) {
if (n % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == n)
System.out.println(+n + " ");
}
}
}
Why program is not printing perfect numbers ?
I have checked code many times but i am unable to find the solution .Please tell me what is going wrong in my code .Thanks in advance
Any help would be appreciated ....
Here, I looked into the perfect number generation and fixed it for you!
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
System.out.println("Enter minimum range : ");
int b = kb.nextInt();
System.out.println("Enter maximum range : ");
int a = kb.nextInt();
kb.close();
System.out.println("Perfect number in the given range are :");
for (int n = b; n <= a; n++) {
int sum = 0;
int i = 1;
while (i < n) {
if (n % i == 0)
sum = sum + i;
i++;
}
if (sum == n)
System.out.println(n + " is perfect");
}
}
You should've had the sum and i variables declared inside the for loop, so they would be reset for each number!

Factorial Issue

I am new at coding Java, but I need to write a program that is from integer 1 to n. The program would ask the user to enter a positive number and if it is not positive then it will ask for another number.
Once the positive integer is entered for n in the program, it shows how the n factorial is computed followed by the result.
So my program will show everything correct except the result, it is not multiplying all the numbers together. If someone can point me in the right direction on how to get this solved that would be great!
CODE:
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 30
As you can see for the result it should be 120 and not 30.
Just change that part
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
}
System.out.print(" is " + n * i);
for
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
Your last print was wrong as you simply multiplied n with i which is a simple multiplication and have nothing to do with factorial.
Your program is doing exactly one computation ( " is " + n * i) and this computation is not doing a factorial. You probably want to do the multiplication more than once - and with different numbers.
You are not doing the calculation properly. You just show the end result of n*i`.
In the below solution, I've taken an int fact = 1 and I'm multiplying it with the value of i inside the for loop and assigning back the result to factvariable. That's the core part. Thats how you get 1*2*3...*n = n!
import java.util.Scanner;
public class SomeArrayQuestion {
public static void main(String[] args) {
int n, i = 1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int fact = 1;
for (i = 1; i <= n; i++) {
System.out.print(i + "*");
fact = fact * i;
}
System.out.print(" is " + fact);
}
}
import java.util.Scanner;
public class Problem5 {
public static void main(String[] args){
int n, i =1;
Scanner kbd = new Scanner(System.in);
System.out.print("Enter n: ");
n = kbd.nextInt();
while (n <= 0) {
System.out.print("Enter n: ");
n = kbd.nextInt();
}
int result = 1;
for (i = 1; i <= n; i++){
System.out.print( i+ "*");
result *= i;
}
System.out.print(" is " + result);
}
}
Output:
Enter n: 5
1*2*3*4*5* is 120

Arrays in numerical order (without importing array.sort() option)

I would like to sort my two arrays in numerical order. So if I input something like: 96 2 1 42 49 5, it'll give me an output of: 1 2 42 49 96.
How would you do this using a for-loop? I'm trying to implement a basic for-loop to get my numbers to ascend numerically.
Here's the code:
public static void main(String[] args) {
System.out.println("Input up to '10' numbers for current array: ");
int[] array1 = new int[10];
int i;
int k;
Scanner scan = new Scanner(System.in);
for (i = 0; i < 10; i++) {
System.out.println("Input a number for " + (i + 1) + ": ");
int input = scan.nextInt();
if (input == -9000) {
break;
} else {
array1[i] = input;
}
}
System.out.println("\n" + "Array 1: ");
for (int j = 0; j < i; j++) {
System.out.println((j + 1) + ": " + array1[j]);
}
int[] array2 = new int[i];
System.out.println("\n" + "Array 2: ");
for (int j = 0; j < i; j++) {
array2[j] = array1[j];
System.out.println((j + 1) + ": " + array2[j]);
}
scan.close();
}
}
Using Bubble Sort. You will be required to use 2 for loops.
Complexity of bubble sort is O(n2)
package com.*;
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0 ; c < n ; c++)
array[c] = in.nextInt();
for (c = 0 ; c < (n - 1) ; c++) {
for (d = 0 ; d < n - c - 1 ; d++) {
if (array[d] > array[d + 1]) /* For descending order use < */
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0 ; c < n ; c++)
System.out.println(array[c]);
}
}
output
Input number of integers to sort
5
Enter 5 integers
96
2
1
42
49
Sorted list of numbers
1
2
42
49
96
There are numerous sorting algorithms that can address this task. Each algorithm makes a trade-offs between speed and complexity. The simplest sorting algorithm is the bubble sort. Bubble sort is inefficient, but totally reasonable to use if you are dealing with small arrays.

Categories

Resources