Tuesday, January 7, 2020

For Loop in C - Hacker Rank Solution

For Loop in C - Hacker Rank 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. You can find the tester's code below.

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

int main() {

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

4 comments:

  1. int main()
    {
    int a,b,i;
    scanf("%d\n%d", &a, &b);
    char *str[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

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

    ReplyDelete
  2. Tecocraft is the most valuable iOS, iPhone & iPad app development service in the US providers that build a high-quality app that helps you grow your online business exponentially

    ReplyDelete

Powered by Blogger.