In this tutorial, we are going to solve HackerRank for loop in c or write a program for this problem. in this problem, we need to take user input and for each user input we need to check if the value is greater or equal to 1 and less than and equal to 9 then we need to print the English representation of it in lowercase like if the value is 5 then print five and soon. else if the value is greater than 9 and is an even number then print even and if it is an odd number then print odd number.
In this problem, we need to take two user input values. the first value will be the start point of the value and then the last value is the end point of values. let’s say values are 5 and 12 then we need to check conditions for 5,6,7,8,9,10,11, and 12.
![HackerRank for loop in c solution](https://programmingoneonone.com/wp-content/uploads/2024/07/hackerRank-for-loop-in-c-solution.webp)
Problem solution in C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
char labels[11][6] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "even", "odd"};
int labels_index;
for (int i=a; i<=b; i++) {
labels_index = i <= 9 ? i - 1 : 9 + i % 2;
printf("%s\n", labels[labels_index]);
}
return 0;
}
In the above program first, we included the necessary header files. after that, we defined a main() function and then inside the main function, we defined two integer variables a and b. after that using the scanf() function we took user input values for variable and b, and then we defined the two-dimensional array and set some static values in it.
after that, we defined a new integer variable labels_index that will hold the value after calculation. after that, we used a for loop that started from value a and ran till value b. we implemented a conditional operator and checked if the value is less than or equal to 9 then we calculated the index by subtracting the 1 from that value. and if the value is greater than 9 then we first check if it’s an even or odd number and in the reminder of that value, we added the 9.
for example, if the value is 10 then 10%2 = 0 + 9 = 9, and on the 9th position of the array we have even text that is printed on the output screen. if the value is 11 then 11%2 = 1+9 = 10 and at the 10th position we have printed the odd text on the output screen.
Important thing about array
array index always starts from 0
anyone can explain this problem for me guys pls pls
what if the b value is smaller than value a ? the loop won't run isn't it?
In the second solution