Input Format
You will be given two positive integers, a and b(a<=b), separated by a newline.
Output Format
For each integer in the inclusive interval :
If 1 <= n <= 9, then print the English representation of it in lowercase. That is “one” for , “two” for , and so on. Else if n>9 and it is an even number, then print “even”.
Else if n>9 and it is an odd number, then print “odd”.
Sample Input
8
11
Sample Output
eight
nine
even
odd
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
// Complete the code.
int a;
int b;
cin>>a>>b;
for(int i = a; i <= b; i++){
if(i <=9 ){
switch(i){
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{
if(i % 2 == 0){
cout<<"even";
}else{
cout<<"odd";
}
}
cout<<endl;
}
return 0;
}
c++14 version
Explaining the Code to the Audience of GetCode
This C++ program solves a very common problem: printing the name of the numbers-for numbers between 1 and 9-and for numbers larger than 9, it prints whether the number is odd or even. Now, let’s go through the code in detail to see the logical explanation of it.
#include <iostream>: This is used for input and output operations, namely cin and cout.
#include <cstdio>: For C-style input and output. Not used in this code. Probably out of habit when writing code.
int a;
int b;
int a and int b: Variable to store the range of number input provided by the user.
cin >> a >> b;
Purpose: Takes input from user two integers a and b. Represents the start and end of a range, inclusive. Iteration Through Range.
for(int i = a; i <= b; i++) {
Purpose: iterates through every number between a and b. Handling Numbers <= 9:
if (i <= 9) {
switch (i) {
case 1: cout << “one”; break;
case 2: cout << “two”; break;
//. other cases
}
}
For any digit from 1 to 9, a switch is used that prints their name in English – one, two etc.
Handling Numbers > 9:
} else {
if (i % 2 == 0) {
cout << “even”;
} else {
cout << “odd”;
}
}
i % 2 == 0 Checks whether the number is even.
If true, prints “even”.
Else, prints “odd”.
-formatting Output:
cout << endl;
Displays result from a new line every time.
End
return 0;
Indicates that the program was executed successfully.
Sample Input and Output
Input→Explanation→Output
1 5 Numbers 1 to 5 are within 1-9. one
two
three
four
five
8 11 , 8 is in 1-9, others are odd/even. eight
odd
even
odd
Key Features:
Simple Logic: Combines a for loop, switch statement, and conditional checks.
Readable Output: Ensures clarity by mapping names and distinguishing odd/even numbers.
Versatile Range: Works for any positive range [a, b] with a <= b.
Why is this Code Effective?
Beginner-Friendly: Uses basic concepts like loops, conditionals, and switches.
Logical Flow: Covers all cases (1-9, odd/even) systematically.
Efficiency: The range is processed within one loop.
This is a HackerRank problem. I have taken this question from: https://www.hackerrank.com/challenges/c-tutorial-for-loop/problem?isFullScreen=true