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

Chapter 5 Simple Python Programs

The document contains multiple Python programs that demonstrate various mathematical concepts, including checking for prime numbers, printing numbers in a range using recursion, calculating sums of sine and cosine series, and finding Pythagorean triplets. Each program includes a problem description, solution steps, source code, and runtime test cases. The programs are designed for educational purposes and provide practical examples of coding in Python.
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)
25 views10 pages

Chapter 5 Simple Python Programs

The document contains multiple Python programs that demonstrate various mathematical concepts, including checking for prime numbers, printing numbers in a range using recursion, calculating sums of sine and cosine series, and finding Pythagorean triplets. Each program includes a problem description, solution steps, source code, and runtime test cases. The programs are designed for educational purposes and provide practical examples of coding in Python.
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

Leela Soft 1000 MCQ Programs Madhu

2. The first for loop ranges till the upper limit entered by the user.
3. The count variable is first initialized to 0.
4. The for loop ranges from 2 to the half of the number so 1 and the number itself aren’t
counted as divisors.
5. The if statement then checks for the divisors of the number if the remainder is equal to
0.
6. The count variable counts the number of divisors and if the count is lesser or equal to 0,
the number is a prime number.
7. If the count is greater than 0, the number isn’t prime.
8. The final result is printed.

Runtime Test Cases


Case 1:
Enter upper limit: 15
2
3
5
7
11
13

Case 2:
Enter upper limit: 40
2
3
5
7
11
13
17
19
23
29
31
37

Python Program to Print Numbers in a Range (1, upper) Without Using any Loops
Problem Description
The program takes in the upper limit and prints all numbers within the given range.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Problem Solution
1. Define a recursive function.
2. Define a base case for that function that the number should be greater than zero.
3. If number is greater than 0, call the function again with the argument as the number
minus 1.
4. Print the number.
5. Exit.

Program/Source Code
Here is source code of the Python Program to check if a number is a prime number. The program
output is also shown below.

def printno(upper):
if(upper > 0):
printno(upper - 1)
print(upper)

upper = int(input("Enter upper limit: "))


printno(upper)

Program Explanation
1. User must enter the upper limit of the range.
2. This value is passed as an argument for the recursive function.
3. The base case for the recursive function is that number should always be greater than 0.
4. If number is greater than 0, function is called again with the argument as the number
minus 1.
5. The number is printed.
6. The recursion continues until the number becomes lesser than 0.

Runtime Test Cases


Case 1:
Enter upper limit: 5
1
2
3
4
5

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter upper limit: 15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Python Program to Find the Sum of Sine Series


Problem Description
The program takes in the the number of terms and finds the sum of sine series.

Problem Solution
1. Take in the value of x in degrees and the number of terms and store it in separate
variables.
2. Pass these values to the sine function as arguments.
3. Define a sine function and using a for loop, first convert degrees to radians.
4. Then use the sine formula expansion and add each term to the sum variable.
5. Then print the final sum of the expansion.
6. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of sine series. The program output is
also shown below.

import math

def sin(x, n):


sine = 0
for i in range(n):

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
sign = (-1) ** i
pi = 22 / 7
y = x * (pi / 180)
sine = sine + ((y ** (2.0 * i + 1)) / [Link](2 * i + 1)) * sign
return sine

x = int(input("Enter the value of x in degrees:"))


n = int(input("Enter the number of terms:"))
print(round(sin(x, n), 2))

Program Explanation
1. User must enter the value of x in degrees and the number of terms and store it in separate
variables.
2. These values are passed to the sine functions as arguments.
3. A sine function is defined and a for loop is used convert degrees to radians and find the
value of each term using the sine expansion formula.
4. Each term is added the sum variable.
5. This continues till the number of terms is equal to the number given by the user.
6. The total sum is printed.

Runtime Test Cases


Case 1:
Enter the value of x in degrees:30
Enter the number of terms:10
0.5

Case 2:
Enter the value of x in degrees:45
Enter the number of terms:15
0.71

Python Program to Find the Sum of Cosine Series


Problem Description
The program takes in the the number of terms and finds the sum of cosine series.

Problem Solution
1. Take in the value of x in degrees and the number of terms and store it in separate
variables.
2. Pass these values to the cosine function as arguments.
3. Define a cosine function and using a for loop which iterates by 2 steps, first convert
degrees to radians.
4. Then use the cosine formula expansion and add each term to the sum variable.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

5. Then print the final sum of the cosine expansion.


6. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of cosine series. The program output
is also shown below.

import math

def cosine(x, n):


cosx = 1
sign = -1
for i in range(2, n, 2):
pi = 22 / 7
y = x * (pi / 180)
cosx = cosx + (sign * (y ** i)) / [Link](i)
sign = -sign
return cosx

x = int(input("Enter the value of x in degrees:"))


n = int(input("Enter the number of terms:"))
print(round(cosine(x, n), 2))

Program Explanation
1. User must enter the value of x in degrees and the number of terms and store it in separate
variables.
2. These values are passed to the cosine functions as arguments.
3. A cosine function is defined and a for loop is used convert degrees to radians and find the
value of each term using the sine expansion formula.
4. Each term is added the sum variable.
5. This continues till the number of terms is equal to the number given by the user.
6. The total sum is printed.

Runtime Test Cases


Case 1:
Enter the value of x in degrees:0
Enter the number of terms:10
1.0

Case 2:
Enter the value of x in degrees:75
Enter the number of terms:15
0.26

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Python Program to Find the Sum of First N Natural Numbers


Problem Description
The program takes in the the number of terms and finds the sum of first N Natural Numbers.

Problem Solution
1. Take in the number of natural numbers to find the sum of and store it in a separate
variable.
2. Initialize the sum variable to 0.
3. Use a while loop to find the sum of natural numbers and decrement the number for each
iteration.
4. The numbers are added to the sum variable and this continues until the the value of the
number is greater than 0.
5. Then the sum of first N natural numbers is printed.
6. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of first N Natural Numbers. The
program output is also shown below.

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


sum1 = 0
while(n > 0):
sum1 = sum1 + n
n = n - 1
print("The sum of first n natural numbers is", sum1)

Program Explanation
1. User must enter the number of natural numbers to find the sum of.
2. The sum variable is initialized to 0.
3. The while loop is used to find the sum of natural numbers and the number is decremented
for each iteration.
4. The numbers are added to the sum variable and this continues till the value of the variable
is greater than 0.
5. When the value of the variable becomes lesser than 0, the total sum of N natural numbers
is printed.

Runtime Test Cases


Case 1:
Enter a number: 18
The sum of first n natural numbers is 171

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter a number: 167
The sum of first n natural numbers is 14028

Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N
Problem Description
The program takes in the the number of terms and finds the sum of series: 1 + 1/2 + 1/3 + ….. +
1/N.

Problem Solution
1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 0.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.
The program output is also shown below.

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


sum1 = 0
for i in range(1, n + 1):
sum1 = sum1 + (1 / i)
print("The sum of series is", round(sum1, 2))

Program Explanation
1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 0.
3. The for loop is used to find the sum of the series and the number is incremented for each
iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches
the number of terms.
5. Then the sum of the series is printed.

Runtime Test Cases


Case 1:
Enter the number of terms: 7

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

The sum of series is 2.59

Case 2:
Enter the number of terms: 15
The sum of series is 3.32

Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n
Problem Description
The program takes in the the number of terms and finds the sum of series: 1 + x^2/2 + x^3/3 + …
x^n/n.

Problem Solution
1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 0.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.
The program output is also shown below.

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


x = int(input("Enter the value of x:"))
sum1 = 1
for i in range(2, n + 1):
sum1 = sum1 + ((x ** i) / i)
print("The sum of series is", round(sum1, 2))

Program Explanation
1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 0.
3. The for loop is used to find the sum of the series and the number is incremented for each
iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches
the number of terms.
5. Then the sum of the series is printed.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Runtime Test Cases


Case 1:
Enter the number of terms:3
Enter the value of x:1
The sum of series is 1.83

Case 2:
Enter the number of terms:5
Enter the value of x:2
The sum of series is 16.07

Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! +
1/2! + …… 1/n!
Problem Description
The program takes in the the number of terms and finds the sum of series: 1 + 1/2 + 1/3 + ….. +
1/N.

Problem Solution
1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 1.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.

Program/Source Code
Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.
The program output is also shown below.

import math
n = int(input("Enter the number of terms: "))
sum1 = 1
for i in range(1, n + 1):
sum1 = sum1 + (1 / [Link](i))
print("The sum of series is", round(sum1, 2))

Program Explanation
1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 1.

[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

3. The for loop is used to find the sum of the series and the number is incremented for each
iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches
the number of terms.
5. Then the sum of the series is printed.

Runtime Test Cases


Case 1:
Enter the number of terms: 5
The sum of series is 2.72

Case 2:
Enter the number of terms: 20
The sum of series is 2.72

Python Program to Determine all Pythagorean Triplets in the Range


Problem Description
The program takes a upper limit and determines all Pythagorean triplets till the upper limit.

Problem Solution
1. Take in the upper limit and store it in a variable.
2. Using a while loop and for loop, compute the Pythagorean triplets using the formula.
3. If the value of the c is greater than the upper limit or if any of the numbers is equal to 0,
break from the loop.
4. Print all the three numbers of the Pythagorean triplets.
5. Exit.

Program/Source Code
Here is source code of the Python Program to determine all Pythagorean triplets till the upper
limit. The program output is also shown below.

limit = int(input("Enter upper limit:"))


c = 0
m = 2
while(c < limit):
for n in range(1, m + 1):
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if(c > limit):
break
if(a == 0 or b == 0 or c == 0):

[Link] Cell: 78 42 66 47 66

You might also like