I am trying to calculate the sum of the odd numbers between two user inputted numbers.
This is the code I have so far:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("#1: ");
int num1 = s.nextInt();
System.out.print("#2: ");
int num2 = s.nextInt();
int sum = 0;
for (int i = num1; i <= num2; i += 2) {
sum = sum + i;
}
System.out.print(sum);
}
}
When I input 3 and 11, it outputs 35, which is correct.
However, with 4 and 20, it outputs 108, which is not correct. It should instead be 96.
Where have I gone wrong in the code?
You need to check if the first number is even or odd first, because if it is even, it is going to sum the even numbers instead of odd ones since you are increasing "i" by 2 after every iteration. Try adding the line below before the for loop.
if(num1%2==0){num1++);
Explanation
Your loop sums the even numbers if you start with an even number.
Check your code:
for (int i = num1; i <= num2; i += 2)
Assume num1 is even, e.g. 4. Then your loop starts with i = 4. And then you continue with += 2, so you end up with 6, 8, 10, ..., i.e. the even numbers.
Solution
You can simply fix it by patching num1 before your loop.
// Put this before your loop
if (num1 % 2 == 0) { // checks if num1 is even
num1++;
}
This way you will start with 5 then, and then get 7, 9, 11, ....
To sum a range of numbers:
Take the average of the first number and the last number, and multiply by the number of numbers.
But, first you need to make sure you lower and upper bounds are odd numbers. This is actually what is the problem with the code in the question.
Once that is done, calculate the average number, and the number of numbers:
public static long sumOddNumbers(int min, int max) {
long minOdd = (min % 2 == 1 ? min : min + 1); // Round up to odd number
long maxOdd = (max % 2 == 1 ? max : max - 1); // Round down to odd number
if (minOdd > maxOdd)
return 0;
// average = (minOdd + maxOdd) / 2
// count = (maxOdd - minOdd) / 2 + 1
// sum = count * average
return ((maxOdd - minOdd) / 2 + 1) * (minOdd + maxOdd) / 2;
}
Test
System.out.println(sumOddNumbers(3, 11)); // prints: 35
System.out.println(sumOddNumbers(4, 20)); // prints: 96
This can be done easily with a mathematical formula. But based on your question I presume you want to use a loop so here goes.
An odd number has the low order bit set to 1.
If the number is already odd, resetting that bit has no effect so it is still odd.
But setting that bit for an even number makes it the next higher odd number.
So use the bitwise OR operator.
3 | 1 = 3
4 | 1 = 5
And do the following:
int start = 4;
int end = 20;
int sum = 0;
for (int i = (start | 1); i <= end; i += 2) {
sum += i;
}
System.out.println(sum);
If you want to use streams in Java 8+ you can do it this way.
int sum = IntStream.rangeClosed(start, end).filter(i -> i & 1 == 1).sum();
And the formula I was talking about is the following, derived using simple series summation.
For ints, start and end
start |= 1; // ensure start is next odd number
end = (end | 1) - 2;// ensure end is last odd - 2
int sum = ((start + end) * ((end - start) / 2 + 1)) / 2;
If the two numbers are natural numbers, you can apply the formula:
Sum of odd natural numbers from m to n = sum of natural odd numbers up to n - sum of natural odd numbers up to m
= (n/2)^2 - (m/2)^2
where n is an even number or the next even number in case it is an odd number
In the program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("#1: ");
int num1 = Math.abs(s.nextInt());
System.out.print("#2: ");
int num2 = Math.abs(s.nextInt());
System.out.println("Sum of natural odd numbers from "+num1+" to "+num2+" = "+sumNaturalOddOfRange(num1,num2));
num1 = 5;
num2 = 15;
System.out.println("Sum of natural odd numbers from "+num1+" to "+num2+" = "+sumNaturalOddOfRange(num1,num2));
num1 = 5;
num2 = 16;
System.out.println("Sum of natural odd numbers from "+num1+" to "+num2+" = "+sumNaturalOddOfRange(num1,num2));
num1 = 6;
num2 = 15;
System.out.println("Sum of natural odd numbers from "+num1+" to "+num2+" = "+sumNaturalOddOfRange(num1,num2));
}
static int sumNaturalOddOfRange(int num1, int num2) {
if(num2%2==1)
num2++;
return (int)(Math.pow(num2 / 2, 2) - Math.pow(num1 / 2, 2));
}
}
A sample run:
#1: 10
#2: 20
Sum of natural odd numbers from 10 to 20 = 75
Sum of natural odd numbers from 5 to 15 = 60
Sum of natural odd numbers from 5 to 16 = 60
Sum of natural odd numbers from 6 to 15 = 55
**Here is the answer**
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("#1: ");
int num1 = s.nextInt();
System.out.print("#2: ");
int num2 = s.nextInt();
int sum = 0;
if (num1%2==0) {
num1++;
}
for (int i = num1; i <= num2; i++) {
sum +=i;
}
System.out.print(sum);
}
Related
import java.util.Scanner;
class FloatDigit {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double n = sc.nextDouble();
int x = (int) n;
int count = 0;
do {
count++;
x = x / 10;
} while (x != 0);
System.out.println("Before Decimal Digits: " + count);
//it gets stuck from here only
do {
count++;
n = n * 10;
} while (n != (int) n);
System.out.println(count + " total digits present in there in number.");
}
}
This goes in an infinite loop for the value: 58.2354/58.234. It is working fine with other values too and longer values too.
If some debug logging is added to the second loop, it can be seen, that when multiplying a double number by 10, there is a tiny error which does not allow the comparison n == (int) n ever become true.
It is actually a known issue that floating-point arithmetics has a certain computation error, so it should be taken into account when comparing the double n to its counterpart with the decimal point shifted right:
do {
count++;
n = n * 10;
System.out.println(count + " - " + n);
} while (Math.abs(n - (int) n) > 1E-7);
System.out.println(count + " total digits present in the number.");
Output:
58.234
Before Decimal Digits: 2
3 - 582.34
4 - 5823.400000000001
5 - 58234.00000000001
5 total digits present in the number.
I have a program that computes amicable pairs of numbers up to a certain limit and prints them out to Stdout. FYI - two numbers are amicable if each is equal to the sum of the proper divisors of the other (for example, 220 and 284).
My program works fine for the first 8 or so pairs, but the problem is when the limit gets to a very high number e.g 5 million it becomes very slow. Is there a way to optimize this so that it computes much faster?
Here is my code
public class Amicable{
private static int num1, num2, limit = 5000000;
public static void main(String[] args){
for(int i = 1; i < limit;i++){
num1= sumOfDivisors(i);
num2 = sumOfDivisors(i)
if(num1 == num2){
System.out.println(num2 + " " + num1);
}
}
}
}
private static int sumOfDivisors(int n){
int sum = 0;
for(int i=1;i<=n/2;i++){
if(n%i==0){
sum =sum+i;
}
}
return sum;
}
}
You should make the innermost loop efficient.
static int sumOfDivisors(int n) {
int sum = 1;
int max = (int) Math.sqrt(n);
if (n > 1 && max * max == n) // in case of n is perfect square number
sum -= max;
for (int i = 2; i <= max; ++i)
if (n % i == 0)
sum += i + n / i;
return sum;
}
If n = 220, it is divisible by 2, so you can add 2 and 110 to the sum at the same time. However, if n is a perfect square number, for example n = 100, it is divisible by 10, but if 10 and 10 are added, they will duplicate, so subtract it in advance.
output for limit =2000000:
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
63020 76084
66928 66992
.....
1468324 1749212
1511930 1598470
1669910 2062570
1798875 1870245
It took 7 seconds on my PC.
You could change the statement of "if(i<num1)" to "if(i<num1 || num1<limit)".
This is a more efficient and faster approach.
Inside sumOfDivisors:
Start iteration from 2. (no a big deal) and add 1 during return time with sum as 1 is also a divisor.
Modify loop termination logic, i <= Math.sqrt(n).
For any number ‘num’ all its divisors are always less than and equal to ‘num/2’ and all prime factors are always less than and equal to sqrt(num).
If n%i == 0, then check, both divisors are are equal or not. If equal, take one if not equal then take both one.
private static int sumOfDivisors(int n) {
int sum = 0;
int sqrt = Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if (n % i == 0) {
if(i == (n/i))
sum = sum + i;
else
sum += (i+ n/i);
}
}
return sum+1;
}
Here is the coding question for which I am trying to solve
Write a program that reads two numbers aa and bb from the keyboard and calculates and outputs to the console the arithmetic average of all numbers from the interval [a; b][a;b], which are divisible by 33.
Sample Input 1:
-5
12
Sample Output 1:
4.5
Here is my Code:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double average = 0;
int a = scanner.nextInt();
int b = scanner.nextInt();
Problem: Throws Arithmetic Exception
What is the problem?
Your variable i loops from -5 to 12. Then, you divide (a + b) / i (line 14).
0 is between -5 and 12. Thus, you will eventually divide by zero.
(I assume that line 13 is supposed to prevent this, but the way you have written it, it does not. In fact, 0 is among the very few values of i for which line 14 will actually be executed.)
According to your sample input and sample output, you need to add all the numbers in the range that are divisible by 3 and divide that total by how many different numbers are in the range.
Between -5 and 12, the numbers that are divisible by 3 are:
-3, 0, 3, 6, 9, 12
When you add them all together, you get 27.
And there are 6 different numbers altogether.
So the average is 27 divided by 6 which gives 4.5
Now for the code.
import java.util.Scanner;
public class RangeAvg {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter lower bound: ");
int a = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter upper bound: ");
int b = scanner.nextInt();
int lower = Math.min(a, b);
int upper = Math.max(a, b);
int total = 0;
int count = 0;
for (int i = lower; i <= upper; i++) {
if (i % 3 == 0) {
System.out.println(i);
total += i;
count++;
}
}
System.out.println("total = " + total);
System.out.println("count = " + count);
if (count > 0) {
double average = (double) total / count;
System.out.println("average = " + average);
}
else {
System.out.printf("No numbers divisible by 3 between %d and %d%n", lower, upper);
}
}
}
Below is a sample run:
Enter lower bound: -5
Enter upper bound: 12
-3
0
3
6
9
12
total = 27
count = 6
average = 4.5
I'm new at this, and having some trouble with my code. The thing is, it works, but i keep gettingthis "Exception in thread "main" java.util.NoSuchElementException" when i check my exercise in jet brains. But in my IDE it does work. This is the exercise =
"Write a program that reads two numbers a
a
and b
b
from the keyboard and calculates and outputs to the console the arithmetic average of all numbers from the interval [a;b]
[
a
;
b
]
, which are divisible by 3
3
.
In the example below, the arithmetic average is calculated for the numbers on the interval [−5;12]
[
−
5
;
12
]
. Total numbers divisible by 3
3
on this interval 6
6
: −3,0,3,6,9,12
−
3
,
0
,
3
,
6
,
9
,
12
. Their arithmetic average equals to 4.5"
The program works and returns the 4.5.
This is my code:
int a = in.nextInt();
int b = in.nextInt();
double divisible = 0;
int sum = 0;
double num =0;
for (double i = a; i<b; i ++) {
num = in.nextInt();
if (num%3==0) {
divisible = divisible +1;
sum += num;
}
}
double result = sum/divisible;
System.out.println(result);
Hope u can help me because I really wanna improve my skills and keep learning. Thank you for your time.
Try:
int a = sc.nextInt();
int b = sc.nextInt();
double count = 0;
double sum = 0;
for (double i=a; i<=b; i++) {
// Check if number is divisible by 3
if (i % 3 == 0) {
// Count total numbers
count += 1;
// Calculate sum
sum += i;
}
}
double result = sum / count;
System.out.println("Average: " + result);
Output:
Explanation:
Removed num = in.nextInt(); as i in for (double i=a; i<=b; i++) will get all numbers between a and b.
Replaced sum += num; with sum += i;.
The goal of my specific project is to write a program that will prompt the user to input two integers. The program will read the two integers and decide whether they are prime or not. if they are not the program will list the factors, otherwise it will simply print "prime" and ask the user repeatedly for two integers. Also, the program should print factors of all the numbers between the two given integers as well as the integers themselves. It will also give the average value of the prime numbers.
Goal is to make the final result look like this (assuming the two integers are 6 and 11):
Please enter two integers: 6 11
6: 2 3
7: Prime
8: 2 4
9: 3
10: 2 5
11: Prime
There are three prime numbers
The average value of the prime numbers is 9.00
Please enter two integers:
So here is my code:
import java.util.Scanner;
public class Prime {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int r1, r2, i, c = 0;
System.out.println("Please enter two integers : ");
int num1 = input.nextInt();
int num2 = input.nextInt();
while (num1 > 0 && num2 > 0)
{
for (i = 2; i < num1; i++) {
r1 = num1 % i;
r2 = num2 % i;
if (r1 == 0 && r2 == 0)
System.out.println("Prime");
{
System.out.println(i+ "\t");
c++;
}
}
if (c == 0)
System.out.println("Prime");
System.out.print("Please enter two integers : ");
num1 = input.nextInt();
num2 = input.nextInt();
}
}}
And this is my output when inputting 6 and 11:
Please enter two integers :
6 11
2
3
4
5
Please enter two integers :
Now i have no idea where i went wrong but i feel i must be heading somewhat in the right direction. If both inputs are prime it will print prime. If one is prime and one is not it will do what i posted above.
Any and all help is appreciated. Thank you.
Well, I'm just glancing over briefly, but your problem lies within the for loop. You start i at 2, which is sensible for checking for factors. You then check, simultaneously, if both num1 and num2 are evenly divisible by i (at this point, 2). If they are, you print "Prime". Then you iterate and do it again. Think about that carefully: how closely does it match with what you think/thought you were doing?
If I had to guess, you are missing an else line after
if (r1 == 0 && r2 == 0)
System.out.println("Prime");
{
And also the condition for the "if" should probably be negated: if (!(r1 == 0 || r2 == 0)). That should at the very least be enough to get you going in the right direction.
Good luck!
A much shorter way to do.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static List<Integer> primeFactors(int numbers) {
int n = numbers;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n / i; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
if (n > 1) {
factors.add(n);
}
return factors;
}
public static void main(String[] args)
{
System.out.println("Please enter two integers : ");
Scanner input = new Scanner(System.in);
int num1 = input.nextInt();
int num2 = input.nextInt();
List<Integer> primenos = new ArrayList<Integer>();
List<Integer> result;
for(int i=num1; i<=num2; i++)
{
result = primeFactors(i);
System.out.print(i +":");
if(result.size()==1 && result.get(0)==i)
{
System.out.println(" prime");
primenos.add(i);
}
else
{
for (Integer j : result) {
System.out.print(" "+j);
}
}
System.out.println();
}
System.out.println("There are "+primenos.size()+" prime numbers");
int total = 0;
for(Integer j : primenos)
{
total+=j;
}
System.out.println("The average value of the prime numbers is "+total/primenos.size());
}
}
This is just one way of doing it.
You could find hundreds of algorithms if you google. Find one and modify to your needs.