Program to find the product of ASCII values of characters in a string
Given a string str. The task is to find the product of ASCII values of characters in the string.
Examples:
Input : str = "IS" Output : 6059 73 * 83 = 6059 Input : str = "GfG" Output : 514182
The idea is to start with iterating through characters of the string and multiply their ASCII values to a variable namely, prod. Hence, return prod after the complete iteration of string.
Note: If the string is large, the program may cause segmentation fault because of the limited size of an int.
Below is the implementation of the above approach:
C++
// C++ program to find product // of ASCII value of characters // in string #include <bits/stdc++.h> using namespace std; // Function to find product // of ASCII value of characters // in string long long productAscii(string str) { long long prod = 1; // Traverse string to find the product for (int i = 0; i < str.length(); i++) { prod *= (int)str[i]; } // Return the product return prod; } // Driver code int main() { string str = "GfG"; cout << productAscii(str); return 0; } |
Java
// Java program to find product // of ASCII value of characters // in string class GFG { // Function to find product // of ASCII value of characters // in string static long productAscii(String str) { long prod = 1; // Traverse string to find the product for (int i = 0; i < str.length(); i++) { prod *= str.charAt(i); } // Return the product return prod; } // Driver Code public static void main(String[] args) { String str = "GfG"; System.out.println(productAscii(str)); } } // This code is contributed by Bilal |
Python3
# Python3 program to find product # of ASCII value of characters # in string # Function to find product # of ASCII value of characters # in string def productAscii(str): prod = 1 # Traverse string to find the product for i in range(0, len(str)): prod = prod * ord(str[i]) # Return the product return prod # Driver code if __name__=='__main__': str = "GfG" print(productAscii(str)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to find product // of ASCII value of characters // in string using System; class GFG { // Function to find product // of ASCII value of characters // in string static long productAscii(String str) { long prod = 1; // Traverse string to find the product for (int i = 0; i < str.Length; i++) { prod *= str[i]; } // Return the product return prod; } // Driver Code static public void Main () { String str = "GfG"; Console.Write(productAscii(str)); } } // This code is contributed by Raj |
PHP
<?php // PHP program to find product // of ASCII value of characters // in string // Function to find product // of ASCII value of characters // in string function productAscii($str) { $prod = 1; // Traverse string to find the product for ($i = 0; $i < strlen($str); $i++) { $prod *= ord($str[$i]); } // Return the product return $prod; } // Driver code $str = "GfG"; echo productAscii($str); // This code is contributed // by ChitraNayal ?> |
514182
Time Complexity: O(N), where N is the length of string.
Recommended Posts:
- Program to find the largest and smallest ASCII valued characters in a string
- Average of ASCII values of characters of a given string
- Count characters in a string whose ASCII values are prime
- Find the sum of the ascii values of characters which are present at prime positions
- Convert a string to hexadecimal ASCII values
- Check if a string contains only alphabets in Java using ASCII values
- Count pairs of characters in a string whose ASCII value difference is K
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Sum of the alphabetical values of the characters of a string
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Sum and Product of Prime Frequencies of Characters in a String
- Sums of ASCII values of each word in a sentence
- Map function and Dictionary in Python to sum ASCII values
- Count the number of words having sum of ASCII values less than and greater than k
- Print each word in a sentence with their corresponding average of ASCII values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



