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).

Respuesta :

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");

       }

   }

}

Explanation:

Output: 1995

             Yes

             Process finished with exit code 0