Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CSc 1301 Lab #9 Topics: Scanner – user input, if-else, cumulative sum and product Problem 1: · Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that number. ·...

1 answer below »
CSc 1301
Lab #9
Topics: Scanner – user input, if-else, cumulative sum and product
Problem 1:
· 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 XXXXXXXXXXand get 19
· Hint: Use the % operator to extract a digit from a number.
· Use loop(s)
Problem 2:
· 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, if-else, and loop to solve this problem. You can assume user inputs positive number.
·
Problem 3:    
· 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. We will learn Strings later)
Problem 4:
Write a program that shows the factors of 2 in a given positive integer (user input) in the range of [16,128]. For example, if user inputs 7, 18, 68, 120, the co
ectly working program should print the following:
7 = 7
18 = 2 * 9
68 = 2 * 2 * 17
120 = 2 * 2 * 2 * 15
Problem GCD, extra.
Write a program called gcd that accepts two positive integers in the range of [32,256] as parameters and prints the greatest common divisor (GCD) of the two numbers.
The GCD of two integers a and b is the largest integer that is a factor of both a and b. One efficient way to compute the GCD of two numbers is to use Euclid’s algorithm, which states the following:
GCD (a, b) _ GCD (b, a % b)
GCD (a, 0) _Absolute value of a
Page 2 of 5
Answered 1 days After Mar 12, 2021

Solution

Sudipta answered on Mar 13 2021
153 Votes
Ans 1.
package richard;
import java.util.*;
public class digitSum {
    public static void main(String[] args) {
        
        
taking input using scanner class
        Scanner sc = new Scanner (System.in);
        System.out.print("Enter any 5 digit number: ");
        int num=sc.nextInt();
        int digit=0;
        int sum=0;
        
using while loop to take each digit and add to previous sum
        while (num>0) {
            digit=num%10;
            num=num/10;
            sum+=digit;
        }
        System.out.println(sum);
    }
}
Output Screenshot:
Ans 2.
package richard;
import java.util.Scanner;
public class prime {
    public static void main(String[] args) {
        
        
taking input using scanne
        Scanner sc= new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num=sc.nextInt();
        int c=0;
        
using for loop to check if any number other than 1 and itself divides the numbe
        for(int...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here