Count strings having sum of ASCII values of characters equal to a Prime or Armstrong Number | C ++ program for the above approach ; Function to check if a number is prime number ; Define a flag variable ; Check for factors of num ; If factor is found , set flag to True and break out of loop ; Check if flag is True ; Function to calculate order of the number x ; Function to check whether the given number is Armstrong number or not ; If the condition satisfies ; Function to count Armstrong valued strings ; Stores the count of Armstrong valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it an Armstrong number ; Function to count prime valued strings ; Store the count of prime valued strings ; Iterate over the list ; Store the value of the string ; Find value of the string ; Check if it is a Prime Number ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int num ) { bool flag = false ; if ( num > 1 ) { for ( int i = 2 ; i < num ; i ++ ) { if ( ( num % i ) == 0 ) { flag = true ; break ; } } } if ( flag ) return false ; else return true ; } int order ( int x ) { int n = 0 ; while ( x != 0 ) { n = n + 1 ; x = x / 10 ; } return n ; } bool isArmstrong ( int x ) { int n = order ( x ) ; int temp = x ; int sum1 = 0 ; while ( temp != 0 ) { int r = temp % 10 ; sum1 = sum1 + pow ( r , n ) ; temp = temp / 10 ; } return ( sum1 == x ) ; } int count_armstrong ( vector < string > li ) { int c = 0 ; for ( string ele : li ) { int val = 0 ; for ( char che : ele ) val += che ; if ( isArmstrong ( val ) ) c += 1 ; } return c ; } int count_prime ( vector < string > li ) { int c = 0 ; for ( string ele : li ) { int val = 0 ; for ( char che : ele ) val += che ; if ( isPrime ( val ) ) c += 1 ; } return c ; } int main ( ) { vector < string > arr = { " geeksforgeeks " , " a " , " computer " , " science " , " portal " , " for " , " geeks " } ; cout << " Number ▁ of ▁ Armstrong ▁ Strings ▁ are : ▁ " << count_armstrong ( arr ) << endl ; cout << " Number ▁ of ▁ Prime ▁ Strings ▁ are : ▁ " << count_prime ( arr ) << endl ; }