Open In App

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

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

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 the 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 Python3 C# JavaScript PHP

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.


Article Tags :
Practice Tags :

Similar Reads