Convert number string to integers in C++


Here we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.

The atoi() function is present in the <cstdlib> library.

Input: A number string “1234”
Output: 1234

Algorithm

Step 1: Take a number string
Step 2: Convert it to integer using atoi() function
Step 3: Print the result.
Step 4: End

Example Code

 Live Demo

#include<iostream>
#include<cstdlib>
using namespace std;
main() {
   int n;
   char num_string[20] = "1234";
   n = atoi(num_string);
   cout << n;
}

Output

1234

Updated on: 30-Jul-2019

219 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements