Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank For Loop in C solution

YASH PAL, 14 July 202416 January 2026

HackerRank For Loop in C Solution – 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.

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.

Task

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

  • If 1 <= 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 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 <= a <= b <= 106

Output Format

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

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
HackerRank for loop in c solution

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

Next problem solution – HackerRank Sum of Digits of a Five Digit Number in C solution

C Solutions coding problems solutions Hackerrank Problems Solutions cHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C Solutions
Hello world in c solution
Playing with characters problem solution
sum and difference of two numbers problem solution
functions in c problem solution
pointers in c problem solution
Conditional statements in c problem solution
For loop in c solution
Sum of Digits of a five-digit number problem solution
Bitwise operators problem solution
Printing pattern using loops problem solution
1D Arrays in c problem solution
Array Reversal problem solution
Printing Tokens problem solution
Digit Frequency problem solution
Dynamic Array in c problem solution
Calculate the Nth term problem solution
Students Marks sum problem solution
Sorting Array of strings problem solution
Permutations of strings problem solution
Variadic functions in c problem solution
Querying the documents problem solution
Boxes through a tunnel problem solution
Small Triangles, Large Triangles problem solution
post-transition problem solution
Structuring the document problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes