Day 5 Loops Hackerrank Solution | 30 Days of Code

Find the Day 5 Loops Hackerrank Solution in C language. day 5 problem is dedicated to “Loop” in programming, we need to perform a task using “Loop”, as we all know there are basically 4 types of “Loop” in programming, first one is For Loop, the second one is While Loop(Whlie-Do Loop), and the third one is Do-While Loop.

There is one more loop which is For-Each(Used in Java Programming) loop to know more about the visit to Stackoverflow, now come to the point we also have to find the Loops Hackerrank Solution in Java, C++, and C Programming languages.

Day 5 Problem Statement

Given an integer,n, print its first 10 multiples. Each multiple n * i (where 1<=i<=10) should be printed on a new line in the form: n x i = result.

Input Format

A single integer, n.

Constraints

2<=n<=20

Output Format

Print 10 lines of output; each line i(where 1<=i<=10) contains the result of n * i in the form: n x i = result.

Sample Input

2

Sample Output

2 x 1 = 2

2 x 2 = 4

2 x 3 = 6

2 x 4 = 8

2 x 5 = 10

2 x 6 = 12

2 x 7 = 14

2 x 8 = 16

2 x 9 = 18

2 x 10 = 20

For Loop Syntax

Code Explanation

Here n is an integer number we have to print the table of n, first take user input and store the value in ‘n’. after that use a ‘for loop’ and put the three conditions in ‘for loop’. Below is a for-loop syntax.

So according to the above syntax take a user input, initialize the loop with i = 0 and put the condition i <= 10 and the last condition is i++ or i =i+1. And multiplication i and n and printing the result is according to the problem statement.

for(initialization ; condition ; increament / decreament)

{

---

---

Executable statements

---

---

}

Below is the Solution in all majors 3 languages, C, C++, and Java. So according to choice go through the solutions and try to understand the code. If you feel any difficulty comment below or contact us on our social media profiles.

Day 5 Loops Hackerrank 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);

    if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

    for (int i = 1; i <= 10; i++) 

    {

        printf("%i x %i = %i\n", n, i, n * i);

    }

    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;

}

Hackerrank Day 5 Solution in C++

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int n;

    cin >> n;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');

if(2<=n<=20)

{

     for (int i = 1; i <= 10; i++) 

     {

         cout <<n<<" "<<"x"<<" "<<i<<" "<<"="<<" "<<(n*i)<< endl;

     }

}

    return 0;

}

Loops 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])?");

        for (int i = 1; i <= 10; i++) 

        {

            System.out.println(n + " x " + i + " = " + n * i);

        }

        scanner.close();

    }

}

Loops Output

Day 5 Loops Solution

More on “30 Days of Code

  • Let’s Review
  • Arrays
  • Dictionaries and Maps
  • Recursion
  • Binary Numbers 

2 thoughts on “Day 5 Loops Hackerrank Solution | 30 Days of Code”

Leave a Comment