Intro to Conditional Statements Solution

Day 3 Intro to Conditional Statements Solution- [Hackerrank]

Hackerrank Day 3 Intro to Conditional Statements Solution in C, C++, and Java programming language. In this programming problem, we are going to learn about the If-Else statements, We have to use Even-Odd logic, and Greater and Less Number logic to solve this Programming Problem of the Hackerrank Website.

Problem Statement:- According to Hackerrank, you should follow these four given conditions. Hackerrank Intro to Conditional Statements Solution in C.

  • If ‘n‘ is odd, print Weird.
  • If ‘n‘ is even and in the inclusive range of 2 to 5, print Not Weird.
  • 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.
Input Output for Intro to Conditional Statements

Intro to Conditional Statements Solution Explained

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 the number as “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 a number is even and the number is between range(2 to 5) the program will print Number is “Not Weird“.

Again 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 Intro to Conditional Statements Solution In C

#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 Solution in C++ Language

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N;
    cin >> N;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
	// Complete the solve function below.
	if(N%2==0)
    {
        if(N>=2 && N<=5)
        {
        	cout<<"Not Weird";
		}
        else if(N>=6 && N<=20)
        {
        	cout<<"Weird";
		}    
        else
        {
        	cout<<"Not Weird";
		}
    }
    else
        cout<<"Weird";
    return 0;
}

Day 3 Hackerrank Solution in Java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2==0)
        {
        if(N>=2 && N<=5)
            System.out.print("Not Weird");
        else if(N>=6 && N<=20)
            System.out.print("Weird");
            else
            System.out.print("Not Weird");
    }
    else
        System.out.print("Weird");
        scanner.close();
    }
}

Solution Output

Solution Output of Intro to Conditional Statements

More on “30 Days of Code