Program to Check if Given Number is Integer or Float

Program to Check if Given Number is Integer or Float

  • Write a program to Check if Given Number is Integer or Float in C
  • Write a program to Check if Given Number is Integer or Float in C++
  • Write a program to Check if Given Number is Integer or Float in Python
  • Write a program to Check if Given Number is Integer or Float in PHP
  • Write a program to Check if Given Number is Integer or Float in Java
  • Write a program to Check if Given Number is Integer or Float in Java Script
  • Write a program to Check if Given Number is Integer or Float in C#

Explanation:

Logic to Check if Given Number is Integer or Float

  1. Input the number from the user.
  2. Check if the number is an integer:
    • If the number has no fractional part, it is an integer.
    • If the number has a fractional part, it is a float.
  3. Output whether the number is an integer or a float.

How to Identify Integer vs. Float:

  • Integer: A number without a decimal point (e.g., 5, -3, 100).
  • Float: A number with a decimal point (e.g., 5.5, -3.14, 0.2).

Program to Check if Given Number is Integer or Float

#include <stdio.h>
#include <math.h>

int main() {
    float num;
    printf("Enter a number: ");
    scanf("%f", &num);

    if (num == (int)num) {
        printf("The number is an integer.\n");
    } else {
        printf("The number is a float.\n");
    }

    return 0;
}

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    float num;
    cout << "Enter a number: ";
    cin >> num;

    if (num == (int)num) {
        cout << "The number is an integer." << endl;
    } else {
        cout << "The number is a float." << endl;
    }

    return 0;
}

num = input("Enter a number: ")

try:
    num_float = float(num)
    if num_float == int(num_float):
        print("The number is an integer.")
    else:
        print("The number is a float.")
except ValueError:
    print("Invalid input! Please enter a valid number.")

<?php
$num = readline("Enter a number: ");

if (is_numeric($num)) {
    if ((float)$num == (int)$num) {
        echo "The number is an integer.\n";
    } else {
        echo "The number is a float.\n";
    }
} else {
    echo "Invalid input! Please enter a valid number.\n";
}
?>

import java.util.Scanner;

public class NumberType {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        String input = scanner.nextLine();

        try {
            double num = Double.parseDouble(input);

            if (num == (int)num) {
                System.out.println("The number is an integer.");
            } else {
                System.out.println("The number is a float.");
            }
        } catch (NumberFormatException e) {
            System.out.println("Invalid input! Please enter a valid number.");
        }

        scanner.close();
    }
}

let input = prompt("Enter a number:");

if (!isNaN(input)) {
    let num = parseFloat(input);
    if (num === Math.floor(num)) {
        console.log("The number is an integer.");
    } else {
        console.log("The number is a float.");
    }
} else {
    console.log("Invalid input! Please enter a valid number.");
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter a number: ");
        string input = Console.ReadLine();

        if (float.TryParse(input, out float num)) {
            if (num == (int)num) {
                Console.WriteLine("The number is an integer.");
            } else {
                Console.WriteLine("The number is a float.");
            }
        } else {
            Console.WriteLine("Invalid input! Please enter a valid number.");
        }
    }
}

List of All Programs