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 25 of 2547
Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10
When it is required to find all numbers in a range that are perfect squares and the sum of digits in the number is less than 10, list comprehension is used. A perfect square is a number that can be expressed as the product of an integer with itself. Below is the demonstration of the same − Example lower_limit = 5 upper_limit = 50 numbers = [] numbers = [x for x in range(lower_limit, upper_limit + 1) if (int(x**0.5))**2 == x and sum(list(map(int, str(x)))) < 10] print("The result is:") print(numbers) Output The ...
Read MorePython Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number
When you need to create a list of tuples where each tuple contains a number and its square, Python provides several approaches. The most common methods are list comprehension, using loops, and the map() function. Using List Comprehension List comprehension provides a concise way to create the list of tuples ? numbers = [23, 42, 67, 89, 11, 32] print("The list is:") print(numbers) result = [(num, pow(num, 2)) for num in numbers] print("The resultant tuples are:") print(result) The list is: [23, 42, 67, 89, 11, 32] The resultant tuples are: [(23, 529), ...
Read MorePython Program to Find the Second Largest Number in a List Using Bubble Sort
Finding the second largest number in a list can be accomplished using bubble sort to first arrange elements in ascending order, then accessing the second-to-last element. Bubble sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. Understanding Bubble Sort Bubble sort works by comparing each pair of adjacent elements and swapping them if the first element is greater than the second. This process continues until no more swaps are needed. Bubble Sort Process Initial: ...
Read MorePython Program to Merge Two Lists and Sort it
When it is required to merge two lists and sort them, Python provides several approaches. You can use list concatenation with the sort() method, or combine merging and sorting in one step using sorted(). Below is the demonstration of the same − Method 1: Using List Concatenation and sort() Example Define a function that merges two lists and sorts them in-place − def merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return merged_list numbers_1 = [20, 18, 9, 51, 48, 31] ...
Read MorePython Program to Find the Gravitational Force Acting Between Two Objects
When calculating the gravitational force between two objects, we can use Newton's Law of Universal Gravitation. This law states that every particle attracts every other particle with a force proportional to the product of their masses and inversely proportional to the square of the distance between them. The formula is: F = G × (m₁ × m₂) / r², where G is the gravitational constant (6.673 × 10⁻¹¹ N⋅m²/kg²). Example Here's how to calculate gravitational force using a Python function ? def find_gravity(m_1, m_2, r): G_val = 6.673*(10**-11) # Gravitational ...
Read MorePython Program to Check If Two Numbers are Amicable Numbers
Amicable numbers are two different numbers where the sum of proper divisors of each number equals the other number. For example, 220 and 284 are amicable because the proper divisors of 220 sum to 284, and the proper divisors of 284 sum to 220. What are Proper Divisors? Proper divisors are all positive divisors of a number except the number itself. For example, proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. Method 1: Using Mathematical Approach This method finds divisors efficiently by checking only up to the square ...
Read MorePython Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List
When you need to compute a polynomial equation where the coefficients are stored in a list, you can use nested loops to calculate each term. A polynomial like 2x³ + 5x² + 3x + 0 can be evaluated by multiplying each coefficient by the corresponding power of x. Understanding Polynomial Evaluation For a polynomial represented as a list [2, 5, 3, 0], this corresponds to: 2x³ (coefficient 2, power 3) 5x² (coefficient 5, power 2) 3x¹ (coefficient 3, power 1) 0x⁰ (coefficient 0, power 0) Example Here's how to evaluate the polynomial when ...
Read MorePython Program to Check if a Number is a Strong Number
A strong number is a number whose sum of all digits' factorial equals the original number. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145. We can check this using the modulus operator and loops to extract digits and calculate factorials. Example Let's check if 145 is a strong number ? def factorial(n): if n == 0 or n == 1: return 1 fact = 1 ...
Read MorePython Program to Check if a Number is a Perfect Number
A Perfect Number is a positive integer that equals the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6. The first few perfect numbers are 6, 28, 496, and 8128. Basic Method to Check Perfect Number We can iterate through all numbers from 1 to n-1 and check if they divide n evenly ? n = 6 my_sum = 0 for i in range(1, n): if n % i == 0: ...
Read MorePython Program to Convert Binary to Gray Code
When it is required to convert binary code to gray code, a method is defined that performs the XOR operation. Gray code is a binary numeral system where two successive values differ in only one bit, making it useful in digital circuits and error correction. Below is the demonstration of the same − Understanding Gray Code Conversion The conversion from binary to Gray code follows a simple rule: The most significant bit of Gray code is the same as the binary code For other bits, the Gray code bit is the XOR of the current ...
Read More