0% found this document useful (0 votes)
9 views10 pages

4

Uploaded by

Falak Sher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

4

Uploaded by

Falak Sher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

4.

# initialize a variable to keep track of the sum


sum = 0

# loop through the numbers 1 to 10


for i in range(1, 11):
# display the current number
print(i)

# add the current number to the sum


sum += i

# display the total sum


print("The sum of the numbers is:", sum)

4.3

# initialize a variable to keep track of the sum


sum = 0

# loop through the odd numbers between 100 and 200


for i in range(101, 200, 2):
# add the current number to the sum
sum += i

# display the total sum


print("The sum of the odd numbers between 100 and 200 is:", sum)
4.4

# initialize a variable to keep track of the sum


sum = 0

# loop through the numbers from 10 to 75


for i in range(10, 76):
# check if the current number is a multiple of 7
if i % 7 == 0:
# add the current number to the sum
sum += i

# display the total sum


print("The sum of the multiples of 7 from 10 to 75 is:", sum)

4.5

# initialize the first two terms of the Fibonacci sequence


fib1, fib2 = 0, 1

# display the first two terms


print(fib1)
print(fib2)

# initialize a counter to keep track of the number of terms


count = 2

# loop to generate and display the next 18 terms


while count < 20:
# calculate the next term
fib = fib1 + fib2
# display the next term
print(fib)

# update the values of the previous two terms


fib1, fib2 = fib2, fib

# increment the counter


count += 1

4,6

# loop through the numbers from 100 to 999


for i in range(100, 1000):
# get the digits of the current number
digit1 = i // 100
digit2 = (i // 10) % 10
digit3 = i % 10

# calculate the product and sum of the digits


product = digit1 * digit2 * digit3
sum = digit1 + digit2 + digit3

# check if the product is 5 times the sum


if product == 5 * sum:
# display the current number
print(i)
4.8

# get the value of n from the user


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

# initialize the sum to 0


sum = 0

# loop through the numbers from 1 to n-1


for i in range(1, n):
# calculate the product of the current number and the next number
product = i * (i + 1)

# add the product to the sum


sum += product

# display the total sum


print("The sum of the series is:", sum)

4.9

# get the value of n from the user


n = int(input("Enter the number of terms: "))

# initialize the variables


sum = 0
num = 7

# loop through the first n terms


for i in range(n):
# add the current number to the sum
sum += num

# calculate the next number in the sequence


num = num * 10 + 7

# display the total sum


print("The sum of the first", n, "terms is:", sum)

4.10

# print the first series


for i in range(6):
if i % 2 == 0:
print("-1", end=",")
else:
print("1", end=",")

# print a newline character


print()

# print the second series


for i in range(5):
if i % 2 == 0:
print("1", end=",")
else:
print("-1", end=",")

# print a newline character


print()
4.2

# outer loop to control the number of rows


for i in range(1, 6):
# inner loop to control the number of columns
for j in range(i, 0, -1):
print(j, end="")
print()

4.11

# define the sequence of integers


sequence = [7, 678, 234, 315, 489, 536, 456, 367]

# calculate the sum of the sequence


total = sum(sequence)

# calculate the average of the sequence


average = total / len(sequence)

# print the sum and average of the sequence


print("The sum of the sequence is:", total)
print("The average of the sequence is:", average)
4.13

# prompt the user for n


n = int(input("Enter a value for n: "))

# initialize the sum to zero


total = 0

# loop through each number from 1 to n


for i in range(1, n+1):
# add the sum of the first i numbers to the total
total += sum(range(1, i+1))

# print the total sum


print("The total sum is:", total)

4.14

# prompt the user for a number


number = input("Enter a number: ")

# reverse the number


reverse = number[::-1]

# compare the number and its reverse


if number == reverse:
print("The number is a palindrome")
else:
print("The number is not a palindrome")
4.16

# prompt the user for n


n = int(input("Enter a value for n: "))

# print all numbers from 1 to n


print("All numbers from 1 to n:")
for i in range(1, n+1):
print(i, end=" ")
print()

# print even numbers from 1 to n


print("Even numbers from 1 to n:")
for i in range(2, n+1, 2):
print(i, end=" ")
print()

# print odd numbers from 1 to n


print("Odd numbers from 1 to n:")
for i in range(1, n+1, 2):
print(i, end=" ")
print()

4.17

# convert Fahrenheit to Celsius function


def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius

# convert temperatures from 20F to 50F to Celsius


for fahrenheit in range(20, 51):
celsius = fahrenheit_to_celsius(fahrenheit)
print(fahrenheit, "F = ", round(celsius, 2), "C")

4.18

# get the input from the user


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

# initialize variables for the sums


sum_n = 0
sum_n_squared = 0
sum_n_cubed = 0

# loop over the range of numbers from 1 to n


for i in range(1, n+1):
sum_n += i
sum_n_squared += i ** 2
sum_n_cubed += i ** 3

# print the results


print("The sum of the first", n, "natural numbers is:", sum_n)
print("The sum of the squares of the first", n, "natural numbers is:",
sum_n_squared)
print("The sum of the cubes of the first", n, "natural numbers is:",
sum_n_cubed)
4.20

# initialize the matrix with zeros


matrix = [[0 for j in range(3)] for i in range(4)]

# loop over the rows and columns and calculate the value of each
element
for i in range(4):
for j in range(3):
matrix[i][j] = (i + j + 2) / (j + 1) ** 2

# print the matrix


for row in matrix:
print(row)

You might also like