Question

 Write a program to find the prime numbers - Ask user to input the integer...

 Write a program to find the prime numbers

- Ask user to input the integer number

- test the number whether it is a prime number or not

- Then, print “true” or “false” depending on whether the number is prime or isn’t.

- Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime.

- Use idea of user input, cumulative sum, and loop to solve this problem. You can assume user inputs positive number.

• Ask user to input two integer parameters a, and b.

– Assume that a is a positive integer (>0) and b is single-digit numbers from 0-9 inclusive.

– Your program should print:

• true if a contains b at among its digits, and

• false otherwise.

– For example when a is 3415, b is 1, should print true.

– (Note: You can not use a String to solve this problem. )

Homework Answers

Answer #1

PROGRAMMING LANGUAGE - JAVA

PART 1

CODE -

import java.util.Scanner;

public class prime {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        // Initialize count of factors to 0

        int countFactors = 0;

        int num;

        // Take a number as input from user

        System.out.print("Enter a number: ");

        num = keyboard.nextInt();

        // Loop to count number of factors

        for(int i=1; i<=num; i++)

            // If the number is divisible by a number increase the count of factors

            if(num%i == 0)

                countFactors += 1;

        // Number is prime if number of factors is equal to 2

        if(countFactors == 2)

            System.out.println("True");

        else

            System.out.println("False");

        keyboard.close();

    }

}

SCREENSHOT -

PART 2

CODE -

import java.util.Scanner;

public class containDigit {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int num, digit;

        // Take a positive number as input from the user

        System.out.print("Enter a positive integer: ");

        num = keyboard.nextInt();

        // Take a single-digit positive integer as input from user

        System.out.print("Enter a single-digit integer(0-9): ");

        digit = keyboard.nextInt();

        // Initialize contain variable to false

        boolean contain = false;

        // Loop to check if the number contain the entered number among its digit

        while(num>0)

        {

            int d = num%10;

            // Set the variable contain to true if the current digit of the number is equal to the second number(entered by user)

            if (digit == d)

            {

                contain = true;

                break;

            }

            num = num/10;

        }

        System.out.println(contain);

        keyboard.close();

    }

}

SCREENSHOT -

If you have any doubt regarding the solution or if you want the solution to be in a different programming language, then do comment.
Do upvote.

Know the answer?
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
Similar Questions
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
C++ Write a program that will ask the user to input n positive numbers. The program...
C++ Write a program that will ask the user to input n positive numbers. The program will terminate if one of those number is not positive. please respond quickly and clearly, thank you!
Write a program in python that prints the count of all prime numbers between A and...
Write a program in python that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2,...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that number. Assume that the number is positive, integer, 5-digit. Example: 29107 should calculate 2+9+1+0+7 and get 19 Hint: Use the % operator to extract a digit from a number. Use loop(s) Must be in Java
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
JAVA Write an application that will input four positive integers from the user. The program should...
JAVA Write an application that will input four positive integers from the user. The program should print out the largest and smallest number. It should print how many of the numbers are even and how many are odd. It should print how many of the numbers are one digit and how many of the numbers are two digit. It should print out the sum of all even numbers and the sum of all odd numbers. It should print out the...
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...