Related Articles

Related Articles

Program to find the product of ASCII values of characters in a string
  • Difficulty Level : Medium
  • Last Updated : 18 Oct, 2019

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



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

514182

Time Complexity: O(N), where N is the length of string.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :