Question

Use the IO module for all inputs and outputs. Sevens are considered lucky numbers. Your task...

Use the IO module for all inputs and outputs. Sevens are considered lucky numbers. Your task is to count the number of sevens that appear within a range of integer numbers. Your solution should make use of looping constructs.

Ask the user for the following information, in this order:

The lower end of the range

The upper end of the range

Compute and then output the number of sevens that appear in the sequence from lower end to upper end (inclusive). Hint: Some numbers have more than 1 seven, and not every 7 appears in the ones place. Hint2: Nested loops are helpful On error output -1 using the IO module.

USE IO.readint(), IO.readDouble()...etc.

NONE of the scan.next

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have given the implementation of Java program to count the lucky numbers between a range.

Copy the below java file in your editor and test.

DigitCount.java

import java.util.Scanner;

public class DigitCount {

//static function to calc the luckynum count

static int luckyNumCount(int min, int max, int luckyNum) {

// initialize result

int result = 0;

// One by one compute sum of digits

// in every number from 1 to n

for (int x = min; x <= max; x++)

//sum all the luckynums

result += calcCount(x, luckyNum);

return result;

}

//get count of luckynum for the given num (ex :{7-1, 17-1, 77-2, 777-3})

static int calcCount(int x, int luckyNum) {

int count = 0, r;

while (x > 0) {// repeat till the x becomes 0

r = x % 10;// get remainder of x into r

x = x / 10; // get quotient of x into x

if (r == luckyNum)

count++;

}

return count;

}

// Driver Program

public static void main(String args[]) {

//luckynum to be counted

int luckyNum = 7;

Scanner s = new Scanner(System.in);

System.out.print("Enter the lower end value of the range : ");

int min = s.nextInt();

System.out.print("Enter the upper end value of the range : ");

int max = s.nextInt();

System.out.println("Count of numbers from " + min + " to " + max + " that have " + luckyNum

+ " as a digit is " + luckyNumCount(min, max, luckyNum));

}

}

Output :

Enter the lower end value of the range : 1
Enter the upper end value of the range : 80
Count of numbers from 1 to 80 that have 7 as a digit is 18

1 import java.util.Scanner; 2 3 public class DigitCount 4 //static function to cals the luckynum count 5 static int luckyNumC«terminated> DigitCount Java Application C:\Program Files Javallrel .8.0 144 bin yavaw.exe (28-Feb-2018,6:49:2 Enter the lowe

Add a comment
Know the answer?
Add Answer to:
Use the IO module for all inputs and outputs. Sevens are considered lucky numbers. Your task...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You may import the following library functions in your module: from fractions import gcd from math...

    You may import the following library functions in your module: from fractions import gcd from math import log from math import floor You may also use: • the .bit_length() method to efficiently obtain the bit length of an integer, • the abs() function for computing the absolute value of an integer, • and the // operator for integer division (you should avoid using / because it does not work for very large integers). Implement the following Python functions. These functions...

  • Use only if else nested if else only otherwise your answer won't be entertained. Problem 1(b):...

    Use only if else nested if else only otherwise your answer won't be entertained. Problem 1(b): Write a program that gives remainder without using modulus operator and loops. The first number entered will be dividend and second number entered will be divisor. Sample input: 15 6 Sample output: Remainder is 3 In a right triangle, the square of the length of one side is equal to the sum of the squares of the length of the other two sides. Write...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below...

    First, read the article on "The Delphi Method for Graduate Research." ------ Article is posted below Include each of the following in your answer (if applicable – explain in a paragraph) Research problem: what do you want to solve using Delphi? Sample: who will participate and why? (answer in 5 -10 sentences) Round one questionnaire: include 5 hypothetical questions you would like to ask Discuss: what are possible outcomes of the findings from your study? Hint: this is the conclusion....

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT