C++ PROGRAM TO CONVERT NUMBERS INTO CHARACTERS!!!
|CONVERT NUMBERS INTO CHARACTER|
In C++ language, we can easily convert numbers into characters with the help of a loop and switch case. In this program, we are taking input from the user and iterating this number until it is 0. While iteration, we are dividing it by 10, and the remainder is passed in switch case to get the word for the number.
- Let’s see the C++ program to convert numbers into characters.
““#include<iostream>” →Input-output continuous bytes of data.
“using namespace std;” →used to store variable name.
“int main()” →returns datatype function.”
“{ long int n,sum=0,r;
cout<<”Enter the Number= “;
cin>>n;”
- Here, we have declared three variable in long integer datatype(n, sum=0, r)
long int →accepts many digits.
cout →character output
cin →character input
“while(n>0)
{ r=n%10;
sum=sum*10+r;
n=n/10; }”
- Here, if the given number(n) is greater than 0 it will enter while loop
WHILE LOOP:
The syntax of a while loop in C++ is − while(condition) { statement(s); } Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
“n=sum;
while(n>0)
{
r=n%10;
switch(r)
{
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;
case 0:
cout<<”zero “;
break;
default:
cout<<”tttt “;
break;
}
n=n/10;
}
}”
SWITCH:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
- Here, we have given number (n) is equal to sum; And n should be greater than 0; In the switch statement, we have given 10 cases and atlast we have given the default statement.
break →used to terminate the loop.
SOURCE CODE:
OUTPUT:
REFERENCE LINK:
To know more about C++ PROGRAM, Login “GUVI”