+17

[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

Donna

17 Answers

New Answer

+16

https://code.sololearn.com/W194YYBWreLI/?ref=app

+13

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

+13

https://code.sololearn.com/cVrn8XvBATA5/?ref=app

+11

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

+9

+6

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

+5

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

+4

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

+3

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

+3

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

+3

# 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)

+2

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

+1

@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

+1

x.is_integer()

+1

https://code.sololearn.com/w31bSJgzHUz0/?ref=app

0

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