AmitDiwan has Published 10740 Articles

Python Program to Read a Number n and Print the Natural Numbers Summation Pattern

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:09:38

444 Views

When it is required to read a number and print the pattern of summation of natural numbers, a simple ‘for’ loop can be used.Below is a demonstration of the same −Example Live Demomy_num = int(input("Enter a number... ")) for j in range(1, my_num+1):    my_list=[]    for i in range(1, j+1): ... Read More

Python Program to Read a Number n And Print the Series "1+2+…..+n= "

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:06:38

416 Views

When it is required to display the sum all the natural numbers within a given range, a method can be defined that uses a loop to iterate over the elements, and returns the sum of these numbers as output.Below is a demonstration of the same −Example Live Demodef sum_natural_nums(val):    my_sum ... Read More

Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:06:16

3K+ Views

When it is required to print all the elements which can’t be divided by 2 or 3 and lie between 1 and 50, the constraints are mentioned in the form of a ‘while’ loop and ‘if’ condition.Below is a demonstration of the same −Example Live Demoprint("Integers not divisible by 2 and ... Read More

Python Program to Find the Smallest Divisor of an Integer

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:05:59

3K+ Views

When it is required to find the smallest divisor of a an integer, a simple ‘for’ loop is used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter a number...")) my_list = [] print("The number is ") print(first_num) for i in range(2, first_num+1):    if(first_num%i==0):     ... Read More

Python Program to Read Two Numbers and Print Their Quotient and Remainder

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:05:07

2K+ Views

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") ... Read More

Python Program to Print all Numbers in a Range Divisible by a Given Number

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:00:39

2K+ Views

When it is required to print all the elements in a given range that is divisible by a specific number, a simple for loop can be used.Below is a demonstration of the same −Example Live Demolower_num = int(input("Enter lower range limit...")) upper_num = int(input("Enter upper range limit...")) div_num = int(input("Enter the ... Read More

Python Program to Input a Number n and Compute n+nn+nnn

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:00:20

2K+ Views

When it is required to take a number and compute a specific pattern, the value of n is taken from the user. Next, two variables are assigned this specific pattern and their sum is calculated.Below is a demonstration of the same −Example Live Demomy_input = int(input("Enter a value for n...")) temp_val ... Read More

Python program to sort the elements of an array in descending order

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:00:00

2K+ Views

When it is required to sort the elements of an array in descending order, the ‘sort’ method can be used by specifying a parameter named ‘reverse’ to True.Below is a demonstration of the same −Example Live Demomy_list = [44, 56, 42, 31, 11, 23, 78, 89, 9, 0] print("The list is ... Read More

Python program to sort the elements of an array in ascending order

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 11:58:28

880 Views

When it is required to sort the elements of an array in ascending order, the ‘sort’ method can be used. It helps sort the elements in ascending order by default. If we want it to be sorted in descending order, a parameter named ‘reverse’ can be set to True.Below is ... Read More

Python program to right rotate the elements of an array

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 11:58:06

555 Views

When it is required to right rotate the elements of a list, the elements are iterated over, and a last element is assigned a value, after which the elements are iterated over, and an element is swapped.Below is a demonstration of the same −Example Live Demomy_list = [31, 42, 13, 34, ... Read More

Advertisements