0% found this document useful (0 votes)
5 views4 pages

Python Programs for Sum Calculations

The document contains three Python programs with algorithms. The first program calculates the sum of the first N natural numbers, the second program finds the sums of even and odd numbers up to N, and the third program visualizes monthly temperature variations using a line chart. Each program includes a clear algorithm and corresponding Python code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python Programs for Sum Calculations

The document contains three Python programs with algorithms. The first program calculates the sum of the first N natural numbers, the second program finds the sums of even and odd numbers up to N, and the third program visualizes monthly temperature variations using a line chart. Each program includes a clear algorithm and corresponding Python code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Program 01: Write an algorithm and python program to find the sum of ‘N’ natural

numbers.

ALGORITHM

1. Read the value of N.

2. Initialize a variable sum to 0.

3. Set a loop variable i to 1.

4. Repeat steps 5 and 6 while i is less than or equal to N.

5. Add i to sum.

6. Increment i by 1.

7. Print the value of sum as the sum of the first N natural numbers.

PROGRAM

Here's a Python program that calculates the sum of the first N natural numbers:

def sum_of_natural_numbers(n):

if n <= 0:

return "N should be a positive integer."

sum = 0

for i in range(1, n+1):

sum += i

return sum

n = int(input("Enter a positive integer: "))

result = sum_of_natural_numbers(n)

print("Sum of the first", n, "natural numbers:", result)


Program 02: Write python program and algorithm to find the sum of even sum and

odd sum for given N number.

ALGORITHM

1. Read the value of N.

2. Initialize variables even_sum and odd_sum to 0.

3. Set a loop variable i to 1.

4. Repeat steps 5 and 6 while i is less than or equal to N.

5. If i is even, add i to even_sum.

6. If i is odd, add i to odd_sum.

7. Print the value of even_sum as the sum of even numbers from 1 to N.

8. Print the value of odd_sum as the sum of odd numbers from 1 to N.

PROGRAM

def sum_of_even_and_odd_numbers(n):

if n <= 0:

return "N should be a positive integer."

even_sum = 0

odd_sum = 0

for i in range(1, n + 1):

if i % 2 == 0:

even_sum += i

else:

odd_sum += i

return even_sum, odd_sum

n = int(input("Enter a positive integer: "))

even_sum, odd_sum = sum_of_even_and_odd_numbers(n)

print("Sum of even numbers from 1 to", n, ":", even_sum)

print("Sum of odd numbers from 1 to", n, ":", odd_sum)


Program 03: Write a python program to visualize the monthly temperature

variation over a year.

ALGORITHM

1. Import Libraries:

• Import the necessary libraries: [Link] for creating visualizations and

numpy as np for numerical operations.

2. Create Sample Data:

• Define sample data for months and corresponding temperatures.

• months is created using [Link](1, 13) to represent the 12 months.

• temperature is a list containing temperature values for each month.

• This step is for demonstration purposes. Replace this sample data with your actual

dataset.

3. Create Line Chart:

• Use [Link]() to create a line chart.

▪ Set months as the x-axis variable.

▪ Set temperature as the y-axis variable.

▪ Use a red line (color='red').

▪ Display markers at data points using circles (marker='o').

▪ Use a solid line (linestyle='-').

• Set the x-axis label using [Link]('Months').

• Set the y-axis label using [Link]('Temperature (°C)').

• Set the plot title using [Link]('Monthly Temperature Variation').

4. Display the Plot: Use [Link]() to display the generated line chart.

PROGRAM

import [Link] as plt

import numpy as np

# Sample data
months = [Link](1, 13)

temperature = [20, 25, 30, 28, 35, 32, 30, 28, 25, 22, 18, 15]

# Create a line chart

[Link](months, temperature, marker='o', linestyle='-', color='red')

[Link]('Months')

[Link]('Temperature (°C)')

[Link]('Monthly Temperature Variation')

[Link]()

You might also like