...

/

Examples of Algorithms: Part 1

Examples of Algorithms: Part 1

Learn problem-solving techniques with the help of examples using different algorithms.

Coding example: 69

In this example, we have an input like 1122322 and we will try to find how many times 2 appears in this number.

Press + to interact
public class ExampleSixtyNine {
static int frequencyDigits(int n, int d)
{
int c = 0;
while (n > 0)
{
// check for equality
if (n % 10 == d)
c++;
// reduce the number
n = n / 10;
}
return c;
}
public static void main(String[] args)
{
int myNumber = 1122322;
int findTheDigit = 2;
System.out.println("Frequency of digit 2 in the number " + myNumber + " is : " + frequencyDigits(myNumber, findTheDigit));
}
}
...

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy