Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 392 of 2547
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
When it is required to read a number and print the pattern of summation of natural numbers, a simple for loop can be used. This pattern displays progressive sums like "1 = 1", "1 + 2 = 3", "1 + 2 + 3 = 6", and so on. Example my_num = int(input("Enter a number... ")) for j in range(1, my_num + 1): my_list = [] for i in range(1, j + 1): print(i, sep=" ", end=" ") ...
Read MorePython Program to Read a Number n And Print the Series "1+2+.....+n=
When it is required to display the sum of all natural numbers within a given range and show the series format "1+2+...+n=sum", we can create a program that builds the series string and calculates the sum. Below is a demonstration of the same − Method 1: Using Loop to Build Series Example def print_series_sum(n): series = "" total = 0 for i in range(1, n + 1): total += i ...
Read MorePython Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
When it is required to print all integers which are not divisible by either 2 or 3 and lie between 1 and 50, we can use a while loop with conditional statements to check each number. Numbers that satisfy both conditions (not divisible by 2 and not divisible by 3) will be printed. Example The following example demonstrates how to find integers between 1 and 50 that are not divisible by either 2 or 3 ? print("Integers not divisible by 2 and 3, that lie between 1 and 50 are:") n = 1 while n
Read MorePython Program to Find the Smallest Divisor of an Integer
When it is required to find the smallest divisor of an integer, a simple 'for' loop is used. The smallest divisor (other than 1) is the first number that divides the given integer evenly. Example Below is a demonstration of finding the smallest divisor ? first_num = int(input("Enter a number...")) divisors = [] print("The number is") print(first_num) for i in range(2, first_num + 1): if(first_num % i == 0): divisors.append(i) divisors.sort() print("The smallest divisor is :") print(divisors[0]) Output ...
Read MorePython Program to Accept Three Digits and Print all Possible Combinations from the Digits
When generating all possible combinations of three digits, we can use nested loops to create permutations where each digit appears exactly once in different positions. This approach uses three nested loops with conditional checks. Example first_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) third_num = int(input("Enter the third number...")) digits = [] print("The first number is") print(first_num) print("The second number is") print(second_num) print("The third number is") print(third_num) digits.append(first_num) digits.append(second_num) digits.append(third_num) print("All possible combinations:") for i in range(0, 3): for j in range(0, 3): ...
Read MorePython Program to Read Two Numbers and Print Their Quotient and Remainder
When dividing two numbers in Python, we often need both the quotient and remainder. Python provides the floor division operator (//) for quotient and the modulus operator (%) for remainder. Understanding Division Operators The floor division operator // returns the largest integer less than or equal to the division result, while the modulus operator % returns the remainder after division. 17 ÷ 5 = ? 17 // 5 = 3 (quotient) 17 % 5 = 2 (remainder) Verification: 3 × 5 + 2 = 17 ✓ ...
Read MorePython Program to Print all Numbers in a Range Divisible by a Given Number
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 along with the modulus operator. Below is a demonstration of the same − Example # Define range and divisor lower_num = 10 upper_num = 50 div_num = 7 print(f"Numbers between {lower_num} and {upper_num} divisible by {div_num}:") for i in range(lower_num, upper_num + 1): if i % div_num == 0: print(i) Numbers between 10 ...
Read MorePython Program to Input a Number n and Compute n+nn+nnn
In this tutorial, we'll write a Python program to input a number n and compute the sum n + nn + nnn. For example, if n = 4, we calculate 4 + 44 + 444 = 492. The key approach is to convert the number to a string and concatenate it to create the required patterns. Example Here's the complete program ? n = int(input("Enter a value for n: ")) n_str = str(n) # Create the patterns: nn and nnn nn = n_str + n_str nnn = n_str + n_str + n_str # ...
Read MorePython program to print the elements of an array present on even position
When it is required to print the elements of a list that are present at even index/position, a loop can be used to iterate over the elements, and only check the even positions in the list by specifying the step size as 2 in the range function. Below is a demonstration of the same − Example my_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is :") print(my_list) print("The elements in even positions are : ") for i in range(0, len(my_list), 2): print(my_list[i]) Output ...
Read MorePython program to print the elements of an array present on odd position
When we need to print elements at odd positions in a Python list, we can use a for loop with the range() function. By starting from index 1 and using a step size of 2, we can access only the odd-positioned elements. Using range() with Step Size The most common approach is to iterate through odd indices using range(1, len(list), 2) ? my_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is:") print(my_list) print("The elements in odd positions are:") for i in range(1, len(my_list), 2): print(my_list[i]) ...
Read More