Jump to content

Help needed for Java Programming

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
How to design a program that will read a series of integers from the user and store them in an array called num[]. The program should then calculate and display the product of all integers entered by the user.

Sample input-output :
Enter total numbers to read : 5
Key in 5 numbers :
3
6
5
4
7
The product : 2520

Can any please help me out.

Thanks.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
I will not write the java code, but I will show you how it is done in pseudocode:
//get numbers to read
print("Enter total numbers to read: ");

int keys;
keys = input();

//array to hold integers
int num[keys];

//key in the numbers
print("Key in " + keys + " numbers:\n");
for(int i = 0; i <= keys - 1; i++) {
   num[i] = input();
}

//get product of results
int result = num[0]; //first number
for(i = 1; i <= keys - 1; i++) {
    result = result * num[i];
}

print("The product: " + result + "\n");

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
I've done the java code for tat as shown below, but there is something wrong in the code where i couldn't identify. Can you please check and tell me if there is any mistake.

Code:
import java.util.*;

public class ProductNumber {

public static void main(String args[]) {

 Scanner keyboard = new Scanner(System.in);

 int numInteger;

 int[] num;

 int totalProduct;

 System.out.println("Enter total number to read:");
 numInteger = keyboard.nextInt();

 System.out.println();
 System.out.println("Key in " + numInteger + "numbers:");
 System.out.println();

 num = new int[numIntegers]:

 for (int index = 0; index < num. length; index++) {

 System.out.println("Enter number" + "(index+1)" + " :");
 num[index] = keyboard.nextInt();
 System.out.println();
}

for (int index = 0; index < num.length; index++) {

 totalProduct *=num[index];

 System.out.println(totalProduct);

}

}


Thanks.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You made a few mistakes. You put "num = new int[numIntegers]:" when it should be "num = new int[numInteger];", no "s" and a semicolon at the end. You needed to initialize "totalProduct" as you are multiplying it when it was not initialized, you as well forgot a bracket at the end, try to keep your code indented so it is easier. Try this code, I cleaned it up for you, but you may need to work on it more:
import java.util.*;

public class ProductNumber {
    
    public static void main(String args[]) {
        
        Scanner keyboard = new Scanner(System.in);
        
        int numInteger;
        
        int[] num;
        
        int totalProduct = 0;
        
        System.out.println("Enter total number to read:");
        numInteger = keyboard.nextInt();
        
        System.out.println();
        System.out.println("Key in " + numInteger + "numbers:");
        System.out.println();
        
        num = new int[numInteger];
        
        for (int index = 0; index < num. length; index++) {
            
            System.out.println("Enter number" + "(index+1)" + " :");
            num[index] = keyboard.nextInt();
            System.out.println();
        }
        
        for (int index = 0; index < num.length; index++) {
            
            totalProduct *=num[index];
            
            System.out.println(totalProduct);
            
        }
        
    }
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
but how is that possible to calculate the product of integers if the total product is initialized to 0?? the product of integers would then result in 0 rite.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

Pwincy Priya said:

but how is that possible to calculate the product of integers if the total product is initialized to 0?? the product of integers would then result in 0 rite.
You can initialize it to 1 in your original code, because if the array contains "5,4,6,7,3" it will be "1 * 5 * 4 * 6 * 7 * 4"

I found another syntax typo, in your first loop you used "num. length;" with a space in it, I think.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
Pwincy Priya

Pwincy Priya

    Newbie

  • Members
  • PipPip
  • 22 posts
I can do it already. Thank you for your kind help.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users