Find Number of Numbers
Problem Statement:
Given an array A[] of n elements. Your task is to write such program which returns an integer denoting the total number of times digit k appears in the whole array.
For Example:
A[]={11,12,13,14,15}, k=1
Output=6 //Count of the digit 1 in the array
Prerequisite:
MATLAB CODE FOR THIS PROGRAMME:
clc
x=input('Enter the array:');
k=input('Enter the number:');
c=0;
for i=1:length(x)
m=x(i);
while(m>0)
b=rem(m,10);
if(b==k)
c=c+1;
end
m=(m-b)/10;
end
end
Explanation:
Sample Output:
JAVA CODE:
import java.util.Scanner;
class Duk
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the array length:");
int l=obj.nextInt();
int a[]=new int[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter the "+i+"th element");
a[i]=obj.nextInt();
}
System.out.println("Enter the number you want to search:");
int k=obj.nextInt();
int m=0;
int b=0;
int c=0;
for(int i=0;i<l;i++)
{
m=a[i];
while(m>0)
{
b=m%10;
if(b==k)
{
c=c+1;
}
m=m/10;
}
}
System.out.println();
System.out.println(c);
}
}
Output:
No comments