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.
6 replies to this topic
#1
Posted 05 November 2010 - 07:58 PM
|
|
|
#2
Posted 05 November 2010 - 09:07 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 05 November 2010 - 09:13 PM
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:
Thanks.
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
Posted 05 November 2010 - 09:22 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#5
Posted 05 November 2010 - 09:37 PM
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
Posted 05 November 2010 - 11:08 PM
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.
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#7
Posted 06 November 2010 - 12:18 AM
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


Sign In
Create Account


Back to top









