Convert char digit to int

Convert char digit to int

Easy Java Data Types 24 views
Explanation Complexity

Problem Statement

Task: given a digit character '0'..'9', return its integer value.

Input Format

A character ch ('0' to '9').

Output Format

An integer: numeric value of the digit.

Example

'7'
7

Constraints

• ch is between '0' and '9'

• Use basic character logic

Concept Explanation

In Java, digit characters are stored using ASCII/Unicode values.
To get the integer value, subtract '0' from the character.

Step-by-Step Explanation

1.Read character ch.

2.Compute value = ch - '0'.

3.Return value.

Concept Explanation

In Java, digit characters are stored using ASCII/Unicode values.
To get the integer value, subtract '0' from the character.

Step-by-Step Explanation

1.Read character ch.

2.Compute value = ch - '0'.

3.Return value.

Input / Output Format

Input Format
A character ch ('0' to '9').
Output Format
An integer: numeric value of the digit.
Constraints
• ch is between '0' and '9'

• Use basic character logic

Examples

Input:
'7'
Output:
7

Example Solution (Public)

Java
static int digitToInt(char c){return c-'0';}

Official Solution Code

static int digitToInt(char c){return c-'0';}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.