Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

for loop

The document contains Python code snippets for various programming tasks, including calculating the sum and average of a list, counting elements divisible by 5, displaying the first N natural numbers, calculating the sum of numbers from 1 to a given number, calculating the factorial of a number, and printing the multiplication table of a given number. Each task is presented with input prompts and output statements. The code demonstrates basic programming concepts and operations in Python.

Uploaded by

hetanshi rana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

for loop

The document contains Python code snippets for various programming tasks, including calculating the sum and average of a list, counting elements divisible by 5, displaying the first N natural numbers, calculating the sum of numbers from 1 to a given number, calculating the factorial of a number, and printing the multiplication table of a given number. Each task is presented with input prompts and output statements. The code demonstrates basic programming concepts and operations in Python.

Uploaded by

hetanshi rana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q1

numbers = [10, 20, 30, 40, 50]

sum_of_numbers = sum(numbers)

avg_of_numbers = sum_of_numbers / len(numbers)

print("Sum:", sum_of_numbers)

print("Average:", avg_of_numbers)

q2

a=[10,15,32,40,22,88]

count = sum(1 for x in a if x % 5 != 0)

print("count_divisible_by_five", count)

q3

n = int(input("enter value of n"))

sum=sum(range(1,n+1))

print("sum of first",n,"num is ",sum)

q4

n=int(input("enter the number for factorial = "))

# Initialize the factorial variable to 1

factorial = 1

if n ==0 or n==1

print("value of factorial = 1")

else:

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

factorial = factorial * i
# Output: The factorial of the number

print(f"The factorial of {n} is {factorial}")

q5

num = int(input("enter number for which you need table"))

# To take input from the user

# num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10

for i in range(1, 11):

print(num, 'x', i, '=', num*i)


##Q1 Find sum,average of given list

# List of numbers

numbers = [10, 20, 30, 40, 50]

# Calculate the sum

total = sum(numbers)

# Calculate the average

average = total / len(numbers)

# Display the results

print("Sum:", total)

print("Average:", average)

##Q2 Count elements divisible by 5 in list

# List of numbers

a = [10, 15, 32, 40, 22, 88]

# Count elements divisible by 5

count = sum(1 for num in a if num % 5 == 0)

# Display the result

print("Count of elements divisible by 5:", count)

#Q3 Write a program to display the first N natural numbers.

# Take input for N

n = int(input("Enter the value of N: "))

# Display the first N natural numbers

print("First", n, "natural numbers:")

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

print(i, end=" ")

#Q4 Calculate sum of all numbers from 1 to a given number


# Take input for the number

n = int(input("Enter a number: "))

# Calculate the sum using the formula

total = n * (n + 1) // 2

# Display the result

print(f"The sum of numbers from 1 to {n} is: {total}")

#Q5 Calculate factorial of given number

# Take input for the number

n = int(input("Enter a number: "))

# Initialize factorial to 1

factorial = 1

# Calculate factorial using a for loop

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

factorial *= i

# Display the result

print(f"The factorial of {n} is: {factorial}")

#Q6 Print multiplication table of a given number

# Take input for the number

n = int(input("Enter a number: "))

# Print the multiplication table

print(f"Multiplication Table of {n}:")

for i in range(1, 11):

print(f"{n} x {i} = {n * i}")

You might also like