The Wayback Machine - https://web.archive.org/web/20241124194142/https://www.geeksforgeeks.org/convert-number-to-words/
Open In App

Program to convert a given number to words

Last Updated : 12 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a non-negative integer n, the task is to convert the given number into its English representation according to International Number System.

Examples:

Input: n = 0
Output: “Zero”

Input: n = 123
Output: “One Hundred Twenty Three”

Input: n = 10245
Output: “Ten Thousand Two Hundred Forty Five“

Input: n = 2147483647
Output: “Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven”

[Approach 1] By breaking down the number into groups of three digits

The idea is to divide the number into groups of three digits (Thousands, Millions, Billions) and process each group separately. For each group, we convert the digits in the Hundreds, Tens, and Units places into their corresponding words, then, append the corresponding multiplier (like Thousand, Million, Billion). This process continues until the entire number is traversed. Finally, we combine all the groups, which form the complete English representation of the given number.

C++
// C++ program to convert number into words by breaking 
// it into groups of three

#include <iostream>
#include <vector>
#include <string>
using namespace std;

string convertToWords(int n) {
    if (n == 0) 
        return "Zero";
    
    // Words for numbers 0 to 19
    vector<string> units = {
        "",        "One",       "Two",      "Three",
        "Four",    "Five",      "Six",      "Seven",
        "Eight",   "Nine",      "Ten",      "Eleven",
        "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    };
    
    // Words for numbers multiple of 10        
    vector<string> tens = { 
        "",     "",     "Twenty",  "Thirty", "Forty",
        "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" 
    };
    
    vector<string> multiplier = 
                    {"", "Thousand", "Million", "Billion"};
  
    string res = "";
    int group = 0;
    
    // Process number in group of 1000s
    while (n > 0) {
        if (n % 1000 != 0) {
            
            int value = n % 1000;
            string temp = "";
            
            // Handle 3 digit number
            if (value >= 100) {
                temp = units[value / 100] + " Hundred ";
                value %= 100;
            }

            // Handle 2 digit number
            if (value >= 20) {
                temp += tens[value / 10] + " ";
                value %= 10;
            }

            // Handle unit number
            if (value > 0) {
                temp += units[value] + " ";
            }

            // Add the multiplier according to the group
            temp += multiplier[group] + " ";
            
            // Add this group result to overall result
            res = temp + res;
        }
        n /= 1000;
        group++;
    }
    
    // Remove trailing space
    return res.substr(0, res.find_last_not_of(" ") + 1);
}

int main() {
    int n = 2147483647;
    cout << convertToWords(n) << endl;
    return 0;
}
Java
// Java program to convert number into words by breaking 
// it into groups of three

import java.util.*;

class GfG {
    static String convertToWords(int n) {
        if (n == 0) 
            return "Zero";
        
        // Words for numbers 0 to 19
        String[] units = {
            "",        "One",       "Two",      "Three",
            "Four",    "Five",      "Six",      "Seven",
            "Eight",   "Nine",      "Ten",      "Eleven",
            "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
        
        // Words for numbers multiple of 10        
        String[] tens = { 
            "",     "",     "Twenty",  "Thirty", "Forty",
            "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" 
        };
        
        String[] multiplier = 
                    {"", "Thousand", "Million", "Billion"};
      
        String res = "";
        int group = 0;
        
        // Process number in group of 1000s
        while (n > 0) {
            if (n % 1000 != 0) {
                
                int value = n % 1000;
                String temp = "";
                
                // Handle 3 digit number
                if (value >= 100) {
                    temp = units[value / 100] + " Hundred ";
                    value %= 100;
                }

                // Handle 2 digit number
                if (value >= 20) {
                    temp += tens[value / 10] + " ";
                    value %= 10;
                }

                // Handle unit number
                if (value > 0) {
                    temp += units[value] + " ";
                }

                // Add the multiplier according to the group
                temp += multiplier[group] + " ";
                
                // Add this group result to overall result
                res = temp + res;
            }
            n /= 1000;
            group++;
        }
        
        // Remove trailing space
        return res.trim();
    }

    public static void main(String[] args) {
        int n = 2147483647;
        System.out.println(convertToWords(n));
    }
}
Python
# Python program to convert number into words by breaking 
# it into groups of three

def convertToWords(n):
    if n == 0:
        return "Zero"
    
    # Words for numbers 0 to 19
    units = [
        "",        "One",       "Two",      "Three",
        "Four",    "Five",      "Six",      "Seven",
        "Eight",   "Nine",      "Ten",      "Eleven",
        "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    ]
    
    # Words for numbers multiple of 10        
    tens = [
        "",     "",     "Twenty",  "Thirty", "Forty",
        "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
    ]
    
    multiplier = ["", "Thousand", "Million", "Billion"]
    
    res = ""
    group = 0
    
    # Process number in group of 1000s
    while n > 0:
        if n % 1000 != 0:
            
            value = n % 1000
            temp = ""
            
            # Handle 3 digit number
            if value >= 100:
                temp = units[value // 100] + " Hundred "
                value %= 100

            # Handle 2 digit number
            if value >= 20:
                temp += tens[value // 10] + " "
                value %= 10

            # Handle unit number
            if value > 0:
                temp += units[value] + " "

            # Add the multiplier according to the group
            temp += multiplier[group] + " "
            
            # Add the result of this group to overall result
            res = temp + res
        n //= 1000
        group += 1
    
    # Remove trailing space
    return res.strip()

if __name__ == "__main__":
    n = 2147483647
    print(convertToWords(n))
C#
// C# program to convert number into words by breaking 
// it into groups of three

using System;
using System.Collections.Generic;

class GfG {
    static string ConvertToWords(int n) {
        if (n == 0) 
            return "Zero";
        
        // Words for numbers 0 to 19
        string[] units = {
            "",        "One",       "Two",      "Three",
            "Four",    "Five",      "Six",      "Seven",
            "Eight",   "Nine",      "Ten",      "Eleven",
            "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
            "Sixteen", "Seventeen", "Eighteen", "Nineteen"
        };
        
        // Words for numbers multiple of 10        
        string[] tens = { 
            "",     "",     "Twenty",  "Thirty", "Forty",
            "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" 
        };
        
        string[] multiplier = 
                    {"", "Thousand", "Million", "Billion"};
      
        string res = "";
        int group = 0;
        
        // Process number in group of 1000s
        while (n > 0) {
            if (n % 1000 != 0) {
                
                int value = n % 1000;
                string temp = "";
                
                // Handle 3 digit number
                if (value >= 100) {
                    temp = units[value / 100] + " Hundred ";
                    value %= 100;
                }

                // Handle 2 digit number
                if (value >= 20) {
                    temp += tens[value / 10] + " ";
                    value %= 10;
                }

                // Handle unit number
                if (value > 0) {
                    temp += units[value] + " ";
                }

                // Add the multiplier according to the group
                temp += multiplier[group] + " ";
                
                // Add the this group result to overall result
                res = temp + res;
            }
            n /= 1000;
            group++;
        }
        
        // Remove trailing space
        return res.Trim();
    }

    static void Main() {
        int n = 2147483647;
        Console.WriteLine(ConvertToWords(n));
    }
}
JavaScript
// JavaScript program to convert number into words by breaking 
// it into groups of three

function convertToWords(n) {
    if (n === 0) 
        return "Zero";
    
    // Words for numbers 0 to 19
    const units = [
        "",        "One",       "Two",      "Three",
        "Four",    "Five",      "Six",      "Seven",
        "Eight",   "Nine",      "Ten",      "Eleven",
        "Twelve",  "Thirteen",  "Fourteen", "Fifteen",
        "Sixteen", "Seventeen", "Eighteen", "Nineteen"
    ];
    
    // Words for numbers multiple of 10        
    const tens = [
        "",     "",     "Twenty",  "Thirty", "Forty",
        "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
    ];
    
    const multiplier = ["", "Thousand", "Million", "Billion"];
    
    let res = "";
    let group = 0;
    
    // Process number in group of 1000s
    while (n > 0) {
        if (n % 1000 !== 0) {
            
            let value = n % 1000;
            let temp = "";
            
            // Handle 3 digit number
            if (value >= 100) {
                temp = units[Math.floor(value / 100)] + " Hundred ";
                value %= 100;
            }

            // Handle 2 digit number
            if (value >= 20) {
                temp += tens[Math.floor(value / 10)] + " ";
                value %= 10;
            }

            // Handle unit number
            if (value > 0) {
                temp += units[value] + " ";
            }

            // Add the multiplier according to the group
            temp += multiplier[group] + " ";
            
            // Add the result of this group to overall result
            res = temp + res;
        }
        n = Math.floor(n / 1000);
        group++;
    }
    
    // Remove trailing space
    return res.trim();
}

const n = 2147483647;
console.log(convertToWords(n));

Output
Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven

Time Complexity: O(log10(n)) – As we are dividing the number by a multiple of ten (like 1000, 100, 10), reducing the number logarithmically.
Auxiliary Space: O(1)

[Approach 2] By Mapping Key Numeric Values with English Words

The idea is to store the key numeric values, such as billion, million, thousand, hundred, tens, and units and their corresponding English words in descending order. We compare the given number with these values, starting from the largest. If the number is greater than or equal to the current numeric value, we first add the word for the quotient (number / numeric value), then append the word for the current numeric value (like “Billion”, “Thousand”, or “Hundred”), and finally append the word for the remainder (number % numeric value) recursively. This process continues until the entire number is converted to words.

For example, if the number is 203. So, the first key numeric value which is smaller than this number is 100.

  1. First, we add the quotient part = 203 / 100 = 2, result = “Two”.
  2. Next, we append the word for key numeric value which is 100, result = “Two Hundred” .
  3. Finally, we append the word for remainder part = 203 % 100 = 3, so the final result is = “Two Hundred Three”.
C++
// C++ program to convert number into words by Mapping Key Numeric Values
// with English Words

#include <iostream>
#include <vector>
#include <string>
using namespace std;

string convertToWordsRec(int n, vector<int> &values, vector<string> &words) {
    string res = "";
      
    // Iterating over all key Numeric values
    for (int i = 0; i < values.size(); i++) {
        int value = values[i];
        string word = words[i];

        // If the number is greater than or 
        // equal to the current numeric value
        if (n >= value) {

            // Append the quotient part
            // If the number is greater than or equal to 100
            // then only we need to handle that
            if (n >= 100)
                res += convertToWordsRec(n / value, values, words) + " ";

            // Append the word for numeric value
            res += word;

            // Append the remainder part
            if (n % value > 0)
                res += " " + convertToWordsRec(n % value, values, words);

            return res;
        }
    }

    return res;
}

string convertToWords(int n) {
    if (n == 0)
        return "Zero";

    // Key Numeric values and their corresponding English words
    vector<int> values = {1000000000, 1000000, 1000, 100, 90, 80, 70, 
                          60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14,
                          13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    
    vector<string> words = {"Billion", "Million", "Thousand", "Hundred", 
                            "Ninety", "Eighty", "Seventy", "Sixty", "Fifty",
                            "Forty", "Thirty", "Twenty", "Nineteen",
                            "Eighteen", "Seventeen", "Sixteen", "Fifteen",
                            "Fourteen", "Thirteen", "Twelve", "Eleven",
                            "Ten", "Nine", "Eight", "Seven", "Six", "Five",
                            "Four", "Three", "Two", "One"};
  
    return convertToWordsRec(n, values, words);
}

int main() {
    int n = 2147483647;
    cout << convertToWords(n) << endl;
    return 0;
}
Java
// Java program to convert number into words by Mapping Key Numeric 
// Values with English Words

class GfG {

    // Method to convert number into words recursively
    static String convertToWordsRec(int n, int[] values, String[] words) {
        String res = "";

        // Iterating over all key Numeric values
        for (int i = 0; i < values.length; i++) {
            int value = values[i];
            String word = words[i];

            // If the number is greater than or equal to current numeric value
            if (n >= value) {

                // Append the quotient part
                // If the number is greater than or equal to 100
                // then only we need to handle that
                if (n >= 100)
                    res += convertToWordsRec(n / value, values, words) + " ";

                // Append the word for numeric value
                res += word;

                // Append the remainder part
                if (n % value > 0)
                    res += " " + convertToWordsRec(n % value, values, words);

                return res;
            }
        }

        return res;
    }

    static String convertToWords(int n) {
        if (n == 0)
            return "Zero";

        // Key Numeric values and their corresponding English words
        int[] values = {
            1000000000, 1000000, 1000, 100, 90, 80, 70,
            60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14,
            13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
        };

        String[] words = {
            "Billion", "Million", "Thousand", "Hundred",
            "Ninety", "Eighty", "Seventy", "Sixty", "Fifty",
            "Forty", "Thirty", "Twenty", "Nineteen",
            "Eighteen", "Seventeen", "Sixteen", "Fifteen",
            "Fourteen", "Thirteen", "Twelve", "Eleven",
            "Ten", "Nine", "Eight", "Seven", "Six", "Five",
            "Four", "Three", "Two", "One"
        };

        return convertToWordsRec(n, values, words);
    }

    public static void main(String[] args) {
        int n = 2147483647;
        System.out.println(convertToWords(n));
    }
}
Python
# Python program to convert number into words by Mapping Key Numeric Values
# with English Words

def convertToWordsRec(n, values, words):
    res = ""
    
    # Iterating over all key Numeric values
    for i in range(len(values)):
        value = values[i]
        word = words[i]

        # If the number is greater than or equal to the current numeric value
        if n >= value:

            # Append the quotient part
            # If the number is greater than or equal to 100
            # then only we need to handle that
            if n >= 100:
                res += convertToWordsRec(n // value, values, words) + " "

            # Append the word for numeric value
            res += word

            # Append the remainder part
            if n % value > 0:
                res += " " + convertToWordsRec(n % value, values, words)

            return res

    return res

def convertToWords(n):
    if n == 0:
        return "Zero"

    # Key Numeric values and their corresponding English words
    values = [1000000000, 1000000, 1000, 100, 90, 80, 70, 
              60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14,
              13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    
    words = ["Billion", "Million", "Thousand", "Hundred", 
             "Ninety", "Eighty", "Seventy", "Sixty", "Fifty",
             "Forty", "Thirty", "Twenty", "Nineteen",
             "Eighteen", "Seventeen", "Sixteen", "Fifteen",
             "Fourteen", "Thirteen", "Twelve", "Eleven",
             "Ten", "Nine", "Eight", "Seven", "Six", "Five",
             "Four", "Three", "Two", "One"]
  
    return convertToWordsRec(n, values, words)
  
if __name__ == "__main__":
    n = 2147483647
    print(convertToWords(n))
C#
// C# program to convert number into words by Mapping Key Numeric Values
// with English Words

using System;

class GfG {
    static string convertToWordsRec(int n, int[] values, string[] words) {
        string res = "";

        // Iterating over all key Numeric values
        for (int i = 0; i < values.Length; i++) {
            int value = values[i];
            string word = words[i];

            // If the number is greater than or equal to the current numeric value
            if (n >= value) {

                // Append the quotient part
                // If the number is greater than or equal to 100
                // then only we need to handle that
                if (n >= 100)
                    res += convertToWordsRec(n / value, values, words) + " ";

                // Append the word for numeric value
                res += word;

                // Append the remainder part
                if (n % value > 0)
                    res += " " + convertToWordsRec(n % value, values, words);

                return res;
            }
        }

        return res;
    }

    static string convertToWords(int n) {
        if (n == 0)
            return "Zero";

        // Key Numeric values and their corresponding English words
        int[] values = {
            1000000000, 1000000, 1000, 100, 90, 80, 70, 
            60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14,
            13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
        };

        string[] words = {
            "Billion", "Million", "Thousand", "Hundred", 
            "Ninety", "Eighty", "Seventy", "Sixty", "Fifty",
            "Forty", "Thirty", "Twenty", "Nineteen",
            "Eighteen", "Seventeen", "Sixteen", "Fifteen",
            "Fourteen", "Thirteen", "Twelve", "Eleven",
            "Ten", "Nine", "Eight", "Seven", "Six", "Five",
            "Four", "Three", "Two", "One"
        };

        return convertToWordsRec(n, values, words);
    }

    static void Main() {
        int n = 2147483647;
        Console.WriteLine(convertToWords(n));
    }
}
JavaScript
// JavaScript program to convert number into words by Mapping Key Numeric Values
// with English Words

function convertToWordsRec(n, values, words) {
    let res = "";

    // Iterating over all key Numeric values
    for (let i = 0; i < values.length; i++) {
        let value = values[i];
        let word = words[i];

        // If the number is greater than or equal to the current numeric value
        if (n >= value) {

            // Append the quotient part
            // If the number is greater than or equal to 100
            // then only we need to handle that
            if (n >= 100)
                res += convertToWordsRec(Math.floor(n / value), values, words) + " ";

            // Append the word for numeric value
            res += word;

            // Append the remainder part
            if (n % value > 0)
                res += " " + convertToWordsRec(n % value, values, words);

            return res;
        }
    }

    return res;
}

function convertToWords(n) {
    if (n === 0)
        return "Zero";

    // Key Numeric values and their corresponding English words
    const values = [1000000000, 1000000, 1000, 100, 90, 80, 70, 
                    60, 50, 40, 30, 20, 19, 18, 17, 16, 15, 14,
                    13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
    
    const words = ["Billion", "Million", "Thousand", "Hundred", 
                   "Ninety", "Eighty", "Seventy", "Sixty", "Fifty",
                   "Forty", "Thirty", "Twenty", "Nineteen",
                   "Eighteen", "Seventeen", "Sixteen", "Fifteen",
                   "Fourteen", "Thirteen", "Twelve", "Eleven",
                   "Ten", "Nine", "Eight", "Seven", "Six", "Five",
                   "Four", "Three", "Two", "One"];

    return convertToWordsRec(n, values, words);
}

let n = 2147483647;
console.log(convertToWords(n));

Output
Two Billion One Hundred Forty Seven Million Four Hundred Eighty Three Thousand Six Hundred Forty Seven

Time Complexity: O(log10(n)) – As we are dividing the number by a key numeric value (typically by 1000, 100, etc.), reducing the number logarithmically with each call.
Auxiliary Space: O(log10(n)) – Recursion stack size will be same as the total number of recursive calls.



Previous Article
Next Article

Similar Reads

Program to convert a given number to words | Set 2
Write code to convert a given number into words. Examples: Input: 438237764Output: forty three crore eighty two lakh thirty seven thousand seven hundred and sixty four Input: 999999Output: nine lakh ninety nine thousand nine hundred and ninety nine Input: 1000Output: one thousand Explanation: 1000 in words is "one thousand" Recommended PracticeInte
13 min read
Find all words from String present after given N words
Given a string S and a list lis[] of N number of words, the task is to find every possible (N+1)th word from string S such that the 'second' word comes immediately after the 'first' word, the 'third' word comes immediately after the 'second' word, the 'fourth' word comes immediately after the 'third' word and so on. Examples: Input: S = “Siddhesh i
11 min read
Count words that appear exactly two times in an array of words
Given an array of n words. Some words are repeated twice, we need to count such words. Examples: Input : s[] = {"hate", "love", "peace", "love", "peace", "hate", "love", "peace", "love", "peace"}; Output : 1 There is only one word "hate" that appears twice Input : s[] = {"Om", "Om", "Shankar", "Tripathi", "Tom", "Jerry", "Jerry"}; Output : 2 There
6 min read
Convert given Array of Integers into Words
Given an array arr[] of N elements which are in range [0, 9]. the task is to convert each array element into its numeric strings. Examples: Input: arr[] = [1, 4, 3, 2, 6]Output: one four three two six Input: arr[]= [0, 4, 4, 6, 9]Output: zero four four six nine Approach: The problem can be solved with the help of map. Follow the steps given below:
4 min read
Convert given time into words
Given a time in the format of hh:mm (12-hour format) 0 < hh < 12, 0 <= mm < 60. The task is to convert it into words as shown:Examples : Input : h = 5, m = 0 Output : five o' clock Input : h = 6, m = 24 Output : twenty four minutes past six Corner cases are m = 0, m = 15, m = 30 and m = 45. 6:00 six o'clock 6:10 ten minutes past six 6:1
7 min read
Convert numeric words to numbers
Given a string S, containing numeric words, the task is to convert the given string to the actual number. Example: Input: S = "zero four zero one"Output: 0401 Input: S = "four zero one four"Output: 4014 Method #1: Using loop + join() + split() One of the ways to solve this problem is to use a map, where one can map the numeric with words and then s
6 min read
Program to extract words from a given String
The task is to extract words from a given string. There may be one or more space between words. Examples: Input : geeks for geeks Output : geeks for geeks Input : I love coding. Output: I love coding We have discussed a solution in the below post. How to split a string in C/C++, Python and Java? In this post, a new solution using stringstream is di
3 min read
Javascript Program To Reverse Words In A Given String
Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples:  Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "practice of lot a needs coding at good getting" Al
4 min read
Lex Program to count number of words
Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator) Example: Input: Hello everyone Output: 2 Input: This is
1 min read
Find least start index of a substring from given String that contains all the given words in a contiguous manner
Given a string S and an array of equal length words (strings) arr[]. The task is to find the minimum start index of the substring that contains all the given words in a contiguous manner. If no such index is found, return -1.Note: The words can be in any sequence. Examples: Input: arr[ ] = {"bat", "bal", "cat"}, S = "hellocatbyebalbatcatyo"Output:
9 min read
Print all valid words from given dictionary that are possible using Characters of given Array
Given a dictionary of strings dict[] and a character array arr[]. Print all valid words of the dictionary that are possible using characters from the given character array.Examples: Input: dict - ["go", "bat", "me", "eat", "goal", boy", "run"] arr[] = ['e', 'o', 'b', 'a', 'm', 'g', 'l']Output: "go", "me", "goal".Explanation: Only all the characters
5 min read
Lexicographically largest string formed by choosing words from given Sentence as per given Pattern
Given a sentence S and a string B having distinct characters, find a string by joining the words of S according to given conditions:- Choose a word from S ifIt has at least length(B)/2 characters from string B orHaving at least one character from string B and lexicographically sorted in increasing order.The string formed should be lexicographically
7 min read
Check if words in given sentence occur based on the given pattern
Given a pattern 'pattern' and a sentence 's', the task is to check if the words in the given sentence occur based on the pattern represented in the 'pattern'.Examples: Input: pattern = "abba", s = "geeks for for geeks"Output: trueExplanation: In the pattern, 'a' denotes 'geeks' and 'b' denotes 'for'. Therefore, sentence 's' follows the pattern 'pat
6 min read
Number of words that can be made using exactly P consonants and Q vowels from the given string
Given a string str and two integers P and Q. The task is to find the total count of words that can be formed by choosing exactly P consonants and Q vowels from the given string.Examples: Input: str = "geek", P = 1, Q = 1 Output: 8 "ge", "ge", "eg", "ek", "eg", "ek", "ke" and "ke" are the possible words.Input: str = "crackathon", P = 4, Q = 3 Output
7 min read
Program to Convert Octal Number to Binary Number
Given an Octal number as input, the task is to convert that number to Binary number. Examples: Input : Octal = 345Output : Binary = 11100101Explanation : Equivalent binary value of 3: 011Equivalent binary value of 4: 100Equivalent binary value of 5: 101 Input : Octal = 120Output : Binary = 1010000 Octal Number: An Octal number is a positional numer
9 min read
Program to Convert BCD number into Decimal number
Given a BCD (Binary Coded Decimal) number, the task is to convert the BCD number into its equivalent Decimal number. Examples: Input: BCD = 100000101000 Output: 828 Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1000. Here, 1000 is equivalent to 8 and 0010 is equivalent to 2. So, the number becomes 828. Input: BCD = 1001000
10 min read
Program to convert a Binary Number to Hexa-Decimal Number
Given a Binary Number, the task is to convert this Binary number to its equivalent Hexa-Decimal Number.Examples: Input: 100000101111 Output: 82F Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1111. Here, 1000 is equivalent to 8, 0010 is equivalent to 2 and 1111 is equivalent to F Therefore, the number becomes 82FInput:10101
10 min read
Program to convert a binary number to hexadecimal number
Given a Binary Number, the task is to convert the given binary number to its equivalent hexadecimal number. The input could be very large and may not fit even into an unsigned long long int. Examples:  Input: 110001110Output: 18EInput: 1111001010010100001.010110110011011Output: 794A1.5B36 794A1D9B Approach 1: Binary Number: A binary number is a num
13 min read
Smallest number to be subtracted to convert given number to a palindrome
Given an integer N, the task is to find the smallest number to be subtracted from N to obtain a palindrome. Examples: Input: N = 1000Output: 1Explanation: Since 1000 – 1 = 999, which is a palindrome, the smallest number to be subtracted is 1. Input: N = 3456Output: 13Explanation: Since 3456 – 13 = 3443, which is a palindrome, the smallest number to
5 min read
Convert a number m to n using minimum number of given operations
Convert a number m to n with minimum operations. The operations allowed are : Multiply by 2, i.e., do m = 2 * mSubtract 1, i.e., do m = m - 1 Print -1 if it is not possible to convert.Examples : Input : m = 3, n = 11 Output : 3 1st operation: *2 = 3*2 = 6 2nd operation: *2 = 6*2 = 12 3rd operation: -1 = 12-1 = 11 Input : m = 15, n = 20 Output : 6 1
7 min read
Javascript Program for Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Python program to remove words that are common in two Strings
Given two strings S1 and S2, representing sentences, the task is to print both sentences after removing all words which are present in both sentences. Input: S1 = "sky is blue in color", S2 ="Raj likes sky blue color "Output: is in Raj likes Explanation: The common words are [ sky, blue, color ]. Removing these words from the two sentences modifies
7 min read
Remove all the palindromic words from the given sentence
Given a sentence str. The problem is to remove all the palindromic words from the given sentence.Examples: Input : str = "Text contains malayalam and level words" Output : Text contains and words Input : str = "abc bcd" Output : abc bcd Approach: One by one extract all the words. Check if the current word is not a palindrome then add it to the fina
12 min read
Reverse Words in a Given String in Python
We are given a string and we need to reverse words of a given string Examples: Input : str =" geeks quiz practice code" Output : str = code practice quiz geeks Input : str = "my name is laxmi" output : str= laxmi is name my Reverse the words in the given string program C/C++ Code # Python code # To reverse words in a given string # input string str
6 min read
Possible Words using given characters in Python
Given a dictionary and a character array, print all valid words that are possible using characters from the array. Note: Repetitions of characters is not allowed. Examples: Input : Dict = ["go","bat","me","eat","goal","boy", "run"] arr = ['e','o','b', 'a','m','g', 'l'] Output : go, me, goal. This problem has existing solution please refer Print all
5 min read
K length words that can be formed from given characters without repetition
Given an integer k and a string str consisting of lowercase English alphabets, the task is to count how many k-character words (with or without meaning) can be formed from the characters of str when repetition is not allowed. Examples: Input: str = "cat", k = 3 Output: 6 Required words are "cat", "cta", "act", "atc", "tca" and "tac". Input: str = "
6 min read
Count of words that are present in all the given sentences
Given n sentences. The task is to count the number of words that appear in all of these sentences.Note that every word consists of only lowercase English alphabets. Examples: Input: arr[] = { "there is a cow", "cow is our mother", "cow gives us milk and milk is sweet", "there is a boy who loves cow"} Output: 2 Only the words "is" and "cow" appear i
15 min read
Check if the given Trie contains words starting from every alphabet
Given a Trie, the task is to check if it contains words starting from every alphabet from [a - z]. Examples: Input: keys[] = {"element", "fog", "great", "hi", "ok", "ios", "parrot", "quiz", "kim", "mango", "nature", "apple", "ball", "cat", "dog", "lime", "ruby", "shine", "tinkter", "ultra", "volly", "wow", "xerox", "yak", "zenon", "joke"} Output: Y
8 min read
Find all words in given Sentence that are lexicographically increasing and lexicographically decreasing
Given a string representing a sentence named str of size N, the task is to find all the valid words in a sentence that are lexicographically sorted in increasing order and in decreasing order along with their counts. Note: Valid words are:- words don't containing numbers.words greater than size 1. Examples: Input: str="geeks for geeks"Output: 1 "fo
10 min read
Print a sorted list of words represented by the expression under the given grammar
Given a string R(x) of length n representing an expression having the set of words under the given grammar: For every lowercase letter x, R(x) = {x}For expressions e_1, e_2, ..., e_k with k?2, R({e_1, e_2, ..., e_k}) = R(e_1) ? R(e_2) ? ... ? R(e_k).For expressions e_1 and e_2, R(e_1 + e_2) = {a + b for (a, b) in R(e_1) × R(e_2)}, where + denotes c
14 min read
three90RightbarBannerImg