avatarKaushik Katari

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1993

Abstract

dual digits.</p><p id="d968">Once the loop ends, we are comparing the values of n and s and printing the appropriate output.</p><p id="1ac1">Here:</p><ol><li>n is input</li><li>d is digit</li><li>s is sum of the ‘digits power 3’</li></ol><p id="414a">Now, let’s see how we can use the above code to determine Armstrong number for n digits. Once you are familiar with the above code for 3 digit number, with small tweak to your code, you can check for n number of digits in a number.</p> <figure id="9e19"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Kaushik-Varma/f7357a395b6b075a65b596967b72ef60.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="7e6a">If you observe in the above code, we took another variable ‘p’ in line #2, to find out the number of digits the input number has, so that we know the power of each digit while adding, which we have done in line #7.</p><h2 id="c242">2. Fenced Matrix</h2><p id="8051">While learning Python Programming, often we will come across the concept of 2-D Arrays also known as Matrix. While learning the Matrix, you’ll be asked to create a specific type of Matrix called as Fenced Matrix.</p><h2 id="a100">What is a Fenced Matrix?</h2><figure id="f8cc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*M3VH4a40Z7pvWtRJ6bmDyQ.jpeg"><figcaption>Fenced Matrix</figcaption></figure><p id="2b65">You have to make a list of lists of size m*n, that is m sub-lists (rows), with each sub-lists having n integers (columns). The matrix should be such that it should have 1 on the border and 0 everywhere else.</p><p id="4bba">Let’s see how we can write a program for that. Before proceeding further I strongly suggest to attempt to write the code yourselves first.</p><p id="be7b" type="7">To understand the code, one should have an understanding abo

Options

ut some of the Python Programming topics:</p><p id="d481" type="7">i) Python if….else statements</p><p id="afa7" type="7">ii) Python nested for loops</p> <figure id="0348"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Kaushik-Varma/e6d62b6370889c875e123d5c72f7294d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="a278">Here, we are taking input (no.of rows &amp; no. of columns) and converting them into a single list.</p><p id="cdf5">“m” is no. of rows</p><p id="9b11">‘‘n’’ is no. columns.</p><p id="a158">In line #6 we are creating a list of zeroes, which has ’n’ elements in single row. In line #8 we are extending that single row of list into ‘m’ no. of rows which contains only zeroes.</p><p id="d0d0">Now, by using nested for loops and if statements as shown, we are creating <b>Fenced Matrix </b>which has element ‘1’ on the border and ‘0’ everywhere else.</p><p id="a221">The output will look like this</p><div id="41e7"><pre><span class="hljs-attribute">4</span>,<span class="hljs-number">5</span><span class="hljs-meta">

[1, 1, 1, 1, 1]</span><span class="hljs-meta"> [1, 0, 0, 0, 1]</span><span class="hljs-meta"> [1, 0, 0, 0, 1]</span><span class="hljs-meta"> [1, 1, 1, 1, 1]</span></pre></div><p id="bcdc">Here ‘4’ is no. of rows and ‘5’ is no. of columns.</p><h2 id="b9ec">Conclusion</h2><p id="8f71">Finally, this is how one can check for <b>Armstrong Number</b> and can print <b>Fenced Matrix</b></p><p id="49e1">You can also check my previous article <a href="https://medium.com/@kaushikvarma.katari/python-problems-for-basics-reference-swapping-factorial-reverse-digits-pattern-print-241dde763c74?source=friends_link&amp;sk=2e5bf5b0ace69c8bbdcce194b12175e0">Python: Problems for Basics Reference</a></p><p id="4d53">Thank you for reading and Happy Coding!!!!</p></article></body>

Photo by Maxwell Nelson on Unsplash

Python Programs to check for Armstrong Number (n digit) and Fenced Matrix

This article is for those who are new to the Python Programing and wants to learn some basic concepts about looping and if…else statements.

Here we will see how to check whether the given number is Armstrong Number or not and after that we will see about the Fenced Matrix.

1. What is an Armstrong Number?

An Armstrong number in a given number base is a number that is the sum of its own digits each raised to the power of the number of digits.

Following are some of the examples of Armstrong Numbers.

i) 153 = 1³ + 5³ + 3³

ii) 1634 = 1⁴+ 6⁴ + 3⁴ + 4⁴

Let’s see how we can write a program to find whether a given number is an Armstrong Number or not in Python.

To understand the code, one should have an understanding about some of the Python Programming topics:

i) Python if….else statements

ii) Python While loop

To check whether the given number is Armstrong number or not, first we will check for a 3 digit number.

In the above code we are taking a 3 digit number as input and running the while loop till the value of k is greater than 0.

Here ‘k’ is used to divide the input number into individual digits.

Once the loop ends, we are comparing the values of n and s and printing the appropriate output.

Here:

  1. n is input
  2. d is digit
  3. s is sum of the ‘digits power 3’

Now, let’s see how we can use the above code to determine Armstrong number for n digits. Once you are familiar with the above code for 3 digit number, with small tweak to your code, you can check for n number of digits in a number.

If you observe in the above code, we took another variable ‘p’ in line #2, to find out the number of digits the input number has, so that we know the power of each digit while adding, which we have done in line #7.

2. Fenced Matrix

While learning Python Programming, often we will come across the concept of 2-D Arrays also known as Matrix. While learning the Matrix, you’ll be asked to create a specific type of Matrix called as Fenced Matrix.

What is a Fenced Matrix?

Fenced Matrix

You have to make a list of lists of size m*n, that is m sub-lists (rows), with each sub-lists having n integers (columns). The matrix should be such that it should have 1 on the border and 0 everywhere else.

Let’s see how we can write a program for that. Before proceeding further I strongly suggest to attempt to write the code yourselves first.

To understand the code, one should have an understanding about some of the Python Programming topics:

i) Python if….else statements

ii) Python nested for loops

Here, we are taking input (no.of rows & no. of columns) and converting them into a single list.

“m” is no. of rows

‘‘n’’ is no. columns.

In line #6 we are creating a list of zeroes, which has ’n’ elements in single row. In line #8 we are extending that single row of list into ‘m’ no. of rows which contains only zeroes.

Now, by using nested for loops and if statements as shown, we are creating Fenced Matrix which has element ‘1’ on the border and ‘0’ everywhere else.

The output will look like this

4,5
[1, 1, 1, 1, 1]
[1, 0, 0, 0, 1]
[1, 0, 0, 0, 1]
[1, 1, 1, 1, 1]

Here ‘4’ is no. of rows and ‘5’ is no. of columns.

Conclusion

Finally, this is how one can check for Armstrong Number and can print Fenced Matrix

You can also check my previous article Python: Problems for Basics Reference

Thank you for reading and Happy Coding!!!!

Python
Programming
Armstrong
Matrix
Coding
Recommended from ReadMedium