Program to Multiply Numbers Present in a String

Description

To multiply numbers present in a string check which character is number.
If character is a number, multiply it else ignore that character.

C/C++

/* C program to multiply numbers present in a string */
//Save it as MultiplyDigitsNumber.c


#include<stdio.h>
int main(){

    char str[100];
    int i, len, x, mul=1;

    printf("Enter a string : ");
    scanf("%s",str);

    len = strlen(str);

    for(i=0;i<len;i++){
        x = str[i] - '0';
        if(x>=0 && x<=9){
            mul *= x;
        }else{
            continue;
        }
    }

    printf("\nMultiplication : %d", mul);

    return 0;
}
Input:
Enter a string : gja563lk

Output:
Multiplication : 90

Java

/* Java program to multiply numbers present in a string */
//Save it as MultiplyDigitsNumber.java


import java.io.*;
import java.util.Scanner;

public class MultiplyDigitsNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        String str;
        int i, len, x, mul=1;

        System.out.println("Enter a string : ");
        str = scanner.nextLine();

        len = str.length();

        for(i=0;i<len;i++){
            x = str.charAt(i) - '0';
            if(x>=0 && x<=9){
                mul *= x;
            }else{
                continue;
            }
        }
        System.out.println("\nMultiplication : " + mul);
    }
}
Input:
Enter a string : 
gja563lk

Output:
Multiplication : 90

Related Programs

1) Program to remove given number from a string
2) Program to Add numbers present in a String
3) Program to find maximum number of 1s in a substring of a String
4) Count Number of Words in a String
5) Find Occurrence Of Each Word In a Sentence
6) Print All Unique Words Of A String
7) Program To Print Unique Word Of A String Using Set
8) Program to Append one String to another String
9) Program to Compare Two Strings
10) Program to Reverse a String
Share Me

Leave a Reply