PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on If & Else

Program to check whether a number is integer or float

Input - Output

Input
Enter a number : 12.6

Output
It is a floating point number

Input
Enter a number : 8

Output
It is an integer number

Algorithm

Step 1: START

Step 2: Enter a number as an input.

Step 3: declare another variable and intilize it with 0.

Step 3: Check the condition:

   for(int i=0;number[i];i++)

    if(number[i]=='.')

      flag=1;

      break;

    if(flag==0)

       print It is an integer number;

   else

       print It is a floating point number;

Step 4: END

CODE


/* Check number is integer or float */
#include  <stdio.h>
int main()
{
      int flag=0;
      char number[10];
      printf(" Enter a number : ");
      scanf("%s",number);
      for(int i=0;number[i];i++)
     {
          if(number[i]=='.')
       {
         flag=1;
         break;
       }
     }
      if(flag==0)
          printf(" It is an integer number ");
      else
          printf(" It is a floating point number ");
    return 0;
}


/* Check number is integer or float */
#include <iostream>
using namespace std;
int main()
{
      int flag=0;
      char number[10];
      cout<<" Enter a number : ";
      cin>>number;
      for(int i=0;number[i];i++)
     {
          if(number[i]=='.')
       {
         flag=1;
         break;
       }
     }
      if(flag==0)
          cout<<" It is an integer number ";
      else
          cout<<" It is a floating point number ";
    return 0;
}

OutPut

Enter a number : 12.6

It is a floating point number