HackerRank Conditional Statements in C solution

In this tutorial, we are going to solve HackerRank conditional statements in the c problem or write a solution program. In this problem, we need to read the positive integer value and then we need to check if the value is greater than print the text “Greater than 9” Else we need to print the English word that corresponds to that value in lowercase like if value 1 then print the one in lowercase.

Conditional Statements in C

Problem solution in C programming.

#include<stdio.h>
#include<string.h>
int main()
{
 int n;
 scanf("%d",&n);
  char *a[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
  if(n<=9)
  {
     printf("%s",a[n]);
  }

 else
 {
     printf("Greater than 9");
 }
 return 0;
}

Explanation

In the above-given program first, we included the two header files stdio.h and string.h, after that we defined the main() function. inside the main function, we first defined the integer variable n and then using the scanf() function we read the user input value and stored it in the variable n.

after that, we defined an array named a with predefined values like zero, one, two, three, four, five, six, seven, eight, and nine. we used these values to get the corresponding English words of value from 0 to 9.

After that using the if condition we checked if the value was less than or equal to 9 then using the printf() function we printed the corresponding English word from the array. else we printed the “Greater than 9” text on the output screen.

Conditional Statements in c

2 thoughts on “HackerRank Conditional Statements in C solution”

  1. Thank you for C solution but I have a question. how does the program know which one of the arrays to print? I mean, if the input is 5 then it returned "Five" but how does the program knows exactly?

Leave a Comment