[ASSIGNMENT] : Integer or Decimal ?

Write a program to find out whether a number is of integer or decimal type. Example:- 12.33 is of decimal type 23.0 is of integer type ( .0 after any number has no value) so convert it to 23

5/17/2018 1:54:12 AM


17 Answers

New Answer




https://code.sololearn.com/csFI4mXJaFWc/?ref=app https://code.sololearn.com/cNn10rmtfMQn/?ref=app




This is my try using java https://code.sololearn.com/cwvTLlzmG4by/?ref=app



Simple solution https://code.sololearn.com/cO364VDkUXt7/?ref=app


Python oneliner: https://code.sololearn.com/cUK70VYzHROm/?ref=app


Another try (string version) https://code.sololearn.com/cP6XYeC0324A/?ref=app


import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner (System.in); double a=sc.nextDouble(); double b=a-(Math.floor(a)); if(b!=0.0){ System.out.println(""+a+"is a double value"); } else { System.out.println(""+a+"is an integer value"+((int)(Math.floor(a)))); } } } it's my try


Ruby https://code.sololearn.com/cjQa2CKbM2VU/#rb C++ https://code.sololearn.com/c3bJ1Wb45sbO/#cpp


# Python # number given as n integ, dec = tuple(str(n).split(".")) result = "integer" if all(i == "0" for i in dec) else "decimal" print(result)


My solution as pythoneliner: https://code.sololearn.com/ca69PqsUzqT3/?ref=app https://code.sololearn.com/cBbw6N6jOP70/?ref=app


@nitish kumar jha - Comparing floating point values to absolute numbers (such as 0.0) isn't a good plan. Some references: Very technical - https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html More accessable - http://www.floating-point-gui.de/errors/comparison/ Here's my C solution. Instead of dealing with floating point comparisons, I convert the characters after the decimal point to an integer and compare that to zero. This solution won't work if the decimal point is not proceeded with a valid number (such as ".0232"). https://code.sololearn.com/c902hiAmNjBE






C# shortest answer possible (I think) 60 Bytes namespace a{class b{static bool c(float d){return d%1f==0}}} EDIT: -1 byte due to 1f instead of 1.0 EDIT 2: yes, I like codegolf