Program3(a)

3.a. Read N numbers from the console and create a list. Develop a program to print mean, variance and standard deviation with suitable messages.

Objective of the Program

  • To understand list creation from user input

  • To perform statistical calculations

  • To use loops for computation

  • To apply mathematical formulas in programming


Logical Construction (Think Before You Code)

Before writing the program, understand the mathematical formulas clearly.

Suppose the list contains N numbers:

x1, x2, x3, ... , xN

1.Mean (Average)

                                            Mean=(Sumofallelements)/N

2.Variance

Steps:
  • Subtract mean from each element

  • Square the result

  • Add all squared values

  • Divide by N


3.Standard Deviation


                                                         Standard Deviation=SQRT(Variance)


Program Logic Steps

  1. Read N

  2. Create empty list

  3. Read N numbers and store in list

  4. Compute sum

  5. Calculate mean

  6. Compute squared differences

  7. Calculate variance

  8. Calculate standard deviation

  9. Display results

👉 The key idea:
First compute mean, then use it to compute variance, then compute standard deviation.


Flowchart (Block Diagram of Logic)



Flowchart Explanation
  • Start

  • Read N

  • Input N numbers into list

  • Calculate sum

  • Compute mean

  • Compute variance

  • Compute standard deviation

  • Display results

  • Stop


Algorithm

  1. Start

  2. Read N

  3. Initialize empty list

  4. For i from 1 to N

    • Read number

    • Append to list

  5. Calculate sum of list

  6. Compute mean = sum / N

  7. Initialize variance_sum = 0

  8. For each element in list

    • variance_sum += (element − mean)²

  9. variance = variance_sum / N

  10. standard_deviation = √variance

  11. Display mean, variance, standard deviation

  12. Stop


Python Program

⚠ Students must complete the missing parts.

# Program to calculate Mean, Variance and Standard Deviation


import math


n = int(input("Enter number of elements: "))


numbers = []


for i in range(n):

    value = float(input("Enter number: "))

    numbers.append(__________)


total = sum(__________)

mean = total / __________


variance_sum = 0


for num in numbers:

    variance_sum += (num - __________) ** 2


variance = variance_sum / __________


std_deviation = math.sqrt(____________)


print("Mean =", mean)

print("Variance =", variance)

print("Standard Deviation =", std_deviation)



Probable Output

Case 1

Enter number of elements: 5
Enter number: 2
Enter number: 4
Enter number: 6
Enter number: 8
Enter number: 10

Mean = 6.0
Variance = 8.0
Standard Deviation = 2.8284271247461903

Important Concepts Used

  • Lists

  • sum() function

  • Looping (for)

  • Arithmetic operations

  • math.sqrt()

  • Statistical formulas


Viva Voce Questions

  1. What is mean?

  2. What is variance?

  3. Why do we square the difference?

  4. Why is standard deviation square root of variance?

  5. Can variance be negative?

  6. What happens if N = 0?

  7. What is the time complexity of this program?



Procedure to Execute the Program

  1. Open Python IDLE / VS Code / Google Colab

  2. Create a new file

  3. Type the program

  4. Save with .py extension

  5. Run the program

  6. Enter N and the numbers



Google Colab – Online Execution

👉 Paste your Colab link here.

[https://colab.research.google.com/drive/1cUDbj_f5HZbs5k4mlgX5YLK3ONXcub2A?usp=sharing]



Program Explanation




Assignment for Students

  • Modify program to calculate sample variance

  • Use statistics module instead of manual calculation

  • Store results in a dictionary

  • Display results rounded to 2 decimal places



Lab Record Instructions

Right Side of the Record
1. Problem Statement
2. Python Program
Write the following neatly on the right-hand side page:


Left Side of the Record
Write the following neatly on the left-hand side page:
3. Flowchart
4. Algorithm
5. Output
⚠️ Students must write ALL possible outputs.



-:END:-



Comments

Popular posts from this blog