Sunday, June 26, 2022

Numbers with the digit d

 Numbers with the digit d

Problem Statement
Given two integers N and D, your task is to find the numbers between 1 to N which contains the digit D at least 1 time.

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework


class Main {
    static boolean isDigitPresent(int x, int d)
    {
        // Break loop if d is present as digit
        while (x > 0)
        {
            if (x % 10 == d)
                break;
 
            x = x / 10;
        }
 
        // If loop broke
        return (x > 0);
    }
 
    // function to display the values
    static void printNumbers(int n, int d)
    {
        // Check all numbers one by one
        for (int i = 0; i <= n; i++)
 
            // checking for digit
            if (i == d || isDigitPresent(i, d))
                System.out.print(i + " ");
    }
 
 
 
    public static void main(String[] args)
    {
        Scanner sc= new Scanner(System.in);

        int n =sc.nextInt();
        int d =sc.nextInt() ;
        printNumbers(n, d);
    }
}

No comments:

Post a Comment

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...