Write a program to convert the string into an integer.
Example:
Input: str="1320"
Output: num=1320
Approach
Java
public class StringToInt {public static void main(String[] args) {String num = "1320";int numInt = stringToInt(num);System.out.println("Number Integer is " + numInt);}private static int stringToInt(String num) {return Integer.parseInt(num);}}
C++
#include <bits/stdc++.h>using namespace std;//function to convert the//string to integerint stringToint(string str){//string streamstringstream str1 (str);//varible to hold the integer valueint num=0;str1>>num;return num;}int main(){string str="1320";//call the function to convert//the into stringint num=stringToint(str);// print the integer valuecout << num;return 0;}
No comments:
Post a Comment