Search⌘ K
AI Features

Examples of Algorithms: Part 1

Explore how to apply algorithms in Java through practical coding examples. Learn to count specific elements in data, perform iterative arithmetic operations, and sort arrays. This lesson helps build foundational problem-solving techniques using algorithms in Java programming.

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 ...

Java
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));
}
}
...