Contents

For Loop in C

Contents

A challenges Hackerrank and its solution.

Objective

In this challenge, you will learn the usage of the for loop, which is a programming language statement which allows code to be executed until a terminal condition is met. They can even repeat forever if the terminal condition is never met.

The syntax for the for loop is:

1
2
for ( <expression_1> ; <expression_2> ; <expression_3> )
    <statement>
  • expression_1 is used for intializing 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.

The following loop initializes i to 0, tests that i is less than 10, and increments i at every iteration. It will execute 10 times.

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

Task

For each integer n in the interval [a,b] (given as input) :

  • If \(1 \leq n \leq 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 an even number, then print “even”.
  • Else if \(n > 9\) and it is an odd number, then print “odd”.

Input Format

The first line contains an integer, a. The seond line contains an integer, b.

Constraints \(1 \leq a \leq b \leq 10^6\)

Output Format

Print the appropriate English representation,even, or odd, based on the conditions described in the ’task’ section.

Note: \([a,b] = {x \in \Zeta | a \leq x \leq b} = {a, a + 1, … , b}\)

Sample Input

1
2
8
11

Sample Output

1
2
3
4
eight
nine
even
odd

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>



int main() 
{
    int a, b;
    scanf("%d\n%d", &a, &b);
  	// Complete the code.
    for (int i = a; i <= b;i++){
        if (i == 1){
            printf("one");
        }
        else if (i == 2){
            printf("two");
        }
        else if (i == 3){
            printf("three");
        }else if (i == 4){
            printf("four");
        }else if (i == 5){
            printf("five");
        }else if (i == 6){
            printf("six");
        }else if (i == 7){
            printf("seven");
        }else if (i == 8){
            printf("eight");
        }else if (i == 9){
            printf("nine");
        }
        if (i>=1 && i<=9){
            printf("\n");
        }
    }
    for (int i = 10; i <= b;i++){
        if (i%2==0){
            printf("even");
        }else{
            printf("odd");
        }
    printf("\n");
    }
    return 0;
}