-6

In C++ according to user input programmer able to find the final value is what double or int using this code.If 4/2 the answer is 2 Not 2.0. If 5/3 the answer is 1.66667 not 1.

float a;
float b;
cin >> a >> b;
float z = a / b;
cout << z << endl;

Simple thing is if user input is 2/4 the answer is 2 not 2.0. And if user input is 5/3 the answer is 1.66667 not 1 according to my c++ code. Can anybody do this same think in java Please?

Everybody know in calculator if we type 4/2 the answer is 2 not 2.0. And if we type 5/3 the answer is 1.66667 not 1. I just want to create something program like that i think now you understand me.

Muhammad Raza
  • 847
  • 1
  • 8
  • 22
  • Users gave me minus points. But not answer. I am new in java. Why? – Muhammad Raza Apr 30 '15 at 05:10
  • This code doesn't find out if the final value "is what double or int". The final value is a `float` because the division involves two `float`s. – juanchopanza Apr 30 '15 at 05:33
  • Sir, you need to try this code. If user input is 2/4 answer is 2 not 2.0. And if user enter 5/3 answer is 1.66667 not 1. – Muhammad Raza Apr 30 '15 at 05:35
  • It doesn't matter. The type is still `float`. If is to do with how `2.0` gets printed to the screen, not its type. – juanchopanza Apr 30 '15 at 05:43
  • @MuhammadRaza have you tried using ```int```for ```a``` and ```b```? To me it is not very clear what your question is. So I am just poking around. If you want to do division without rest in java you would define the parameter as ```int```. – Tobias Kremer Apr 30 '15 at 05:43
  • Simple thing is if user input is 2/4 the answer is 2 not 2.0. And if user input is 5/3 the answer is 1.66667 not 1 according to my c++ code. Can anybody do this same think in java Please? – Muhammad Raza Apr 30 '15 at 05:46
  • @MuhammadRaza I added an answer explaining where your understanding is wrong. – juanchopanza Apr 30 '15 at 05:52
  • Now you changed the meaning of the question completely. – juanchopanza Apr 30 '15 at 05:56
  • Sir i update my question again read it. Or second think. You know in calculator if we type 4/2 the answer is 2 not 2.0. And if we type 5/3 the answer is 1.66667 not 1. I just want to create something program like that i think now you understand me. – Muhammad Raza Apr 30 '15 at 05:57
  • Duplicate: http://stackoverflow.com/questions/14677448/how-to-cout-a-float-number-with-n-decimal-places – juanchopanza Apr 30 '15 at 05:57
  • Why can't you understand that it is just the way the number gets printed to the screen? I already explained that twice. Once in my answer. – juanchopanza Apr 30 '15 at 05:58

3 Answers3

2

The premise of the question is wrong because it is based on a misunderstanding. C++'s auto is a compile time construct. It cannot infer a type based on runtime information. In your example, the type of z in this code is float:

auto z = a/b;

This is because that is the type of the expression a/b, given that both a and b are of type float.

The issue is that std::cout is not printing any decimal places for a whole floating point number such as 2.0. That is all. But you can tell it to do so. For example,

std::cout << std::fixed << std::setprecision(4);

Here's a working example

#include <iostream>
#include <iomanip>

int main() {
    float a = 4;
    float b = 2;
    auto z = a / b;
    std::cout << std::fixed << std::setprecision(4);
    std::cout << z << std::endl;
}

output:

2.0000

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

If I understand your question, then you could use a Scanner to get float(s) from System.in (the equivalent to cin), and then either a float z or a double z and then call System.out.println() like

Scanner scanner = new Scanner(System.in);
float a = scanner.nextFloat();
float b = scanner.nextFloat();
float z = a / b;
System.out.println(z);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

A simple double z = a/b; should work

Example:

import java.util.Scanner;

public class Test {

public static void main(String[] arg){
    try{
        Scanner in = new Scanner(System.in); 
        System.out.printf("Enter first Value:  ");
        double a = in.nextDouble();

        System.out.printf("Enter second Value:  ");
        double b = in.nextDouble();

        System.out.println("Division result: "+a/b);
    } catch(Exception e){
        e.printStackTrace();
    }
  }
}