Sunday, September 7, 2025
HomeData Modelling & AIProgram to find the product of ASCII values of characters in a...

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.

Implementation:

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
?>


Javascript




<script>
 
// javascript 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)
{
    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>


Output

514182

Time Complexity: O(N), where N is the length of string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6642 POSTS0 COMMENTS
Nicole Veronica
11808 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11871 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS