We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Exercise was ment to teach conditional statements, so I used them all without anything else which would be (cheating in my opinion).
I changed input type int n to unsigned int n because of given constraints. It says that there can't be negative numbers, so we should use a proper type.
I have put Greater than 9 condition before all other because it is more likely that user will input greater number than 9. This way we prevent 9 statement checks which will make code execute faster in most cases.
This is the code:
unsignedintn=stoi(ltrim(rtrim(n_temp)));if(n>9)cout<<"Greater than 9";elseif(n==1)cout<<"one";elseif(n==2)cout<<"two";elseif(n==3)cout<<"three";elseif(n==4)cout<<"four";elseif(n==5)cout<<"five";elseif(n==6)cout<<"six";elseif(n==7)cout<<"seven";elseif(n==8)cout<<"eight";elsecout<<"nine";
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Conditional Statements
You are viewing a single comment's thread. Return to all comments →
Exercise was ment to teach conditional statements, so I used them all without anything else which would be (cheating in my opinion).
I changed input type
int n
tounsigned int n
because of given constraints. It says that there can't be negative numbers, so we should use a proper type.I have put
Greater than 9
condition before all other because it is more likely that user will input greater number than 9. This way we prevent 9 statement checks which will make code execute faster in most cases.This is the code: