182k views
3 votes
موقع تحويل الكود c++ الى flowchart

20. Write a c++ Program to accept any single digit number
and print it in words.
# include
# include
# include
using namespace std;
int main( )
{
int n;
cout<<"enter a number :";
cin>>n;
switch(n)
{
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 9cout<<"NINE";
break;
default:
cout<<"please enter the number between 0 and 9";
}
return 0;
}

1 Answer

4 votes

Below is a C++ program to accept any single digit number and print it in words:

C++

#include <iostream>

using namespace std;

int main() {

int n;

cout << "Enter a single digit number: ";

cin >> n;

if (n >= 0 && n <= 9) {

switch (n) {

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;

}

} else {

cout << "Please enter a single digit number between 0 and 9";

}

return 0;

}

So, the program should work as expected. If the user enters a number between 0 and 9, it will print the corresponding word.

Hence, If the entered number is outside this range, it will display a message asking the user to enter a number between 0 and 9.

User Randy Klingelheber
by
8.0k points