Hi, how do u calculate the sum of the numbers in the array?

Thanks!

Recommended Answers

All 6 Replies

By adding each element in the array.

Ever heard of for loop?

how do u add the elements?

this is my program

import java.util.Scanner;

public class RandomNumbers
{
  public static void main(String [] args)
  {
	Scanner scan= new Scanner(System.in);
	
	int pos, value;
		
	System.out.println("Enter value of numbers to generate(0-100): ");
	value=scan.nextInt();
	
	while (value<0 || value>100)
	{
	System.out.println("Please enter value again (0-100): ");
	}	
		double[]random =new double[value];
				
		for (pos=0; pos< value; pos++)
		{
		random[pos]=Math.random()*1000.0 + 17.5;
		}
				
		System.out.println("Here are the random numbers: ");
		for (pos=0; pos<value; pos++)
		System.out.println(random[pos]);	
		
	}
}

i need to generate the sum of the random numbers.

//pseudocode
for (lowest array value to highest array value)
{
add array value to total sum
}

double Total = 0;
        int i; 
	for (i=0; i< 10; i++)
	{
		double rdNumber = Math.random() *1000.0 + 17.5;
		Total+=rdNumber;
	}
	System.out.println("Sum: " +Total );

Sample loop to accumulate total value.

  1. Global declaration of for loop variable "pos" will for sure create numerous issues in more complex application, so local declaration inside the for loop is better to use
  2. You missed the point of this forum, people should learn by trying things rather then be given solution for copying
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.