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
Articles by Pavitra
Page 5 of 13
Python Program for Tower of Hanoi
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given n disks and a series of rods, we need to transfer all the disks to the final rod under the given constraints−We can move only one disk at a time.Only the uppermost disk from the rod can be moved.Any bigger disk cannot be placed on the smaller diskNow let’s observe the solution in the implementation below −Example# tower of hanoi def TowerOfHanoi(n , from_rod, to_rod, aux_rod): if n == 1: print ("Move disk 1 from rod", from_rod, ...
Read MorePython Program for Triangular Matchstick Number
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number X which represents the floor of a matchstick pyramid, we need to display the total number of matchstick required to form a pyramid of matchsticks with x floors.Now let’s observe the solution in the implementation below −Example#function def numberOfSticks(x): return (3 * x * (x + 1)) / 2 # main() n=21 a=numberOfSticks(n) print(int(a))Output693All the variables are declared in the local scope and their references are seen in the figure above.ConclusionIn this article, we have learned about ...
Read MorePython Program to Print Numbers in an Interval
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven the starting and ending range of an interval. We need to print all the numbers in the interval given.A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.There are two for loops, first for loop is for getting the numbers in the interval and second loop for the checking whether the number is prime or not.Now let’s see the implementation.Examplestart = 10 end = 29 for val in range(start, end + 1): ...
Read MorePython program to print odd numbers in a list
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a list iterable as input, we need to display odd numbers in the given iterable.Here we will be discussing three different approaches to solve this problem.Approach 1 − Using enhanced for loopExamplelist1 = [11, 23, 45, 23, 64, 22, 11, 24] # iteration for num in list1: # check if num % 2 != 0: print(num, end = " ")Output11, 23, 45, 23, 11Approach 2 − Using lambda & filter functionsExamplelist1 = [11, 23, 45, 23, 64, ...
Read MorePython Program to find the area of a circle
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given the radius of a circle, we need to find a circle.The area of a circle can simply be evaluated using the following formula.Area = Pi*r*rLet’s see the implementation below −Exampledef findArea(r): PI = 3.142 return PI * (r*r); # Driver method print("Area is %.6f" % findArea(5));OutputArea is 78.550000All variables and functions are declared in the global scope as shown in the figure below.ConclusionIn this article, we learned about the approach to find whether it is possible to make a ...
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 about the solution and approach to solve the given problem statement.Problem statement −Given an integer input n, we need to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!Here we are implementing for loop, therefore, we get O(n) as the time complexity.Here to reach the efficiency we calculate factorial within the same loop.Here we frame a sumofseries function as described below −Exampledef sumOfSeries(num): res = 0 fact = 1 for i in range(1, num+1): fact *= i res = ...
Read MorePython Program to Print Matrix in Z form
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given a square matrix of order n*n, we need to display elements of the matrix in Z form.Z form is traversing the matrix in the following steps −Traverse the first rowNow, traverse the second principal diagonalFinally, traverse the last row.We will take an input matrix here implicitly taken to demonstrate the flow of code.demostrateExamplearr = [[1, 2, 6, 9], [1, 2, 3, 1], [7, 1, 3, 5], [1, 8, 7, 5]] n = len(arr[0]) i = 0 for ...
Read MorePython Program for compound interest
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −We are given with three input values i.e principle, rate & time and we need to compute compound interest.The code given below shows the process of computation of compound interest.The formula used here isCompound Interest = P(1 + R/100)rWhere, P is the principal amountR is the rate andT is the time spanThe implementation is given belowExampledef compound_interest(principle, rate, time): CI = principle * (pow((1 + rate / 100), time)) print("Compound interest : ", CI) # main compound_interest(10000, 7.78, 2)OutputCompound interest ...
Read MorePython Program for cube sum of first n natural numbers
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an input n, we need to print the sum of series 13 + 23 + 33 + 43 + …….+ n3 till n-th term.Here we will discuss two approach to reach the solution of the problem statement −Brute-force approach using loops.Mathematical solution of sum of n numbers.Approach 1 −Computing sum of each term by adding by iterating over the numbersExampledef sumOfSeries(n): sum = 0 for i in range(1, n+1): sum +=i*i*i return sum # Driver ...
Read MorePython Program for Difference between sums of odd and even digits
In this article, we will learn about the solution and approach to solve the given problem statement.Problem statement −Given an integer, we need to calculate if the difference between the sum of odd digits and sum of even digits is 0 or not.The brute-force approach will be calculating the sum of all even and odd digits in the numbers and subtracting them to compute the answer.To reduce the computational time we use the concept of mental mathematics .The above constraints holds true only if the numbers are divisible by 11. So here in the implementation given below we check the ...
Read More