Programming assignment - Week 2
Programming assignment - Week 2
Solutions
Programming assignment - 1
Create a Python program that calculates the absolute value of a given number.
The program should prompt the user to input a number, then compute and print the absolute value of that number.
Input Format-
The input consists of single number.
Output Format-
The output consists of absolute value of the input.
Example-
Input:
-6
Output:
6
Code:
number = int(input())
absolute_value = abs(number)
print(absolute_value,end="")
Programming assignment - 2
Create a Python program to determine the nth element of an arithmetic sequence where the first term a is 7 and the common difference 𝑑 is 11. The program should prompt the user to input the value of n, then compute and print the nth element of the sequence.
Input Format:
The input consists of a single integer n.
Output Format:
The output consists of the nth element of the arithmetic sequence.
Example:
Input:
3
Output:
29
Code:
n = int(input())
element = a + (n - 1) * d
print(element,end="")
Programming assignment - 3
Create a Python program to compute the sum of the first N natural numbers {1,2,3...N}. The program should prompt the user to input the value of N, then compute and print the sum of the first N natural numbers.
Input Format:
The input consists of a single integer N.
Output Format:
The output consists of the sum of the first N natural numbers.
Example:
Input-
5
Output-
15
Code:
# Write your code here
n = int(input())
sum = 0
for i in range(n + 1):
sum += i
print(sum,end="")
Comments
Post a Comment