How to Calculate the Sum of n Natural Numbers and How to Calculate the Average of first n natural numbers Using Python Programming Language

Hello everyone!

Good evening and welcome to my blog! Am so glad today to be your python teacher! I will also be glad if you give me your maximum attention as my student as we go down through this topic!

As we all know that python is a programming language and also also a scripting language. It is called a scripting language because it is an interpreted Language meaning that it uses interpreter to translate and run it's code. Python also have many applications of which the most popular Application areas are:

  • Web development
  • GUI application
  • Data science and artificial intelligence

I want to tutor you on how to use python to write some programs in our computer!


1. How to Calculate the Sum of n Natural Numbers


We all know what natural numbers are. And if you don't know what natural numbers are let me quickly tell you what they are. Natural numbers are those numbers used for counting and ordering! They are positive whole numbers and does not include zero and negative numbers. E.gs. are; 1, 2, 3, 4, 5, 6, 7, 8, 9, ..., n.

So we want to use python program to calculate the sum of n Natural Numbers.


Source code

  1. n = input("Enter Number to calculate sum")

  2. n = int (n)

  3. sum = 0

  4. for num in range(0,n+1,1):

  5. sum = sum+num

  6. print("SUM of first ", n, "numbers is: ", sum )

Output:

Enter Number to calculate sum

SUM of first 5 numbers is: 15


As you can see in the above program, we have six source lines of codes which means that the python interpreter will translate these codes line by line to machine understandable language.

As we all know, the above source codes are written in human readable form(high-level language) and the computer we're giving these instructions does not understand this language, so there must be a program translator to translate it from high-level language to low-level language, in this case, the interpreter.


2. How to Calculate the Average of first n natural numbers


All of us know what is average! But in case if you don't know, let me quickly tell you what an average is. An average is the sum of numbers divided by the total number.
E.g. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

The average of these numbers is calculated thus; 1+2+3+4+5+6+7+8+9+10/10 = 55/10
= 5.5

Therefore, the average of these numbers is 5.5


Source code:

  1. print ("calculate an average of first n natural numbers")

  2. n = input("Enter Number: ")

  3. n = int (n)

  4. average = 0

  5. sum = 0

  6. for num in range(0,n+1,1):

  7. sum = sum+num;

  8. average = sum / n

  9. print("Average of first ", n, "number is: ", average)

Output:

calculate an average of first n natural numbers

Enter Number: 5

Average of first 5 number is: 3.0


So students, these programs are up and running if you have any python IDE installed in your Mobile Phone.
*Tips: Go to Google playstore and download pyroid 3!



Conclusion

Python is a programming language that is very easy to learn and understand. You don't have to declare variables as seen in other programing languages like C, C, Java, e.t.c. Python does not include any preprocessor directives as seen in C. So with this I can say that python is a good memory management application because it doesn't consume much space and time, hence efficient!



Regards, @anyiglobal



H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center