Jump to content

Need Help with Java HW!

Go to solution Solved by elpiop,

You're getting an overflow. The maximum value for an integer in Java is 2,147,483,647. Change product to a double or a long, which have higher maximum values. 

IDK why my product is not coming out correctly.

 

The problem is: 

Write a program which asks for two integers a and b and displays the following output: • The sum of all numbers between [a, b] • The product of all even numbers between [a, b] • The sum of all squares between [a, b] Input Validation: a and b cannot be negative. Must validate using loop validation. Challenge: Solve the problem using only one loop.

 

The output is suppose to be:

Enter the values for a and b: 7 25

Sum of all numbers between 7 and 25: 304

Product of all even numbers between 7 and 25: 40874803200

Sum of all squares between 7 and 25: 5434

 

My solution is:
 

import java.util.Scanner;

public class HW05P01 {
	public static void main(String[] args){
		Scanner input = new Scanner(System.in);
		
		
		System.out.print("Enter 2 integers: ");
		int a = input.nextInt();
		int b = input.nextInt();
		
		int sum = 0;
		int product = 1;
		int sum2 = 0;
		int answer = 0;
		
				
		do{
			sum += a;
			if(a%2 == 0){
				product *= a;
			}
			sum2 += Math.pow(a, 2);
			++a;
		}while (a > 0 && b > 0 && a <= b);
		
		
		
		System.out.println("The sum of all the digits: " + sum);
		System.out.println("The product of all the even digits: " + product);
		System.out.println("The sum of all the squares: " + sum2);

	}
	
	
}

which for some reason gives me the wrong output for product.

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/593400-need-help-with-java-hw/
Share on other sites

Link to post
Share on other sites

10 minutes ago, elpiop said:

You're getting an overflow. The maximum value for an integer in Java is 2,147,483,647. Change product to a double or a long. 

omg. I forgot about overflow's. Is there a way I can convert it back to standard form instead of scientific notation because i get 4.08748032E10 instead of 408748032.

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/593400-need-help-with-java-hw/#findComment-7717182
Share on other sites

Link to post
Share on other sites

1 minute ago, elpiop said:

You can use printf(). 

 

System.out.printf("The product of all the even digits: %.0f",product);

 

This formats the number to 0 decimal places.

 

Thanks! :)

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/593400-need-help-with-java-hw/#findComment-7717215
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×