Skip to main content

Count the number of array elements whose digit sum are equal to K

Here given code implementation process.

// C Program for
// Count the number of array elements whose digit sum are equal to K
#include <stdio.h>

int digitSum(int number)
{
	int n = number;
	if (n < 0)
	{
		// Convert negative number to positive number
		n = -n;
	}
	int result = 0;
	while (n != 0)
	{
		// Adding last digit of a given number
		result = result + (n % 10);
		// Remove last digit
		n = n / 10;
	}
	return result;
}
void countKDigitSum(int arr[], int n, int k)
{
	int count = 0;
	for (int i = 0; i < n; ++i)
	{
		if (digitSum(arr[i]) == k)
		{
			// Increase counter
			count++;
		}
	}
	printf("\n %d", count);
}
int main()
{
	int arr[] = {
		17 , 123 , 16 , 60 , 11 , 23 , 42
	};
	// Get the number of elements
	int n = sizeof(arr) / sizeof(arr[0]);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	countKDigitSum(arr, n, 6);
	return 0;
}

Output

 3
/*
    Java program for
    Count the number of array elements whose digit sum are equal to K
*/
public class Counting
{
	public int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	public void countKDigitSum(int[] arr, int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		System.out.print("\n " + count);
	}
	public static void main(String[] args)
	{
		Counting task = new Counting();
		int[] arr = {
			17 , 123 , 16 , 60 , 11 , 23 , 42
		};
		// Get the number of elements
		int n = arr.length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	public: int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	void countKDigitSum(int arr[], int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (this->digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		cout << "\n " << count;
	}
};
int main()
{
	Counting *task = new Counting();
	int arr[] = {
		17 , 123 , 16 , 60 , 11 , 23 , 42
	};
	// Get the number of elements
	int n = sizeof(arr) / sizeof(arr[0]);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task->countKDigitSum(arr, n, 6);
	return 0;
}

Output

 3
// Include namespace system
using System;
/*
    Csharp program for
    Count the number of array elements whose digit sum are equal to K
*/
public class Counting
{
	public int digitSum(int number)
	{
		int n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		int result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	public void countKDigitSum(int[] arr, int n, int k)
	{
		int count = 0;
		for (int i = 0; i < n; ++i)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		Console.Write("\n " + count);
	}
	public static void Main(String[] args)
	{
		Counting task = new Counting();
		int[] arr = {
			17 , 123 , 16 , 60 , 11 , 23 , 42
		};
		// Get the number of elements
		int n = arr.Length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
package main
import "fmt"
/*
    Go program for
    Count the number of array elements whose digit sum are equal to K
*/
func digitSum(number int) int {
	var n int = number
	if n < 0 {
		// Convert negative number to positive number
		n = -n
	}
	var result int = 0
	for (n != 0) {
		// Adding last digit of a given number
		result = result + (n % 10)
		// Remove last digit
		n = n / 10
	}
	return result
}
func countKDigitSum(arr[] int, n int, k int) {
	var count int = 0
	for i := 0 ; i < n ; i++ {
		if digitSum(arr[i]) == k {
			// Increase counter
			count++
		}
	}
	fmt.Print("\n ", count)
}
func main() {

	var arr = [] int {17 , 123 , 16 , 60 , 11 , 23 , 42}
	// Get the number of elements
	var n int = len(arr)
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	countKDigitSum(arr, n, 6)
}

Output

 3
<?php
/*
    Php program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	public	function digitSum($number)
	{
		$n = $number;
		if ($n < 0)
		{
			// Convert negative number to positive number
			$n = -$n;
		}
		$result = 0;
		while ($n != 0)
		{
			// Adding last digit of a given number
			$result = $result + ($n % 10);
			// Remove last digit
			$n = (int)($n / 10);
		}
		return $result;
	}
	public	function countKDigitSum($arr, $n, $k)
	{
		$count = 0;
		for ($i = 0; $i < $n; ++$i)
		{
			if ($this->digitSum($arr[$i]) == $k)
			{
				// Increase counter
				$count++;
			}
		}
		echo("\n ".$count);
	}
}

function main()
{
	$task = new Counting();
	$arr = array(17, 123, 16, 60, 11, 23, 42);
	// Get the number of elements
	$n = count($arr);
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	$task->countKDigitSum($arr, $n, 6);
}
main();

Output

 3
/*
    Node JS program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	digitSum(number)
	{
		var n = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = parseInt(n / 10);
		}
		return result;
	}
	countKDigitSum(arr, n, k)
	{
		var count = 0;
		for (var i = 0; i < n; ++i)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count++;
			}
		}
		process.stdout.write("\n " + count);
	}
}

function main()
{
	var task = new Counting();
	var arr = [17, 123, 16, 60, 11, 23, 42];
	// Get the number of elements
	var n = arr.length;
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}
main();

Output

 3
#    Python 3 program for
#    Count the number of array elements whose digit sum are equal to K
class Counting :
	def digitSum(self, number) :
		n = number
		if (n < 0) :
			#  Convert negative number to positive number
			n = -n
		
		result = 0
		while (n != 0) :
			#  Adding last digit of a given number
			result = result + (n % 10)
			#  Remove last digit
			n = int(n / 10)
		
		return result
	
	def countKDigitSum(self, arr, n, k) :
		count = 0
		i = 0
		while (i < n) :
			if (self.digitSum(arr[i]) == k) :
				#  Increase counter
				count += 1
			
			i += 1
		
		print("\n ", count, end = "")
	

def main() :
	task = Counting()
	arr = [17, 123, 16, 60, 11, 23, 42]
	#  Get the number of elements
	n = len(arr)
	#  k = 6
	#  ----------
	#  1 + 2 + 3 = 6
	#  6 + 0   = 6
	#  4 + 2   = 4
	#  [123,60,42]
	#  Result = 3
	task.countKDigitSum(arr, n, 6)

if __name__ == "__main__": main()

Output

  3
#    Ruby program for
#    Count the number of array elements whose digit sum are equal to K
class Counting 
	def digitSum(number) 
		n = number
		if (n < 0) 
			#  Convert negative number to positive number
			n = -n
		end

		result = 0
		while (n != 0) 
			#  Adding last digit of a given number
			result = result + (n % 10)
			#  Remove last digit
			n = n / 10
		end

		return result
	end

	def countKDigitSum(arr, n, k) 
		count = 0
		i = 0
		while (i < n) 
			if (self.digitSum(arr[i]) == k) 
				#  Increase counter
				count += 1
			end

			i += 1
		end

		print("\n ", count)
	end

end

def main() 
	task = Counting.new()
	arr = [17, 123, 16, 60, 11, 23, 42]
	#  Get the number of elements
	n = arr.length
	#  k = 6
	#  ----------
	#  1 + 2 + 3 = 6
	#  6 + 0   = 6
	#  4 + 2   = 4
	#  [123,60,42]
	#  Result = 3
	task.countKDigitSum(arr, n, 6)
end

main()

Output

 3
/*
    Scala program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting()
{
	def digitSum(number: Int): Int = {
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	def countKDigitSum(arr: Array[Int], n: Int, k: Int): Unit = {
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (digitSum(arr(i)) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Counting = new Counting();
		var arr: Array[Int] = Array(17, 123, 16, 60, 11, 23, 42);
		// Get the number of elements
		var n: Int = arr.length;
		// k = 6
		// ----------
		// 1 + 2 + 3 = 6
		// 6 + 0   = 6
		// 4 + 2   = 4
		// [123,60,42]
		// Result = 3
		task.countKDigitSum(arr, n, 6);
	}
}

Output

 3
import Foundation;
/*
    Swift 4 program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	func digitSum(_ number: Int) -> Int
	{
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n  != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	func countKDigitSum(_ arr: [Int], _ n: Int, _ k: Int)
	{
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (self.digitSum(arr[i]) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n ", count, terminator: "");
	}
}
func main()
{
	let task: Counting = Counting();
	let arr: [Int] = [17, 123, 16, 60, 11, 23, 42];
	// Get the number of elements
	let n: Int = arr.count;
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}
main();

Output

  3
/*
    Kotlin program for
    Count the number of array elements whose digit sum are equal to K
*/
class Counting
{
	fun digitSum(number: Int): Int
	{
		var n: Int = number;
		if (n < 0)
		{
			// Convert negative number to positive number
			n = -n;
		}
		var result: Int = 0;
		while (n != 0)
		{
			// Adding last digit of a given number
			result = result + (n % 10);
			// Remove last digit
			n = n / 10;
		}
		return result;
	}
	fun countKDigitSum(arr: Array < Int > , n: Int, k: Int): Unit
	{
		var count: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			if (this.digitSum(arr[i]) == k)
			{
				// Increase counter
				count += 1;
			}
			i += 1;
		}
		print("\n " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Counting = Counting();
	val arr: Array < Int > = arrayOf(17, 123, 16, 60, 11, 23, 42);
	// Get the number of elements
	val n: Int = arr.count();
	// k = 6
	// ----------
	// 1 + 2 + 3 = 6
	// 6 + 0   = 6
	// 4 + 2   = 4
	// [123,60,42]
	// Result = 3
	task.countKDigitSum(arr, n, 6);
}

Output

 3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment