Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int main() {
- char inp[100];
- // receive input as string
- scanf(" %s", inp);
- // use strlen function to get the length of the string
- int len = strlen(inp);
- // check if the input is valid by looping through the string from 0 to len-1
- for(int i=0;i<len;i++) {
- // if the character ASCII value is not between 0 and 9, then it is not a digit
- if(inp[i] < '0' || inp[i] > '9') {
- printf("Invalid input\n");
- return 0;
- }
- }
- // if the loop completes without returning, then the input is valid
- // convert the string to integer using atoi function
- int num = atoi(inp);
- // print the integer
- printf("The number is %d\n", num);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement