8.6k views
4 votes
Checker for integer string (Please answer in C++):

Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.

Ex: If the input is: 1995
the output is: yes

Ex: If the input is: 42,000
or
1995!
the output is: no
Hint: Use a loop and the isdigit() function (don't forget to include the cctype library).

User Jack Frost
by
5.7k points

1 Answer

1 vote

Answer:

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

String num = in.nextLine();

boolean allDigits = true;

for (int i = 0; i < num.length(); i++) {

if (!Character.isDigit(num.charAt(i))) {

allDigits = false;

}

}

if (allDigits) {

System.out.println("yes");

} else {

System.out.println("no");

}

}

}

Step-by-step explanation:

Output: 1995

Yes

Process finished with exit code 0

User Manisha  Bano
by
5.1k points