3/25/2017

Hackerrank Day 3 Solution in C - Intro to Conditional Statements

Write a program to find the hackerrank day 3 solution in c programming language this is a day 3 intro to conditional statements hackerrank problem statement. In this programming problem, we are going to learn about the conditional statements for example IF, IF-ELSE, and IF-ELSE-IF. So with the using of conditional statements we have to solve the given programming problem statement and run the program to check either day 3 solution in c program is following and passing all the conditions given according to the statement.

Hackerrank Day 3 Solution in C

Hackerrank day 3 intro to conditional statements are given below. Let's find the day 3 intro to conditional statements solution in a c programming language with code explanation and output of the program.

Day 3 Intro to Conditional Statements Task


According to the HackerRank program should follow these four given conditions. So below are the task and we have to find the Hackerrank Day 3 Solution in C language.

  • If n is odd, print Weird.
  • If n is even and in the inclusive range of 2 to 5, print Not Weird.
  • The third one If n is even and in the inclusive range of 6 to 20, print Weird.
  • If n is even and greater than 20, print Not Weird.

Hackerrank Day 3 Solution Logic


Let's start, so we have a number n and we need to write a program that follows the above four condition as we can see that our first condition is if n is odd that program will print number "Weird" so for this first condition we divide a number by 2 if a number is divisible by 2 then the number is even and if the number is not divisible by 2 then it will print number is "Weird'.

Now come to the second condition if the number is even and range of 2 to 5 the program will print number "Not Weird", for that again each number we divide by 2 if the number is even and the number is between range(2 to 5) the program will print Number is "Not Weird".

Now comes to the third condition if the number is between 6 to 20 then program will print number is " Weird". same as the second condition. Again check for the fourth condition if the number is greater than 20 then the program will print number is " Weird".

If you want to 30 days solution from Day 0 please check the below link. And also you can find more program below of this post.

Updated Solutions: Hackerrank Day 3 Solution in C, C++, and Java Programming Language 
Visit- Hackerrank Day 3 Solution C++ and Java Languages

Hackerrank Day 3 Solution in C Code


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
int main()
{
    char* N_endptr;
    char* N_str = readline();
    int N = strtol(N_str, &N_endptr, 10);
    // Complete the solve function below.
    
 if(N%2==0)
    {
        if(N>=2 && N<=5)
        {
            printf("Not Weird"); 
  }
        else if(N>=6 && N<=20)
        {
         printf("Weird");
  }
        else
        {
         printf("Not Weird");
  }    
    }
    else
    {
     printf("Weird"); 
 }
        
    if (N_endptr == N_str || *N_endptr != '\0') { exit(EXIT_FAILURE); }
    return 0;
}
char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);
    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
        if (!line) { break; }
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);
        if (!data) { break; }
        alloc_length = new_length;
    }
    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }
    data = realloc(data, data_length);
    return data;
}

Day 3 Hackerrank Solution in C Output

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: