how to check if the number is not an integer in C without using isdigit?

gyau

I've been writing a code of the Fibonacci sequence (iteratively). It scans a number n, and it outputs F(n) until I input -1. The problem is that first, I need to check if the number is not an integer. If it is not an integer, it should output "error".

#include<stdio.h>
#include<ctype.h>

int main(){
    float n=0;
    int x=0,ver=0,Fn=0,last1=0,last2=0;

    scanf("%f",&n);
    ver=n;
    while(n!=-1){
        if(n-ver!=0 || !isdigit(ver)){
            printf("Error\n");
        }
        else if(n==1 || n==2){
            printf("1\n");
        } else{
            last1=1;
            last2=1;
            for(x=3;x<=n;x++){
                Fn=last1+last2;
                last1=last2;
                last2=Fn;
            }
            printf("%d\n",Fn);
        }
        getchar();
        scanf("%f",&n);
        ver=n;
    }
return 0;
}

I've tried with isdigit and !isdigit and I still get wrong outputs. It should output error when I input things like .11$, 1.- , 1.23,KDhf, etc.

P.P

For finding fibonacci numbers, you don't need n to be of float type. You can use any integer type.

To find if the number was scanned successfully, check the return value of scanf().

int n = 0;
if (scanf("%df",&n) != 1) {
    printf("Error\n");
    exit(EXIT_FAILURE);
}

And remove this part:

    if(n-ver!=0 || !isdigit(ver)){
        printf("Error\n");
    }

isdigit() can only handle single digit numbers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C - How to check if the number is integer or float?

From Dev

How to remove the nth hexadecimal digit of a integer number without using Strings?

From Dev

How to reverse a number using a while loop without string or integer operations?

From Dev

How to type negative number with .isdigit?

From Dev

How to check if number is integer number, with good precision?

From Dev

How do i find the right most digit of a number integer with java without using number % 10? see the description

From Java

How do I check that a number is float or integer?

From Dev

Twig - How to check if variable is a number / integer

From Dev

How to check if a big number is a square of an integer in R?

From Dev

Check If a number is contained in an interval without using "if" or loops

From Dev

Check if number is of Integer type

From Dev

Check if number is of Integer type

From Dev

How to store an ID number or Phone number as an integer without an error?

From Dev

how check string is integer or not by using spring annotations?

From Dev

Checking if number is an integer in C/C++ (without scanf/gets/etc)

From Dev

Displaying the digits of an integer without using a reverse number or arrays

From Java

How can I check, the number that scanner received is an integer certainly in java?

From Dev

How can I check when integer is incremented beyond a multiple of a number?

From Dev

How to check if a number (float or integer) is within a range (0 - 100)

From Dev

How can I check when integer is incremented beyond a multiple of a number?

From Dev

How to check if the input is a valid integer without any other chars?

From Dev

How can I check the next line without eating the next integer?

From Dev

How to check if the input is a valid integer without any other chars?

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to inverse a positive integer in C without * or - operators?

From Java

How to negate a positive integer in C without * or - operators?

From Dev

isdigit() always passes the check

From Dev

How to print the char, integer, float and double values without using format specifiers in c

From Dev

C++:How to convert string into integer without using any built in functions

Related Related

  1. 1

    C - How to check if the number is integer or float?

  2. 2

    How to remove the nth hexadecimal digit of a integer number without using Strings?

  3. 3

    How to reverse a number using a while loop without string or integer operations?

  4. 4

    How to type negative number with .isdigit?

  5. 5

    How to check if number is integer number, with good precision?

  6. 6

    How do i find the right most digit of a number integer with java without using number % 10? see the description

  7. 7

    How do I check that a number is float or integer?

  8. 8

    Twig - How to check if variable is a number / integer

  9. 9

    How to check if a big number is a square of an integer in R?

  10. 10

    Check If a number is contained in an interval without using "if" or loops

  11. 11

    Check if number is of Integer type

  12. 12

    Check if number is of Integer type

  13. 13

    How to store an ID number or Phone number as an integer without an error?

  14. 14

    how check string is integer or not by using spring annotations?

  15. 15

    Checking if number is an integer in C/C++ (without scanf/gets/etc)

  16. 16

    Displaying the digits of an integer without using a reverse number or arrays

  17. 17

    How can I check, the number that scanner received is an integer certainly in java?

  18. 18

    How can I check when integer is incremented beyond a multiple of a number?

  19. 19

    How to check if a number (float or integer) is within a range (0 - 100)

  20. 20

    How can I check when integer is incremented beyond a multiple of a number?

  21. 21

    How to check if the input is a valid integer without any other chars?

  22. 22

    How can I check the next line without eating the next integer?

  23. 23

    How to check if the input is a valid integer without any other chars?

  24. 24

    How to inverse a positive integer in C without * or - operators?

  25. 25

    How to inverse a positive integer in C without * or - operators?

  26. 26

    How to negate a positive integer in C without * or - operators?

  27. 27

    isdigit() always passes the check

  28. 28

    How to print the char, integer, float and double values without using format specifiers in c

  29. 29

    C++:How to convert string into integer without using any built in functions

HotTag

Archive