For loop in C | Hackerrank Solution

For loop in C | Hackerrank solution


Welcome Back, Guys !!
Today in this post we will discuss For loop in  Hackerrank Practice problem in C programming language.
This problem is set for students to get an introductory knowledge of using For Loop in C.



For Loop in C Problem Statement:


Objective

In this challenge, you will learn the usage of the for loop, which is a programming language statement that allows code to be repeatedly executed.

The syntax for this is

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.
A sample loop will be

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

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, "two" for , 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<=10^6

Output Format

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


Sample Input

8
11

Sample Output

eight
nine
even
odd


For loop in C | Hackerrank solution

Given two numbers and, for every number starting from a till b, you have to print the English representation if the number is less than or print if it's even or odd. So, basically, you need to loop from till and print as per the given conditions. Code is as follows

Simple code using an if-else ladder:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>



int main() 
{
    int a=0,b=0,n=0;
    char* arr[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    scanf("%d",&a);
    scanf("%d",&b);

    for(n = a;n<=b;n++)
    {
       if(n == 1) {

        printf("one\n");

    }

    else if(n == 2) {

        printf("two\n");

    }

    else if(n == 3) {

        printf("three\n");

    }

    else if(n == 4) {

        printf("four\n");

    }

    else if(n == 5) {

        printf("five\n");

    }

    else if(n == 6) {

        printf("six\n");

    }

    else if(n == 7) {

        printf("seven\n");

    }

    else if(n == 8) {

        printf("eight\n");

    }

    else if(n == 9) {

        printf("nine\n");

    }

    else {

        if(n %2 ==0)
            printf("even\n");
        else
           printf("odd\n");

    }
    }
    return 0;

}


Code using arrays:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>



int main() 
{
    int a=0,b=0,n=0;
    char* arr[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    scanf("%d",&a);
    scanf("%d",&b);

    for(n = a;n<=b;n++)
    {
        if(n>9)
        {
            if(n%2 ==0)
               printf("even\n");
            else {
            printf("odd\n");
            }   
        }
        else {
          puts(arr[n]);
        }
    }
    return 0;

}

Analysis the code properly.
Feel Free to post your doubts in the comments down below.
See you next time.

Post a Comment

8 Comments

  1. sir i use this method but i got wrong output #include
    #include
    #include
    #include



    int main()
    {
    int i;
    int a, b;
    int c;
    scanf("%d\n%d", &a, &b);
    for(i=a;i9)
    {
    if(i%2==0)
    {
    printf("Even\n");

    }
    else
    {
    printf("Odd\n");

    }
    }
    c=i;
    switch(c) {

    case 1:
    printf("One\n");
    break;

    case 2:
    printf("Two\n");
    break;

    case 3:
    printf("Three\n");
    break;

    case 4:
    printf("Four\n");
    break;

    case 5:
    printf("Five\n");
    break;

    case 6:
    printf("Six\n");
    break;

    case 7:
    printf("Seven\n");
    break;

    case 8:
    printf("Eight\n");
    break;

    case 9:
    printf("Nine\n");
    break;

    }
    }

    return 0;
    }

    ReplyDelete
  2. #include
    #include
    #include
    #include



    int main()
    {
    int a,i,b;
    char* arr[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    scanf("%d\n%d", &a, &b);
    for(i=a;i<=b;i++)
    {
    if(i<=9){
    printf("%s\n",arr[i]);
    }
    else if(i%2==0){
    printf("even\n");
    }
    else{
    printf("odd\n");
    }
    }
    return 0;
    }

    ReplyDelete

  3. HackerRank C Programming all Solutions

    https://www.chase2learn.com/hackerrank-c-programming-solutions

    ReplyDelete
  4. sir the first code should have worked even without defining the array then why you defined it??

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. #include
    #include
    #include
    #include



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

    return 0;
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete