Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1329 of 3366
2K+ Views
When it is required to find out if a number is a prime number or not using recursion technique, a method is defined, and the ‘while’ condition is used.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same −def check_prime(my_num, my_val = None): if my_val is None: my_val = my_num – 1 while my_val >= 2: if my_num % my_val == 0: print(“The number is not a prime number”) ... Read More
355 Views
When it is required to reverse a rotated array, a method is defined, that iterates through the list and reverse the list. Another method is defined, that rotates the list, and another method is defined that displays the list. A simple loop and indexing is used to achieve this.Below is a demonstration for the same −Example Live Demodef reverse_list(my_list, begin, end): while (begin < end): temp = my_list[begin] my_list[begin] = my_list[end] my_list[end] = temp begin += 1 end = end-1 def left_rotate(my_list, to_rotate): n ... Read More
1K+ Views
When it is required to determine if a given number is a Harshad number or not, a simple loop and ‘%’ operator, ‘+’ operator and ‘//’ operators can be used.A Harshad number is also known as a Niven number. It is a number whoses base is an integer that can be divided by the sum of its digits when it is written as that base value.Below is a demonstration for the same −Example Live Demomy_num = 134 remaining = sum_val = 0 print("A copy of the number to be checked is being made...") my_num_copy = my_num; while(my_num > 0): remaining ... Read More
755 Views
When it is required to print all the ahppy numbers between 1 and 100, a simple loop and operations like ‘%’, ‘+’, and ‘//’ are used.A happy number is the one that ends up as 1, when it is replaced by the sum of square of every digit in the number.To print the happy numbers between a given range, a simple loop can be used.Below is a demonstration for the same −Example Live Demodef check_happy_num(my_num): remaining = sum_val = 0 while(my_num > 0): remaining = my_num%10 sum_val = sum_val + (remaining*remaining) ... Read More
1K+ Views
When it is required to print all the disarium numbers between 1 and 100, a simple loop can be run between 1 and 100 and the length of every number can be calculated, and the power of the position can be multipled with the number itself.If they are equal, it is considered as a disarium number.A Disarium number is the one where the sum of its digits to the power of their respective position is equal to the original number itself.Below is a demonstration for the same −Example Live Demodef length_calculation(my_val): len_val = 0 while(my_val != 0): ... Read More
377 Views
When it is required to check if a given nmber is a disarium number, the sum of digits powered to their respective position is computed. Before this, the number of digits present in the number is determined.A Disarium Number is the one where the sum of its digits to the power of their respective position is equal to the original number itself.Below is a demonstration for the same −Example Live Demodef length_calculation(num_val): length = 0 while(num_val != 0): length = length + 1 num_val = num_val//10 return length my_num = 192 remaining ... Read More
794 Views
When it is required to find the sum of digits in a number without using the method of recursion, the ‘%’ operator, the ‘+’ operator and the ‘//’ operator can be used.Below is a demonstration for the same −Example Live Demodef sum_of_digits(my_num): sum_val = 0 while (my_num != 0): sum_val = sum_val + (my_num % 10) my_num = my_num//10 return sum_val my_num = 12345671 print("The number is : ") print(my_num) print("The method to calculate sum of digits is being called...") print("The sum of " +str(my_num) + " is : ") print(sum_of_digits(my_num))OutputThe number ... Read More
549 Views
When it is required to find all the numbers that are odd, and are palindromes and lie between a given range of values, and it has been told that recursion can’t be used, then, list comprehension, and ‘%’ operator can be used to achieve the same.Palindromes are string that are same when they are read in either way- left to right and right to left.Below is a demonstration for the same −Example Live Demomy_list = [] lower_limit = 5 upper_limit = 189 print("The lower limit is : ") print(lower_limit) print("The upper limit is : ") print(upper_limit) my_list = [x for x ... Read More
647 Views
When it is required to implement a binomial tree in Python, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations.Below is a demonstration for the same −Example Live Democlass binomial_tree: def __init__(self, key): self.key = key self.children = [] self.order = 0 def add_at_end(self, t): self.children.append(t) self.order = self.order + 1 my_tree = ... Read More
513 Views
When it is required to split the list, and then add this first part to the end of the list, a simple iteration through the list and list slicing is required.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration for the same −Example Live Demodef split_list(my_list, n_val, k_val): for i in range(0, k_val): first_val = my_list[0] for k in range(0, n_val-1): my_list[k] = my_list[k + 1] my_list[n_val-1] = first_val my_list ... Read More