How to Check If a Number is Integer in C | Check If Number is Integer C
ADVERTISEMENTS
How to check if a number is integer in C | check if number is integer C.
In this article, you will learn how to check if a number is integer in C | check if number is integer C.
Example-1
Input: 15
15 is an integer number.
Example-2
Input: 15.3
15.3 is a floating-point number.
You should have knowledge of the following topics in c programming to understand these programs:
- C Strings
- C
main()function - C
forloop statement - C
whileloop statement - C
if-elsecondition statement - C
breakkeyword - C
printf()function
C Program to Check Whether a Number is Integer or Not using While loop
// C Program to Check Whether a Number is Integer or Not using While loop
#include <stdio.h>
int main() {
char random_number[100];
int f = 0, i = 0;
printf("Enter the number to check itself: ");
scanf("%s", random_number);
while (random_number[i++] != '\0') {
if (random_number[i] == '.') {
f = 1;
break;
}
}
if (f)
printf("\n%s is a floating-point number.\n", random_number);
else
printf("\n%s is an integer number.\n", random_number);
return 0;
}
Output
Enter the number to check itself: 10.0
10.0 is a floating-point number.
C Program to Check Whether a Number is Integer or Not using For loop
// C Program to Check Whether a Number is Integer or Not using For loop
#include <stdio.h>
int main() {
char random_number[100];
int f = 0;
printf("Enter the number to check itself: ");
scanf("%s", random_number);
for (int i = 0; random_number[i] != 0; i++) {
if (random_number[i] == '.') {
f = 1;
break;
}
}
if (f)
printf("\n%s is a floating-point number.\n", random_number);
else
printf("\n%s is an integer number.\n", random_number);
return 0;
}
Output
Enter the number to check itself: 15
15 is an integer number.
Explanation
In this program, we have taken input 15 from the user and parsed this input as a string.
Meanwhile using for loop or while loop on this string we find the . symbol to verify It's an integer or floating-point number.