Programming Articles - Page 2409 of 3363

Check if a large number is divisible by 11 or not in C++

Akansha Kumari
Updated on 30-Jul-2025 15:19:13

665 Views

In this article, we are given a larger number and we need to check whether it is divisible by 11 or not using the C++ program. To handle large numbers, we treat the number as a string and apply a divisibility rule to check if it's divisible by 11. Rule of Divisibility by 11 A number is divisible by 11 if the difference between the sum of its digits at odd positions and the sum of its digits at even positions is divisible by 11. Consider the following example scenarios to understand the divisibility of a large number ... Read More

Check if a large number is divisibility by 15 in C++

Arnab Chakraborty
Updated on 27-Sep-2019 07:48:52

191 Views

Here we will see how to check a number is divisible by 15 or not. In this case the number is very large number. So we put the number as string.To check whether a number is divisible by 15, if the number is divisible by 5, and divisible by 3. So to check divisibility by 5, we have to see the last number is 0 or 5. To check divisibility by 3, we will see the sum of digits are divisible by 3 or not.Example Live Demo#include using namespace std; bool isDiv15(string num){    int n = num.length();    if(num[n ... Read More

Check if a given string is made up of two alternating characters in C++

Arnab Chakraborty
Updated on 27-Sep-2019 07:43:32

478 Views

Here we will see how to check a string is made up of alternating characters or not. If a string is like XYXYXY, it is valid, if a string is like ABCD, that is invalid.The approach is simple. We will check whether all ith character and i+2 th character are same or not. if they are not same, then return false, otherwise return true.Example Live Demo#include using namespace std; bool hasAlternateChars(string str){    for (int i = 0; i < str.length() - 2; i++) {       if (str[i] != str[i + 2]) {          return ... Read More

Python program to print all even numbers in a range

Pavitra
Updated on 27-Sep-2019 08:01:48

2K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a range, we need to print all the even numbers in the given range.The brute-force approach is discussed below −Here we apply a range-based for loop which provides all the integers available in the input interval.After this, a check condition for even numbers is applied to filter all the odd numbers.This approach takes O(n) + constant time of comparison.Now let’s see the implementation below −Examplestart, end = 10, 29 # iteration for num in range(start, end + 1):    # check   ... Read More

Python Program to find whether a no is the power of two

Pavitra
Updated on 27-Sep-2019 07:56:11

284 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a number n, we need to check whether the given number is a power of two.ApproachContinue dividing the input number by two, i.e, = n/2 iteratively.We will check In each iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2.If n becomes 1 then it is a power of 2.Let’s see the implementation below −Exampledef isPowerOfTwo(n):    if (n == 0):       return False    while (n != 1):       ... Read More

Custom instance creator using Gson in Java?

Aishwarya Naglot
Updated on 12-May-2025 16:19:13

2K+ Views

While parsing a JSON String to or from a Java object, by default, Gson tries to create an instance of the Java class by calling the default constructor. In the case of Java, if a class doesn’t contain a default constructor or we want to do some initial configuration while creating Java objects, we need to create and register our own instance creator. Custom Instance Creator Using Gson Custom instance means creating a new instance that is not the default. In this instance, we can add whatever properties we want. There are many libraries that create custom instances. In this ... Read More

Python program to find the most occurring character and its count

Pavitra
Updated on 26-Sep-2019 14:31:24

506 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an input string we need to find the most occurring character and its count.ApproachCreate a dictionary using Counter method having strings as keys and their frequencies as values.Find the maximum occurrence of a character i.e. value and get the index of it.Now let’s see the implementation below −Examplefrom collections import Counter    def find(input_):    # dictionary    wc = Counter(input_)    # Finding maximum occurrence    s = max(wc.values())    i = wc.values().index(s)    print (wc.items()[i]) # Driver program if __name__ ... Read More

Python program to find the highest 3 values in a dictionary

Pavitra
Updated on 04-Jul-2020 12:44:08

3K+ Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven a dictionary, we need to find the three highest valued values and display them.Approach 1 − Using the collections module ( Counter function )Example Live Demofrom collections import Counter # Initial Dictionary my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21} k = Counter(my_dict) # Finding 3 highest values high = k.most_common(3) print("Dictionary with 3 highest values:") print("Keys: Values") for i in high:    print(i[0], " :", i[1], " ")OutputDictionary with 3 highest values: Keys: Values r : 21 t ... Read More

Python program to find sum of elements in list

Akshitha Mote
Updated on 07-Jan-2025 11:14:14

1K+ Views

In this article, lets see try to find the sum of elements in a list. For a given integer list, the program should return the sum of all elements of the list. For an example let's consider list_1 = [1, 2, 3, 4, 5] the output of the program should be 15. Following are the different types of approaches to find the sum of elements in a list − Iterating through loops Using Recursion Using sum() function By ... Read More

Python Program to find the sum of array

Pavitra
Updated on 26-Sep-2019 14:11:29

976 Views

In this article, we will learn about the solution and approach to solve the given problem statement.Problem statementGiven an array as an input, we need to compute the sum of the given array.Here we may follow the brute-force approach i.e. traversing over a list and adding each element to an empty sum variable. Finally, we display the value of the sum.We can also perform an alternate approach using built-in sum function as discussed below.Example# main arr = [1, 2, 3, 4, 5] ans = sum(arr, n) print ('Sum of the array is ', ans)Output15 All the variables & functions are ... Read More

Advertisements