In: Computer Science
C PROGRAMMIMG
I want to check if my 2 input is a number or not
all of the input are first stored in an array if this the right way?
read = sscanf(file, %s%s%s, name, num, last_name);
if(strcmp(num, "0") != 0)
printf("Invalid. Please enter a number.")
CODE:
#include <stdio.h>
#include <string.h>
int main(void) {
char name[10];
char num[10];
char last_name[10];
int i;
scanf("%s%s%s", name, num, last_name);
// to check if all the digits in the array are numbers
int numberOfDigits=0;
for(i=0;i<strlen(num);i++){
// if the number is between 0 and 9
if(num[i] >= '0' && num[i] <= '9')
numberOfDigits++;
}
if(numberOfDigits==strlen(num))
printf("Number\n");
else
printf("Invalid. Please enter a number.\n");
return 0;
}

OUTPUT:

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.