Skip to main content

Python Array Solution Q1

 Write a program that takes five integers from the user, stores them in an array, and then prints the sum of these integers solved in Python.


from array import array

# Create an array of integers

int_array = array('i', [])


# Take five integer inputs from the user

for _ in range(5):

    user_input = int(input("Enter an integer: "))

    int_array.append(user_input)

# Calculate and print the sum of the integers in the array

sum_of_integers = sum(int_array)

print(f"The sum of the integers is: {sum_of_integers}")


In this program:


We import the array module.

An empty array of integers ('i' typecode) is created.

A loop is used to take five integer inputs from the user, and each input is appended to the array.

The sum() function is used to calculate the sum of the integers in the array.

The result is printed.

This program ensures that the user enters valid integers and calculates the sum of those integers using the array module.

Comments

Popular posts from this blog

Dear Students Welcome to my blog.  It is to inform you that. now you can download your 10th class Computer Science book in PDF. Share with your other students.  Kindly Share your comments on my blog to support me.                                                                                   Download

Class X Test / Model Paper Chapter No 1 Computer

                                                                                Subject: Computer Science                                          Class: -10                                        Date: 4 Jan 2023 Test   # 1   Short Test (MCQs=6, SQs=8, LQs=1)     ...

Inter Board Karachi Guess/Model Papers 2023 (1st year & 2nd year)

  Inter Board Karachi Guess/Model Papers 2023 Board Karachi Model Papers 2023 Computer's Guess Paper 9th Class 2023 Computer's Guess Paper 9th Class 2026 Computer's Guess Paper  10th Class 2023 Computer's Guess Paper 11th Class 2023 Computer's Guess Paper  12th Class 2023 Prepared By Sir Rana Arsalan Inter Part-I Model Papers Inter Part-II Model Papers Model Paper Mathematics I Model Paper Mathematics II Model Paper Islamiat Model Paper Urdu II Model Paper Urdu I Model Paper Pakistan Studies Model Paper Botany I Model Paper Botany II Model Paper Chem I — MCQs Model Paper Chem II — MCQs Model Paper Physics I Model Paper Physics II Model Paper Zoology I Model Paper Zoology II Model Paper English I Model Paper English II