You are on page 1of 2

Class Numeric 1/2

/*
* ashking13th@gmail.com
* javaprogramsbyashking13th.blogspot.in
* ashkingjava.blogspot.in
*
*
* QUESTION
* A class called Numeric has been defined yo find and display the frequency
* of each digit present in the number and the product of the digits .
* Some of the members of the class are given below :
*
* Data Member : n - long integer type
*
* Member functions :
* Numeric(long a) :constructor to assign a to n
* void frequency() :to find the frequency of each digit in the number
* int product() :to return the product of the digits of the number
*
* Write main() function to implement the above
*/
import java.io.*;
public class Numeric
{
long n;
public Numeric(long a)//parameterised constructor
{
n=a;
}
void frequency()//method to find frequency of digits
{
int a[]=new int[10];
//creating an array to store
//frequency of digits
for(int i=0;i<10;i++)//initialising the array
{
a[i]=0;
}
long m=n;//creating dummy variable
int r=0;//variabble to extract digits
int p=1;//variable to store product of digits
while(m>0)
{
r=(int)(m%10);
a[r]=(a[r]+1);//finding frequency
/* we declare an array of 10elments i.e.,from index no.0 to 9.
* We consider every element to be frequency of the digit equal
* to its index no. eg. element at index no. 3 would act as a
* variable to store the frequency of the digit 3.
* For dong this ,first we extract digits of the given number
Mar 25, 2014 3:23:08 PM
Class Numeric (continued) 2/2
* and then we add 10to its counter i.e.,element with the digit
* as its index no.
*/
m=m/10;
//updating loop variable
}
System.out.println("Number entered is "+n);
System.out.println("Digit\tfrequency");
for(int j=0;j<10;j++)
{
if(a[j]!=0)
{
System.out.println(j+"\t"+a[j]);
//printing frequency
//if it is not 0
}
}
}
//end of frequency()
int product()
{
long m=n;
int p=1;
long r=0;
while(m>0)
{
r=m%10;
p=(int)(p*r);
m=m/10;
}
return p;
}//end of product
public static void main()throws IOException
{
DataInputStream d=new DataInputStream(System.in);
System.out.println("Enter a number");
long a=Long.parseLong(d.readLine());
Numeric obj1=new Numeric(a);
//creating object using
//parameterised constructor
obj1.frequency();//calling functions
int p=obj1.product();
System.out.println("Products of digits of the number is "+p);
}
//end of main()
}
Mar 25, 2014 3:23:09 PM

You might also like