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
Python Articles
Page 698 of 855
Python Program for cube sum of first n natural numbers
In this article, we will learn how to calculate the cube sum of first n natural numbers using two different approaches. The cube sum series is: 1³ + 2³ + 3³ + 4³ + ... + n³. Problem statement − Given an input n, we need to print the sum of series 1³ + 2³ + 3³ + 4³ + ... + n³ till n-th term. Here we will discuss two approaches to solve this problem ? Brute-force approach using loops Mathematical solution using the sum formula Using Loop (Iterative Approach) This approach ...
Read MorePython Program for compound interest
In this article, we will learn how to calculate compound interest using Python. Compound interest is the interest calculated on both the initial principal and the accumulated interest from previous periods. Problem statement − We are given three input values: principal amount, interest rate, and time period. We need to compute the compound interest. Formula The formula for calculating compound interest is: Compound Interest = P × (1 + R/100)^T Where: P is the principal amount (initial investment) R is the annual interest rate (in percentage) T is the time period (in ...
Read MorePython Program to Print Matrix in Z form
A Z-form traversal of a square matrix follows a specific pattern: traverse the first row, then the main diagonal (excluding elements already printed), and finally the last row. This creates a Z-like path through the matrix. Understanding Z-Form Pattern For a matrix of size n×n, Z-form traversal involves three steps − Print all elements of the first row Print diagonal elements from top-right to bottom-left (excluding corners) Print all elements of the last row ...
Read MorePython Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
In this article, we will learn how to calculate the sum of the mathematical series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!, where each term is the ratio of a number to its factorial. Problem statement − Given an integer input n, we need to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n! Understanding the Series This series has an interesting mathematical property − as n approaches infinity, the sum converges to the mathematical constant e (approximately 2.718). Each term follows the pattern i/i! where i ranges from ...
Read MorePython Program to find the area of a circle
In this article, we will learn how to calculate the area of a circle using Python. Given the radius of a circle, we need to find its area using a simple mathematical formula. The area of a circle can be calculated using the following formula ? Area = π × r² Where π (pi) is approximately 3.14159 and r is the radius of the circle. Method 1: Using a Simple Function Let's implement a basic function to calculate the area ? def findArea(r): PI = 3.14159 ...
Read MorePython Program for simple interest
In this article, we will learn how to calculate simple interest using Python. Simple interest is a method to calculate the interest charge on a loan or deposit. Simple interest is calculated by multiplying the principal amount by the interest rate and the time period. Unlike compound interest, it doesn't include interest on previously earned interest. Formula The mathematical formula for simple interest is: Simple Interest = (P × T × R) / 100 Where: P = Principal amount (initial money) T = Time period (in years) R = Rate of interest ...
Read MorePython Program for Selection Sort
In this article, we will learn about the selection sort algorithm and its implementation in Python. Selection sort is a simple comparison-based sorting algorithm that works by finding the minimum element from the unsorted portion and placing it at the beginning. How Selection Sort Works The selection sort algorithm maintains two subarrays during execution: The subarray which is already sorted (initially empty) The subarray which is unsorted (initially the entire array) During each iteration, selection sort finds the minimum element from the unsorted subarray and swaps it with the first element of the unsorted ...
Read MorePython Program for Product of unique prime factors of a number
In this article, we will learn how to find the product of all unique prime factors of a given number. For example, if the number is 12, its prime factors are 2 and 3, so the product would be 2 × 3 = 6. Problem statement − Given a number n, we need to find the product of all of its unique prime factors and return it. Example Input: num = 11 Output: Product is 11 Explanation: Here, the input number is 11 having only 1 prime factor and it is 11. And hence their product ...
Read MorePython Program for Print Number series without using any loop
In this article, we will learn how to print a number series without using any loop. We'll use recursion to subtract and add values alternately until we return to the original number. Problem statement − Given two numbers N and K, subtract K from N until N becomes negative or zero, then start adding K until we reach the original number N again. Example Output N = 10, K = 4 Output: 10 6 2 -2 2 6 10 Algorithm The approach uses recursion with a flag to control the direction: ...
Read MorePython Program for n-th Fibonacci number
In this article, we will compute the nth Fibonacci number using different approaches in Python. A Fibonacci number is defined by the recurrence relation given below − Fn = Fn-1 + Fn-2 With F0 = 0 and F1 = 1. The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... We can compute the Fibonacci numbers using recursion and dynamic programming methods. Let's explore both approaches with their implementations. Using Recursion Method The recursive approach directly implements the mathematical ...
Read More