Sunday, 26 March 2017

Hacker Rank Solution For Day 5: Loops

Problem:- Write a Hackerrank Solution For Day 5: Loops or Hacker Rank Solution Program In C++ For "Day 5: Loops " or Hackerrank 30 days of code Java Solution: Day 5 or Loops solution or Hackerrank solution for 30 Days of Code Challenges.

Hacker Rank Solution For Day 5: Loops

Logic:- For day 5 loop solution we can take a for loop and put the condition in the loop for print the table. This is a very simple program, we just use one loop for this problem. We can also this problem using 'while loop' and 'do-while loop'.

Tip:- Try to solve this problem using 'while loop' and 'do-while loop'.

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

For Loop Syntax:-

for(initialization ; condition ; increament / decreament)
{
------------statements--------------
------------statements--------------
------------statements--------------
------------statements--------------
}

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

cin >> n; // User input
for( i = 1 ; i <= 10 ; i++ )
 {
        cout<<n<<" x "<<i<<" = "<<n*i<<endl;
 }
cout<<endl;

This will print the table of any number let's take an example of number 10, then the output is below.

Output:- The output of number 10.

10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

If you want to 30 days solution (All previous solution from day 0 ) from Day 0 please check the below link. You can also find more program in below of this post. Check the below solution with the full explanation.

1. Hackerrank Solution For Day 0: Hello, World.

2. Hackerrank Solution For Day 1: Data Types. 

3. Hackerrank Solution For Day 2: Operators.

4. Hackerrank Solution For Day 3: Intro to Conditional Statements 

5. Hackerrank Solution For Day 4: Class vs. Instance

Copy the colored code and paste it into hacker rank editor. If you have any query feel free to share with me, and if you like my work please share this Article.

Submit Your Solution Here:- Click Here

Solution:-

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main()
{
    int n,i;
    cin >> n;

    for(i=1;i<=10;i++)
    {
        cout<<n<<" x "<<i<<" = "<<n*i<<endl;
    }
    cout<<endl;

    return 0;
}

Output:-


Hacker Rank Solution For Day 5: Loops

You May Also Like:-







7. Hackerrank Solution For Day 12: Inheritance

8. Hackerrank Solution For Day 13: Abstract Classes

9. Hackerrank Solution For Day 14: Scope

10. Hackerrank Solution For Day 15: Linked List


No comments:
Write comments

Recommended Posts × +