Skip to content

Instantly share code, notes, and snippets.

@ShahStavan
Last active July 2, 2021 09:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShahStavan/4af77958e84169c66e3311722901c449 to your computer and use it in GitHub Desktop.
Save ShahStavan/4af77958e84169c66e3311722901c449 to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
void numtochar(int n){
int mod = n%10;
string single[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" , "Ten"};
string double_val1[] = {"Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"};
string double_val[] = {"Ten" , "Twenty" , "Thirty" , "Fourty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety"};
if(n<=10){
cout<<single[n];
}
else if(n>10 && n<20 && mod>0 && n<100){
cout<<n<<"=" + double_val1[mod-1];
}
else if(mod == 0 && n>=20 && n<100){
int div = n/10;
cout<<n<<" = "<<double_val[div-1]<<endl;
}
else if(mod > 0 && n>=20 && n<100){
int div = n/10;
cout<<n<<" = " <<double_val[div-1] + " " + single[mod];
}
else{
cout<<"Inavlid input"<<endl;
cout<<"Enter two digits number only"<<endl;
}
}
int main()
{
int n;
cout<<"Enter number: "<<endl;
cin>>n;
numtochar(n);
return 0;
}
import java.util.*;
class NumtoC{
public void fun_numtochar(int n) {
int mod = n%10;
String single[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" ,"ten"};
String double_val1[] = {"Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"};
String double_val[] = {"Ten" , "Twenty" , "Thirty" , "Fourty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety"};
if(n<=10){
System.out.println(n + "=" + single[n]);
}
else if(n>10 && n<20 && mod>0 && n<100){
System.out.println(n + " " + double_val1[mod-1]);
}
else if(mod == 0 && n>=20 && n<100 ){
int div = n/10;
System.out.println(n + " = "+double_val[div-1]);
}
else if(mod > 0 && n>=20 && n<100){
int div = n/10;
System.out.println(n + " = "+double_val[div-1] + " " + single[mod]);
}
else{
System.out.println("Inavlid input");
}
}
}
public class numtochar {
public static void main(String[] args) {
int n;
System.out.println("Enter Number:");
Scanner input = new Scanner(System.in);
n = input.nextInt();
NumtoC number = new NumtoC();
number.fun_numtochar(n);
}
}
1st array for single value element that is from one to nine [string array]
string single[] = {"Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine"};
2nd array for double value element that is from eleven to nineteen [string array]
string double_val1[] = {"Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen"};
3rd array for double value element but should have gap of 10 that is ten,twenty,thirty and so on..
string double_val[] = {"Ten" , "Twenty" , "Thirty" , "Fourty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety"};
1st **If condition** Logic:
Condition : Whether the number is less than 10
Satisfied : single[n]
Example : Input = 1 || Condition = 1 < 10 (Satisfied) || single[1] which is One || Output: 1 = One
if(n<10){
cout<<single[n];
}
2nd **Else-If condition** Logic:
Condition : Whether number is greater than 10 and lesser than 20
2nd Condition : Whether modulo that is n%10 is greater than 0
3rd Condition : Number should be less than 100
Satisfied : cout<<n<<"=" + double_val1[mod-1];
Example : Input = 14 || All Conditions satisfied
mod = 14%10 = 4 [n%10]
double_val1[mod-1] = [4-1] = [3] which in our case is Fourteen
Here we subtract - 1 from mod because array index starts with 0
Output : 14 = Fourteen
else if(n>10 && n<20 && mod>0 && n<100){
cout<<n<<"=" + double_val1[mod-1];
}
3rd **Else-If condition** Logic:
Condition : Whether mod is equal to 0
2nd Condition : Whether number is greater than 20 and lesser than 100
Satisfied : cout<<n<<" = "<<double_val[div-1]<<endl;
Example : Input = 50 || All conditions are satisfied
Mod = 50%10 = 0 [n%10]
div = 50/10 = 5 [n/10]
double_val[div-1] = [5-1 = 4] = Fifty
Output: 50 = Fifty
else if(mod == 0 && n>=20 && n<100){
int div = n/10;
cout<<n<<" = "<<double_val[div-1]<<endl;
}
4th **Else-If condition** Logic:
Condition : Mod should be greater than 0
2nd condition : Number should be Greater than 20 and less than 100
Satisfied = cout<<n<<" = " <<double_val[div-1] + " " + single[mod];
Example: Input = 72 || All conditions are satisfied
mod = 72%10 = 2 [n%10]
div = 72/10 = 7 [n/10]
double_val[div-1] + " " + single[mod] = double_val[7-1 = 6] + single[2] = Seventy + Two = Seventy Two
div-1 because as we know array index starts with 0.
Output : 72 = Seventy Two
else if(mod > 0 && n>=20 && n<100){
int div = n/10;
cout<<n<<" = " <<double_val[div-1] + " " + single[mod];
}
5th **Else** [If no conditions are satisfied that is number is greater than 100 ]
We would show error message "Inavlid input" and ask them to enter two digits number
Thank You Guys🤘
This was all about. Still Have Queries? Ask me on Twitter or IG
Follow me on Instagram - glitchdevs and Twitter - @StavanS64859518
See you soon with some new Problem Solving Questions👋
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment