For Loop | HackerRank
Learn how to use for loop and print the output as per the given conditions
www.hackerrank.com
Goal
Print results according to two positive input values by using for loop statement.
Solution
To solve easily, I create vector string array included 1-9 numbers. If you use vector array, you can print numbers easily.
// For Loop
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
using namespace std;
int main() {
// Complete the code.
int a,b;
cin >> a;
cin >> b;
vector<string> numbers {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i = a; i <= b; i++){
if(i>9){
if( i % 2 == 0){
cout << "even" << endl;
}
else{
cout << "odd" << endl;
}
}
else{
cout << numbers[i] << endl;
}
}
return 0;
}
'software Engineering > (C++)HackerRank' 카테고리의 다른 글
[C++] HackerRank - Pointer (0) | 2023.02.08 |
---|---|
[C++] HackerRank - Functions (0) | 2023.02.04 |
[C++] HackerRank - Conditional Statements (0) | 2023.01.24 |
[C++] HackerRank - Basic Data Types (0) | 2023.01.22 |
[C++] HackerRank - "Input and Output" (0) | 2023.01.18 |