09/03/2023

Day 3 Conditional Statements in C Hackerrank Solution | 30 Days

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


Day 3 conditional statements in c hackerrank solution

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.

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

Submit Your Solution Here: Click Here

Day 3 Conditional Statements in C Hackerrank Solution


#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 Intro to Conditional Statements Task

 
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.

Intro to Conditional Statements Logic


Let's start, so we have a number n and we need to write a program that follows the above four conditions as we can see that our first condition is if n is odd the program will print the 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 ranges from 2 to 5 the program will print the number "Not Weird", for that again each number we divide by 2 if the number is even and the number is between the range(2 to 5) the program will print Number is "Not Weird".

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

Day 3 Conditional Statements Output


conditional statements in c hackerrank solution Output

Similar to 30 Days of Code


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: