Checking for Digit using if else – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#1865 siteicon   siteicon   siteicon  

Checking for Digit using if else

Collecting a char input and checking the input as a digit or a non-digit.

Learning Objectives

  • Learning the range check for a digit.
  • Learning to find an alternate path when given selection condition is not true using the else keyword.

Program Approach

  • A digit would be essentially in the range of 0 to 9 so the condition check will be formed as character greater or equal to ‘0’ and lesser or equal to ‘9’.

Source Code

TC++ #1865


Source Code

Run Output

Enter a character : 5
You entered a digit

Enter a character : a
Character you entered was not a digit

Code Understanding

char d; cout<<“Enter a character : “; cin>>d;
Here we collect the input as a single character from the user.

if(d >= ‘0’ && d<=’9′)
Here we check if the character entered is between the range 0 to 9 as all the digit characters are supposed to be in that range only.

cout<<“You entered a digit”<<endl;
Here we print the message announcing that the number entered by the user was a digit.

This one is to give an alternate selection path when given condition of checking for digit is not true.

cout<<“Character you entered was not a digit”<<endl;
When the given condition is not true this message is printed.



  • For digit check one can also check using the ascii  value 48 to 57. The same condition may also be written like if( d>= 48 && d<=57).

Suggested Filename(s): checkdigit.cpp


sunmitra| Created: 22-Dec-2017 | Updated: 15-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues