Monday, 28 December 2020

Python Program to Find the Sum of First N Natural Numbers

 This is a 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
 
Case 2:
Enter a number: 167
The sum of first n natural numbers is 14028

Python Program to Find the Sum of Cosine Series

 This is a 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.
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))/math.factorial(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

Python Program to Print the Pascal’s triangle for n number of rows given by the user

 This is a Python Program to print the pascal’s triangle for n number of rows given by the user.

Problem Description

The program takes a number n and prints the pascal’s triangle having n number of rows.

Problem Solution

1. Take in the number of rows the triangle should have and store it in a separate variable.
2. Using a for loop which ranges from 0 to n-1, append the sub-lists into the list.
3. Then append 1 into the sub-lists.
4. Then use a for loop to determine the value of the number inside the triangle.
5. Print the Pascal’s triangle according to the format.
6. Exit.

Program/Source Code

Here is source code of the Python Program to print the pascal’s triangle for n number of rows given by the user. The program output is also shown below.

n=int(input("Enter number of rows: "))
a=[]
for i in range(n):
    a.append([])
    a[i].append(1)
    for j in range(1,i):
        a[i].append(a[i-1][j-1]+a[i-1][j])
    if(n!=0):
        a[i].append(1)
for i in range(n):
    print("   "*(n-i),end=" ",sep=" ")
    for j in range(0,i+1):
        print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
    print()
Program Explanation

1. User must enter the number of rows that the Pascal’s triangle should have.
2. The for loop is used to append sub-lists into an empty list defined earlier.
3. Then 1 is appended into all the sub-lists.
4. The for loop is used to determine the value of the number inside the triangle which is the sum of the two numbers above it.
5. The other for loop is used to print the Pascal’s triangle according to the format.

Runtime Test Cases
Case 1:	
Enter number of rows: 3
               1 
            1      1 
         1      2      1 
 
Case 2:
Enter number of rows: 4
                  1 
               1      1 
            1      2      1 
         1      3      3      1

Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List

 This is a Python Program to compute a polynomial equation given that the coefficients of the polynomial are stored in the list.

Problem Description

The program takes the coefficients of the polynomial equation and the value of x and gives the value of the polynomial.

Problem Solution

1. Import the math module.
2. Take in the coefficients of the polynomial equation and store it in a list.
3. Take in the value of x.
4. Use a for loop and while loop to compute the value of the polynomial expression for the first three terms and store it in a sum variable.
5. Add the fourth term to the sum variable.
6. Print the computed value.
7. Exit.

Program/Source Code

Here is source code of the Python Program to compute a polynomial equation given that the coefficients of the polynomial are stored in a list. The program output is also shown below.

import math
print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
lst=[]
for i in range(0,4):
    a=int(input("Enter coefficient:"))
    lst.append(a)
x=int(input("Enter the value of x:"))
sum1=0
j=3
for i in range(0,3):
    while(j>0):
        sum1=sum1+(lst[i]*math.pow(x,j))
        break
    j=j-1
sum1=sum1+lst[3]
print("The value of the polynomial is:",sum1)
Program Explanation

1. The math module is imported.
2. User must enter the coefficients of the polynomial which is stored in a list.
3. User must also enter the value of x.
4. The value of i ranges from 0 to 2 using the for loop which is used to access the coefficients in the list.
5. The value of j ranges from 3 to 1, which is used to determine the power for the value of x.
6. The value of the first three terms is computed this way.
7. The last term is added to the final sum.
8. The final computed value is printed.

Runtime Test Cases
 
Case 1:
Enter the coefficients of the form ax^3 + bx^2 + cx + d
Enter coefficient:3
Enter coefficient:4
Enter coefficient:5
Enter coefficient:6
Enter the value of x:2
The value of the polynomial is: 56.0
 
Case 2:
Enter the coefficients of the form ax^3 + bx^2 + cx + d
Enter coefficient:2
Enter coefficient:5
Enter coefficient:6
Enter coefficient:3
Enter the value of x:1
The value of the polynomial is: 16.0