For Loop in C++ - Hacker Rank Solution

Today we will learn about For Loops in C++ and how to use them in our program to get the output in a loop. After going through this post you will

For Loop in C++ - Hacker Rank Solution

Hello coders,

Today we will learn about For Loops in C++ and how to use them in our program to get the output in a loop.

After going through this post you will clearly understand the solution of For Loop in C++ Hacker Rank problem.

So, Without Further Ado let's dive into the question.

For Loop in C++ - Hacker Rank Solution | Sloth Coders

Problem 

A for loop is a programming language statement which allows code to be repeatedly executed.

The syntax is 

for ( <expression_1> ; <expression_2> ; <expression_3> )
<statement>

  • expression_1 is used for initializing variables which are generally used for controlling the terminating flag for the loop. 
  • expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
  • expression_3 is generally used to update the flags/variables.

A sample loop is 

for ( int i = 0; i < 10; i++) {
    . . .
}

In this challenge, you will use a  for loop to increment a variable through a range.

Input Format

You will be given two positive integers, a and b (a  b), separated by a newline.

Output Format

For each integer n in the inclusive interval [a,b]:

If  n  9, then print the English representation of it in lowercase.That is "one" for 1, "two" for 2 and so on.

Else if n > 9 and it is even number, then print "even".

Else if n  > 9 and it is odd number, then print "odd".

Note:  [a,b] = { x  Z | a  x  b } = { a, a+1, . . ., b}

Sample Input

8
11

Sample Output 

eight
nine
even 
odd

Solution - For Loop in C++

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a, b;
    cin >> a >> b;
    for (int i = a; i<=b; i++)
    {
        if ( i == 1){
            cout << "one" << endl;
        }
        else if ( i == 2) {
            cout << "two" << endl;
        }
        else if ( i == 3){
            cout << "three" << endl;
        }
        else if ( i == 4) {
            cout << "four" << endl;
        }
        else if ( i == 5) {
            cout << "five" << endl;
        }
        else if ( i == 6) {
            cout << "six" << endl;
        }
        else if ( i == 7) {
            cout << "seven" << endl;
        }
        else if ( i == 8) {
            cout << "eight" << endl;  
        }
        else if ( i == 9) {
            cout << "nine" << endl; 
        }
        else if ( i > 9) {
            if ( i % 2 == 0) {
            cout << "even" << endl;
            }
            else {
            cout << "odd" <<endl;
            }
        }         
    }
    
    return 0;
}
For Loop in C++ - Hacker Rank Solution
For Loop in C++ - Solution
For Loop in C++ - Hacker Rank Solution
For Loop in C++ - Solution
For Loop in C++ - Hacker Rank Solution Results
For Loop in C++ - Result


Conclusion

So, coders today we have learnt about for loops and how to use them in our program. If you have any doubt regarding the problem ( For Loop in C++ ), feel free to contact in the Comment section.

Disclaimer: The above problem is generated by Hacker Rank but the solution is provided by Sloth Coders.

Don't forget to Share this post with your friends and Subscribe to our Blog to get latest updates related to coding.

Happy Coding!!

A Sloth Who loves to Code.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment