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