Computer Science, asked by muhil04, 5 months ago

Question
Total Occurrance
Given two integer numbers input1 and input2, find the total number of occurrences of input1 in a
series of natural numbers from 0 till input2.


Note:
1. input1 will be within 1-9 and
1. input2 will be within 1 – 100.
Prototype: public int findTotalOccurrence(int input1, int input2)

Example 1:

input1 = 2
input2 = 20
output = 3
Explanation: Number 2 is repeated 3 times in the series of natural numbers from 0 - 20 ie at 2,
12, 20.

Example 2:

input1 = 2
input2 = 30
output = 13
Explanation: Number 2 is repeated 13 times in the series of natural numbers from 0 – 30 i.e. at 2,
12, 20, 21, 22 (occurred twice), 23, 24, 25, 26, 27, 28, 29​

Answers

Answered by jamwantsingh112250
8

Answer:

Input : 22

Output : 6

Explanation: Total 2s that appear as digit

from 0 to 22 are (2, 12, 20,

21, 22);

Input : 100

Output : 20

Explanation: total 2's comes between 0 to 100

are (2, 12, 20, 21, 22..29, 32, 42, 52, 62, 72,

82, 92);

hope it help

Answered by vinod04jangid
0

Answer:

import java.util.*;

public class Main {

   public static int findTotalOccurrence(int input1, int input2){

       int result = 0;

       int itr = input2;

       while (itr <= input1){

           if (itr % 10 == input2)

               result++;

           if (itr != 0 && itr/10 == input2){

                   result++;

                   itr++;

               }

           else if (itr/10 == input2-1)

               itr = itr + (10 - input2);

           else

               itr = itr + 10;

       }

       return result;

   }

   public static void main (String[] args)

   {

       Scanner sc = new Scanner(System.in);

       int n = sc.nextInt();

       int d = sc.nextInt();

       System.out.println(findTotalOccurrence(d, n) );

   }

}

Explanation:

The first input that we have taken from the user is the number and the 2nd input is the range.

Then we have passed those values to a function findTotalOccurence() to calculate the number of times that particular digit occurred the provided range.

Then we check if the number is less than the range. After that we check few conditions if they satisfy then the result gets incremented.

#SPJ2

Similar questions