Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
HackerRank for loop in c solution

HackerRank For Loop in C solution

YASH PAL, 14 July 202416 June 2025

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.

Solution explained in Video

For loop in c solution

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 *

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes