0% found this document useful (0 votes)
19 views2 pages

Python Functions for Math Operations

The document contains Python code snippets for various mathematical operations including generating a multiplication table, summing numbers, summing even and odd numbers separately, counting factors, summing factors, checking for prime numbers, calculating factorials, and finding the maximum of five numbers. Each section includes user input prompts and prints the results of the calculations. The code is structured to handle basic arithmetic and number theory tasks.

Uploaded by

dokaniavishisht4
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)
19 views2 pages

Python Functions for Math Operations

The document contains Python code snippets for various mathematical operations including generating a multiplication table, summing numbers, summing even and odd numbers separately, counting factors, summing factors, checking for prime numbers, calculating factorials, and finding the maximum of five numbers. Each section includes user input prompts and prints the results of the calculations. The code is structured to handle basic arithmetic and number theory tasks.

Uploaded by

dokaniavishisht4
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

Multiplication Table

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


for i in range(1, 11):
print(f"{num} x {i} = {num*i}")

Sum of n Numbers
n = int(input("Enter the number of terms: "))
total = 0
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
total += num
print("Sum =", total)

Sum of Even Numbers


n = int(input("Enter the number of terms: "))
total = 0
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
if num % 2 == 0:
total += num
print("Sum of even numbers =", total)

Sum of Even and Odd Numbers Separately


n = int(input("Enter the number of terms: "))
even_sum = 0
odd_sum = 0
for i in range(n):
num = int(input(f"Enter number {i+1}: "))
if num % 2 == 0:
even_sum += num
else:
odd_sum += num
print("Even Sum =", even_sum)
print("Odd Sum =", odd_sum)

Count the Factors


num = int(input("Enter a number: "))
count = 0
for i in range(1, num + 1):
if num % i == 0:
count += 1
print("Number of factors =", count)

Sum of Factors
num = int(input("Enter a number: "))
total = 0
for i in range(1, num + 1):
if num % i == 0:
total += i
print("Sum of factors =", total)

Check Prime or Not


num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

Factorial
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact *= i
print("Factorial =", fact)

Maximum of 5 Numbers
nums = [int(input(f"Enter number {i+1}: ")) for i in range(5)]
print("Maximum number is", max(nums))

You might also like