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 1227 of 3366
3K+ Views
A number is said to be a Perfect Number when that is equal to the sum of all its positive divisors except itself. When it is required to check if a number is a perfect number, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demon = 6 my_sum = 0 for i in range(1, n): if(n % i == 0): my_sum = my_sum + i if (my_sum == n): print("The number is a perfect number") else: print("The number is not a perfect number")OutputThe number is a perfect numberExplanationThe ... Read More
639 Views
When it is required to print the pascal’s triangle for a specific number of rows, where the number is entered by the user, a simple ‘for’ loop is used.Below is the demonstration of the same −Example Live Demofrom math import factorial input = int(input("Enter the number of rows...")) for i in range(input): for j in range(input-i+1): print(end=" ") for j in range(i+1): print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") print()OutputEnter the number of rows...6 1 1 1 1 2 1 1 3 3 1 ... Read More
2K+ Views
When it is required to convert binary code to gray code, a method is defined that performs the ‘xor’ operation.Below is the demonstration of the same −Example Live Demodef binary_to_gray_op(n): n = int(n, 2) n ^= (n >> 1) return bin(n)[2:] gray_val = input('Enter the binary number: ') binary_val = binary_to_gray_op(gray_val) print('Gray codeword is :', binary_val)OutputEnter the binary number: 101100110 Gray codeword is : 111010101ExplanationA method named ‘binary_to_gray_op’ is defined, that takes the binary number as its parameter.It performs the ‘xor’ operation.It returns the converted output.The input of binary number is taken from the user.The function ... Read More
1K+ Views
When it is required to convert gray code to binary code, a method is defined, that checks to see if the number is 0 or not.Below is the demonstration of the same −Example Live Demodef flip_num(my_nu): return '1' if(my_nu == '0') else '0'; def gray_to_binary(gray): binary_code = "" binary_code += gray[0] for i in range(1, len(gray)): if (gray[i] == '0'): binary_code += binary_code[i - 1] else: binary_code += flip_num(binary_code[i - 1]) return binary_code gray_code = "01101001" print("The gray code ... Read More
436 Views
When it is required to generate gray codes with the help of recursion, a method is defined, that creates an empty list and appends values 0 and 1 to it. Multiple ‘for’ loops are used to generate the gray code within the function.Below is the demonstration of the same −Example Live Demoimport math as mt def generate_gray_list(my_val): if (my_val = 1
432 Views
When it is required to clear the rightmost bit of a number which was previously set, the ‘&’ operator can be used.Below is the demonstration of the same −Example Live Demodef clear_right_bit(my_val): return my_val & (my_val-1) n_val = 6 print("The vlaue of n is :") print(n_val) print("The number after unsetting the rightmost set bit is ") print(clear_right_bit(n_val))OutputThe vlaue of n is : 6 The number after unsetting the rightmost set bit is 4ExplanationA method is defined that takes an integer as a parameter.It computes the ‘&’ operation between the number and the number decremented by 1.Outside the method, an integer ... Read More
402 Views
When it is required to search the frequency of a number in a list, a method is defined, that takes a list and the number. It iterates through the list, and every time the number is encountered, the counter is incremented.Below is the demonstration of the same −Example Live Demodef count_num(my_list, x_val): my_counter = 0 for elem in my_list: if (elem == x_val): my_counter = my_counter + 1 return my_counter my_list = [ 66, 26, 48, 140, 66, 20, 1, 96, 86] print("The list is :") print(my_list) occ_number = 66 ... Read More
2K+ Views
When it is required to determine the Pythagorean triplets within a given range, a method is defined, that helps calculate the triplet values.Below is the demonstration of the same −Example Live Demodef pythagorean_triplets(limits) : c, m = 0, 2 while c < limits : for n in range(1, m) : a = m * m - n * n b = 2 * m * n c = m * m + n * n if c > limits : break ... Read More
1K+ Views
When it is required to implement Euler’s number, a method is defined, that computes the factorial.Another method is defined that find the sum of these factorial numbers.Below is the demonstration of the same −Example Live Demodef factorial_result(n): result = 1 for i in range(2, n + 1): result *= i return result def sum_result(n): s = 0.0 for i in range(1, n + 1): s += 1.0 / factorial_result(i) print(s) my_value = 5 print("The value is :") print(my_value) print("The result is :") sum_result(my_value)OutputThe value is : ... Read More
566 Views
When it is required to print the numbers in a given range without using any loop, a method is defined, that keeps displays numbers from higher range by uniformly decrementing it by one after every print statement.Below is the demonstration of the same −Example Live Demodef print_nums(upper_num): if(upper_num>0): print_nums(upper_num-1) print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim)OutputThe upper limit is : 6 The numbers are : 1 2 3 4 5 6ExplanationA method named ‘print_nums’ is defined.It checks if the upper limit is greater than 0.If ... Read More