Integer To Character in C++
Q. Write a C++ program to convert Number in Character?
#include
using namespace std;
int main()
{
int n, sum = 0;
cout << "Enter the number to print in words: ";
cin >> n;
while(n > 0)
{
sum = (sum * 10) + (n % 10);
n = n /10;
}
while(sum > 0)
{
switch(sum % 10)
{
case 0:
cout << "Zero ";
break;
case 1:
cout << "One ";
break;
case 2:
cout << "Two ";
break;
case 3:
cout << "Three ";
break;
case 4:
cout << "Four ";
break;
case 5:
cout << "Five ";
break;
case 6:
cout << "Six ";
break;
case 7:
cout << "Seven ";
break;
case 8:
cout << "Eight ";
break;
case 9:
cout << "Nine ";
break;
}
sum = sum / 10;
}
return 0;
}
Q. Write an algorithm to convert Number in Character?
1. Start
2. Initialize variables
- Create an integer variable `n` to store the input number.
- Create an integer variable `sum` and set it to 0.
3. Input the number `n`
- Prompt the user: "Enter the number to print in words: "
- Read the integer `n`.
4. Reverse the number `n` and store in `sum`
- While `n > 0`
a. Extract the last digit of `n` as `digit = n % 10`
b. Update `sum = sum * 10 + digit` (this reverses the digits)
c. Divide `n` by 10 (integer division) to remove the last digit `n = n / 10`.
5. Print the words corresponding to each digit of reversed number (`sum`)
- While `sum > 0`
a. Extract last digit: `digit = sum % 10`
b. Use a switch-case or if-else to find the word representation of the digit:
- 0 → "Zero"
- 1 → "One"
- 2 → "Two"
- 3 → "Three"
- 4 → "Four"
- 5 → "Five"
- 6 → "Six"
- 7 → "Seven"
- 8 → "Eight"
- 9 → "Nine"
c. Print the corresponding word followed by a space.
d. Remove last digit from `sum` by integer division `sum = sum / 10`.
6. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com