Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help me solve this problem on Racket In this problem, you will write fun

ID: 3588399 • Letter: C

Question

Can you help me solve this problem on Racket

In this problem, you will write functions that examine the individual digits of an Integer. For instance, the number 122333 is both a number over one hundred thousand, and a number comprised of six digits: one 1, two 2s, and three 3s when viewed as a decimal number.

We can use basic mathematical operations to access the individual digits of any such number, by exploiting how the place-value system (also known as "positional notation") works: think about how to get the digit in the ones place, and how to get the set of digits that result from "chopping off" the ones place using simple operations.

If we want to investigate a property of the digits of an arbitrary number, we would need to examine the digits in turn, but we cannot anticipate how many there are in advance. Recursion gives us the ability to scan down the digits, however many there may be.

We need to be precise about how to handle zeros in the decimal representation of a number. For instance, the number 15100 has two zeros. But the exact same number can be written as 015100 -- this has the same mathematical value, but appears to have three zeros. These so-called "leading zeros" do not change the value of the number, and we can have arbitrarily many of them, confusing the issue of counting the number of zeros in a number. For this problem, we will never attempt to count leading zeros.

On a related note, consider the number 0 itself. We write a single digit, the digit 0, when we write this number. But it serves almost as a placeholder to show that there is a number being written; arguably this digit is itself a leading zero in front of a blank number. So, we will consider the number 0 to have no digits at all, for the purposes of this problem.

Write the following functions:

contains-digit?, which takes in an Integer, representing a number, and a second Integer, representing a single digit, and evaluates to true if and only if at least one copy of that digit is present in the number; it is an error to be given a digit outside of the range 0-9

Explanation / Answer

/*DigitChecking.java*/

Program:

import java.util.Scanner;

public class DigitChecking {

     public static void main(String[] args) {

         

          Scanner sc = new Scanner(System.in);

          System.out.println("Enter Number:");

          int number = sc.nextInt();

          System.out.println("Enter digit in the range of 0-9:");

          int digit = sc.nextInt();

          boolean flag = contains_digit(number,digit);

          if(flag)

          {

              System.out.println("The number "+number+" contains digit: "+digit);

          }

          else

          {

              System.out.println("The number "+number+" don't have digit: "+digit);

          }

          String str[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

         

          System.out.println("The above number has:");

          int res[]= scan_number(number);

          for(int i=0;i<res.length;i++){

              if(res[i]>=1){

                   System.out.println(str[i]+": "+res[i]);

              }

          }

     }

     public static boolean contains_digit(int number,int digit)

     {

          boolean flag = false;

          while(number!=0){

              int rem = number % 10;

              number = number /10;

              if(rem == digit){

                   flag = true;

                   break;

              }

          }

          return flag;

     }

     public static int[] scan_number(int number)

     {

          int arr[] = new int[10];

          for(int i=0;i<arr.length;i++){

              arr[i] = 0;

          }

          while(number!=0){

              int rem = number % 10;

              number = number/10;

              arr[rem] = ++arr[rem];

          }

          return arr;

     }

}

Output:

Enter Number:

168795867

Enter digit in the range of 0-9:

6

The number 168795867 contains digit: 6

The above number has:

one: 1

five: 1

six: 2

seven: 2

eight: 2

nine: 1