This question already has answers here:
Separating the Digits in an Integer - exercise from Deitel's Java book
(11 answers)
Closed 7 years ago.
Let's say you have an integer '75'. Normally, in your head, you can add the '7' with the '5' in order to get '12'. So you split the number '75' into two different numbers 7 and 5 then add them together. That leads to my question, how can you perform that in java? Is there a Math method that does it for you?
You can use plain maths
int i = 75;
int a = i / 10; // 7
int b = i % 10; // 5
int c = a + b; // 12
You can use some code like:
int num=75;
int sum_digits=0;
while(num>0){
int digit = num%10;
num /= 10;
sum_digits += digit;
}
Related
This question already has answers here:
How to sum digits of an integer in java?
(22 answers)
Closed 1 year ago.
Suppose
int n = 5
I want to find out the sum of n number. For example:- if n is given by user as 5 then in number array from 1 to 30 we have sum of n number is 14 and 23. Which is int number = 1+4= 5 and 2+3 = 5 that is number == n.
To reduce double digits i.e., if user put 15 then:-
reduce(int doubleDigit){
return (doubleDigit-1) %9 +1;
So to reduce 15 i.e., 1+5=6
But to calculate the sum of 6, how to find? What I want as a result is that when ans is 6 then the output should be 6, 15 (1+5=6) and 24 (2+4=6) which is sum of two digits is equal to ans in the range between 1 to 30.
So do I need to reverse the reduce function or is their any method to solve.
Small code hint will be very helpful.
There is a semi-tautological function for doing this:
public int sumDigits (int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 3 years ago.
I want to calculate the sum of a geometric series. ie: 1 , 5 , 25 , 125 , etc
I try to use the math formula to calculate it: a(r^n -1)/(r-1)
My code:
int a = 1;
int r = 5;
int deno = r -1;
int n = 3
int rn = r^n -1 ;
int total = a * rn / deno;
Apparently there is wrong with the code and only some values like the example I give works. I do not know why QAQ
I think the problem is the symbol ^
can anyone explain what ^ does in java? Appreciate it
Atleast in Java 7, the symbol ^ does not mean power.
Try this, you would also like to put a condition where r>1 or r<1. Both have different formuals.
int a = 1;
int r = 5;
int deno = r -1;
int n = 3;
double sum=a*(Math.pow(r, n)-1)/deno;
This question already has answers here:
Error with division using double type in Java
(5 answers)
Integer division: How do you produce a double?
(11 answers)
Closed 3 years ago.
Consider the following code snippet:
int[] dataSet = {1,2,3,4};
int total = 0;
for(int temp : dataSet){
total += temp;
}
double mean = (total / dataSet.length);
System.out.println(mean);
I expected this to output 2.5, as that is the mean of 1,2,3,4. Instead, it printed 2.0. Why is this and how can I fix it?
Java 7 or more if you use a int / int you have a int
Try to cast one number to double.
double res = 1 / (double) 2;
This question already has answers here:
How Does Modulus Divison Work
(19 answers)
Closed 5 years ago.
package PracticePackage;
public class whileLoop {
public static void main(String[] args) {
int i=1;
System.out.println("Quotient "+i/2);
System.out.println("Remainder "+i%2);
}
}
this is the fomula that Java uses to yield the remainder of its operands:
(a/b)*b+(a%b)
where a is the dividend and b is the divisor.
so in your case it's like:
int i = 1;
int b = 2;
int result = (i / b) * b + (i % b);
hence the result 1 rather than 0
1/2 = 0.5
you defined i as int
Integral division in java takes floor of the answer if the answer is a real number so 1/2 becomes 0, making 1%2 equal to 1
I hope that explains.
because integers are not real numbers so you get 1 as answer and the real part of remainder is ignored since you defined i as an integer
This question already has answers here:
How to write a recursive method to return the sum of digits in an int?
(13 answers)
Closed 7 years ago.
So I have to write recursive method that adds up value of digits within a certain number. For example, digitSum (1234) returns 10 (which is the sum 1+2+3+4).
So far I have this:
public static int digitSum (int n) {
if(n<10) { return n ;} //basecase
else return !!! ;
}
What should I add in the !!! part, thank you
Just write it in plain english: when you have 1234, initial step should be to do sum(123) + 4
When you convert it to code:
public static int digitSum (int n) {
if(n<10) {
return n
}
else
return n%10 + digitSum(n/10);
}
n%10 gives you the last digit, n/10 gets you the remaining part.
When you have n=1234, n%10 = 4 and n/10 = 123. So according to your plain english sum(123) + 4 it should be digitSum(n/10) + (n%10)
Homework?
1234 remainder 10 is 4, 1234 / 10 is 123 you schould return digitSum(123) + 4