c++ please
The only outside functions you can use are: a) cout, cin, rand(), srand(), and time() b) functions you write yourself
Q3) convert character digit to number
CharToNum('0') ==> 0
CharToNum('9') ==> 9
int CharToNum(const char c) {
}
You just have to subtract ASCII value of 0 from the character, to get character as number.
I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.
------------------main.cpp-----------
#include <iostream>
using namespace std;
int CharToNum(const char c)
{
int num = c - '0';//Subtract Ascii value of '0' from
character, gives actual number
return num;
}
int main()
{
int n0 = CharToNum('0'); //Sample test cases
int n1 = CharToNum('1');
int n2 = CharToNum('2');
int n3 = CharToNum('3');
cout << "\nSum(0, 1, 2, 3) = " <<
n0+n1+n2+n3 << endl; //print sum and verify
return 0;
}
------------------Screenshot main.cpp-----------
-----------------Output-----------
-----------------------------------------
I hope this helps you,
Please rate this answer if it helped you,
Thanks for the opportunity
Get Answers For Free
Most questions answered within 1 hours.