Sum of n Numbers using Python
Topic: Sum of n Numbers using Python.
Hey Guys, today we are going see a Python Program to calculate the sum of first n natural numbers.
Title: Python Code for Sum of n Numbers with user input. (Beginner Friendly+ Easy + Code + Output + Online Compiler)
What is this program and why we are going to use it?
This Program consists of Variables, Input Function, Loop (for loop), Arithmetic Operator and Conditional Understanding.
By using variables, we are going to store values in variables in int format. By using input function, we are going to input values from the user. For example, storing values:
n = 5
Here n is a variable where 5 is assigned to n.
Now for input function
By using variables, we are going to store values in variables in int format. By using input function, we are going to input values from the user. For example, storing values:
n = 5
Here n is a variable where 5 is assigned to n.
Now for input function
n = int(input("Enter Number n: "))
So, this will ask user to input a number.
Logic:
1. Take a number n as input from user.
2. Initialize a variable total = 0.
3. Use a loop from 1 to n.
- Add each number to total
4. Display the final sum.
Code:
Online Compiler (Switch to Interactive Mode before execution):
Applications:
1. Used to calculate sum of natural numbers
2. Helps in understanding loops (for loop)
3. Forms the base for mathematical and algorithmic problems
2. Helps in understanding loops (for loop)
3. Forms the base for mathematical and algorithmic problems
Comments
Post a Comment