The Python program has to accept a list of integers from the user as an input to the program and then generate a histogram out of those numbers.
The histogram should be displayed as output of the python program.
Write A Python Program To Create A Histogram From A Given List Of Integers
Let's take a variable and and ask a user to enter the list size and then convert into an integer, using the for loop let's iterate from 0 to end and ask a user to enter a list item (an integer) and then append it to the list of numbers.
n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
temp = int(input("Enter List Item (Number) : "))
numbers.append(temp)
Using the print function call print a user-friendly message “histogram from a given list of integers”. Then using the for loop it iterate all the numbers present in the list of numbers and multiply a pattern that is “*” pattern with that number and print the pattern
print("Histogram From A Given List Of Integers ...")
for number in numbers:
pattern = '*' * number
print(pattern)
We will be using a string multiplyer to generate the pattern multiplied by number.
Complete python code to print the histogram from list on integers
==============================
n = int(input("Enter List Size : "))
numbers = []
for _ in range(n):
temp = int(input("Enter List Item (Number) : "))
numbers.append(temp)
print("Histogram From A Given List Of Integers ...")
for number in numbers:
pattern = '*' * number
print(pattern)
OUTPUT
=======================
Enter List Size : 5
Enter List Item (Number) : 1
Enter List Item (Number) : 2
Enter List Item (Number) : 3
Enter List Item (Number) : 4
Enter List Item (Number) : 5
Histogram From A Given List Of Integers ...
*
**
***
****
*****
OUTPUT
=======================
Enter List Size : 7
Enter List Item (Number) : 12
Enter List Item (Number) : 3
Enter List Item (Number) : 23
Enter List Item (Number) : 9
Enter List Item (Number) : 16
Enter List Item (Number) : 10
Enter List Item (Number) : 5
Histogram From A Given List Of Integers ...
************
***
***********************
*********
****************
**********
*****
Conclusion
================
Run the program by providing the Random numbers of integers as input. Note down the results.
Comment it down below if you have any suggestions to improve the program
0 Comments