Program to find the product of ASCII values of characters in a string - GeeksforGeeks

22 Mar, 2021
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++
Java
Python3
C#
PHP
Javascript
// 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 stringlong 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 codeint main(){    string str = "GfG";     cout << productAscii(str);     return 0;}
// Java program to find product// of ASCII value of characters// in stringclass GFG{ // Function to find product// of ASCII value of characters// in stringstatic 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 Codepublic static void main(String[] args){    String str = "GfG";         System.out.println(productAscii(str));}} // This code is contributed by Bilal
# Python3 program to find product# of ASCII value of characters# in string # Function to find product# of ASCII value of characters# in stringdef 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 codeif __name__=='__main__':    str = "GfG"     print(productAscii(str)) # This code is contributed by# Sanjit_Prasad
// C# program to find product// of ASCII value of characters// in stringusing System; class GFG{ // Function to find product// of ASCII value of characters// in stringstatic 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 Codestatic public void Main (){    String str = "GfG";         Console.Write(productAscii(str));}} // This code is contributed by Raj
<?php// PHP program to find product// of ASCII value of characters// in string // Function to find product// of ASCII value of characters// in stringfunction 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?>
<script> // javascript program to find product// of ASCII value of characters// in string // Function to find product// of ASCII value of characters// in stringfunction productAscii(str){    var prod = 1;     // Traverse string to find the product    for (i = 0; i < str.length; i++)    {        prod *= str.charAt(i).charCodeAt(0);    }     // Return the product    return prod;} // Driver Code str = "GfG"; document.write(productAscii(str)); // This code contributed by shikhasingrajput </script>
514182
 
Time Complexity: O(N), where N is the length of string. 
Sanjit_Prasad
bilal-hungund
R_Raj
ukasp
nidhi_biet
shikhasingrajput
ASCII
loop
C++ Programs
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
Passing a function as a parameter in C++
cin in C++
Generics in C++
CSV file management using C++
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4