Hanwool Codes RSS Tag Admin Write Guestbook
2023-01-24 06:01:01

For Loop | HackerRank

 

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;
}
Hanwool Codes. Designed by 코딩재개발.