This question already has answers here:
How to remove decimal values from a value of type 'double' in Java
(21 answers)
Closed 6 years ago.
this is my code:
public static void main(String[] args) {
System.out.println("N:");
Scanner in = new Scanner(System.in);
float n = in.nextFloat();
float a,b,c,d;
a = n/1000;
b= n/100%10;
c = n/10%10;
d = n%10;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
and I want to remove those numbers. I'm newbie :) help me pls
Your code seems to try to find the digits of a 4 digit integer number.
Therefore, it makes no sense to use float variables. Use int.
int n = in.nextInt();
int a,b,c,d;
a = n/1000;
b= n/100%10;
c = n/10%10;
d = n%10;
Math.Round(number,2) 2 decimals in this case.
Or, you want the number without decimals? If yes, use int
Related
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 2 years ago.
While I'm using truncating in this method, it is somewhere wrong as in picture
public static List<Double> interp(DoubleUnaryOperator f, double l, double u, int n) {
double d = (u - l)/n;
List<Double> result = new ArrayList<>(n);
double net = f.applyAsDouble(l);
result.add(net);
int i = 0;
while(i+1 < n){
result.add(Math.floor(f.applyAsDouble(l + d)*100)/100);
l += d;
i++;
}
return result;
}
Who know how to truncate correclty? P.S. its important for the number be in two decimal places.
Use this :
new DecimalFormat("#.##").format(dblVar);
df.setRoundingMode(RoundingMode.DOWN);
s = df.format(d);
and import the class java.text.DecimalFormat.
This question already has answers here:
Sum of the digits of the number 2^1000 [closed]
(11 answers)
Closed 3 years ago.
import java.math.*;
public class PowerDigitSum {
public static void main(String[] args) {
double[] digits ;
digits = new double[302];
double i = Math.pow(2, 1000);
double c = 301;
double c1 = 0;
double d = 0;
while(c>=0) {
c1 = Math.pow(10, c);
d = Math.floor(i/c1);
i = i - d*c1;
digits[(int)c] = (int)d;
c = c-1;
}
double sum = 0;
c = 0;
while (c<302) {
sum = sum+digits[(int)c];
c= c+1;
}
System.out.println(sum);
}
}
The output is 1281 but that's not correct according to projecteuler. What am I doing wrong?
You can't do correct math with double values that large due to their limited nature.
You could probably fix your code with BigDecimal, but it is much easier using BigInteger. Hint: it has a pow method, and you can work out the digit sum starting from toString.
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;
}
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
/**
* Write a description of class GUI here.
*
* #author (your name)
* #version (a version number or a date)
*/
import java.util.*;
public class GUI
{
// instance variables - replace the example below with your own
public static void main(String [] args){
Scanner r = new Scanner(System.in);
int x;
int y;
int z;
System.out.println("x");
x = r.nextInt();
System.out.println("y");
y = r.nextInt();
System.out.println("z");
z = r.nextInt();
double t = (x+y+z)/3;
System.out.println("result " + t);
}
}
Hello, above is my code.
I purposely made it int x,y,z to test the program.
When I input for example (when running the program) :$x = 1, 1, 3$ it rounds the answer always! Why is this?
This is an example of Java's integer division, which must always return another integer. It truncates any decimal result. This occurs even though the result is assigned to a double.
Use a double literal when dividing to force floating-point division.
double t = (x+y+z)/3.0;
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I have tried this in Javascript and have gotten my answers, but the answer I need must be more exact. I am trying to divide 1 by every number between 2 and 1000, and simply print them.
public static void main(String [] args) {
for (int i=2;i<=1000;i++){
double g = (1/i);
System.out.println(g); // print "1/1,2,3,4.....1000"
}
}
I haven't done Java in a while, so I forget my correct variable names.
Since both 1 and i are integers, integer division is being used. Either 1 or i need to be double in the 1/i section of your code so that integer division is not used. You can do something like 1.0/i or 1/((double) i) to ensure that float division is used instead.
replace 1 by 1.0D that will result into double
try this
public static void main ( String args[] ) {
for (int i=2;i<=1000;i++){
double g = (1.0/i);
System.out.println("1/"+ig);
}
output:
0.5
0.3333333333333333
0.25
0.2
0.16666666666666666
0.14285714285714285
0.125
.
.
.
.
I would do something like this (note you can have as many digits of precision as you like) utilizing BigDecimal.
public static void main(String[] args) {
java.math.BigDecimal ONE = java.math.BigDecimal.ONE;
// 50 digits of precision.
java.math.MathContext mc = new java.math.MathContext(50);
for (int i = 2; i <= 1000; i++) {
java.math.BigDecimal divisor = new java.math.BigDecimal(i);
java.math.BigDecimal result = ONE.divide(divisor, mc);
result.round(mc);
System.out.printf("%d/%d = %s\n", 1, i,
result.toString());
}
}