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:

--

--

--

Studying B.TECH(IT) at VCET Madurai.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Introduction to Voice over Internet Protocol- Part

Qubic status update October 3rd 2018

Side Projects Invigorate Software Engineering Culture

Qubic status update July 3rd 2019

Introduction of AWS DynamoDB!!

Simplifying Cloud Migration using Microsoft Azure Migrate

Explaining the Qubic Computation Model: part 3

Setting up GlusterFS on Fedora

Gomathi Keerthana.R.S.

Gomathi Keerthana.R.S.

Studying B.TECH(IT) at VCET Madurai.

More from Medium

QR code reader for OS X via Terminal

Programming Language and Its Types

Python vs Ruby: Which to Choose?

Advent Of Code 2021 — Smoke Basin — Puzzle 9