0% found this document useful (0 votes)
46 views

Al Jouf University Department of Information Systems Dr. Abd El-Aziz Ahmed Java Programming Fall 2018 Practice Lab 2 (Selection and Loops)

This document contains 37 practice problems for a Java programming lab focusing on selection and loops. The problems cover a range of concepts including obtaining user input, comparing values, calculating sums/averages, determining divisibility, working with strings and characters, and more. Students are instructed to write Java programs that prompt for user input to solve problems like determining the larger of two numbers, calculating factorials, and counting vowels/consonants in strings.

Uploaded by

wiem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Al Jouf University Department of Information Systems Dr. Abd El-Aziz Ahmed Java Programming Fall 2018 Practice Lab 2 (Selection and Loops)

This document contains 37 practice problems for a Java programming lab focusing on selection and loops. The problems cover a range of concepts including obtaining user input, comparing values, calculating sums/averages, determining divisibility, working with strings and characters, and more. Students are instructed to write Java programs that prompt for user input to solve problems like determining the larger of two numbers, calculating factorials, and counting vowels/consonants in strings.

Uploaded by

wiem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Al Jouf University

Department of Information Systems


Dr. Abd El-Aziz Ahmed
Java Programming
Fall 2018
Practice Lab 2 (selection and loops)

1. Write a java program that asks the user to enter two integers, obtains the numbers from the
user, hence prints the larger number followed by the words "is larger." If the numbers are
equal, print the message “These numbers are equal.”.

2. Write a java program that accepts three integers from the keyboard and prints the sum,
average, product, smallest, and largest of these numbers.

3. Write a java program that reads in five integers and determines and prints the largest and the
smallest integers in the group.

4. Write a program that prompts the user to enter three integers and display the integers in non-
decreasing order.

5. Write a java program to solve ax2+bx+c=0.

6. Write a java program that reads two integers and determines and prints if the first is a multiple

of the second.

7. Write a program that randomly generates an integer between 1and 12 and displays the English
month name January, February, …, December for the number 1, 2, …, 12, accordingly.

8. Write a program that prompts the user to enter an integer for today’s day of the week (Sunday
is 0, Monday is 1, …, and Saturday is 6). Also prompt the user to enter the number of days after
today for a future day and display the future day of the week.
9. An ISBN-10 (International Standard Book Number) consists of 10 digits:
d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the other
nine digits using the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11. If the
checksum is 10, the last digit is denoted as X according to the ISBN-10 convention. Write a
program that prompts the user to enter the first 9 digits and displays the 10-digit ISBN (including
leading zeros). Your program should read the input as an integer.

10. Write a program that prompts the user to enter the month and year and displays the number
of days in the month. For example, if the user entered month 2 and year 2012, the program
should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the
program should display that March 2015 had 31 days.

11. Write a program that prompts the user to enter a three-digit integer and determines whether it
is a palindrome number. A number is palindrome if it reads the same from right to left and from
left to right.

12. Write a program that reads three edges for a triangle and computes the perimeter if the input
is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of
two edges is greater than the remaining edge.

13. Write a program that prompts the user to enter an integer and determines whether it is
divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not
both.

14. Write a progrm to read a number, then do the following:

1. Check if the number is greater than 20 do the following:


a. If the number is equal to 24 then display "you entered 24".

b. If the number is equal to 23 then display "you entered 23".

Else of the above then do the following


Display the next text "the number is greater than 20 but not 24 nor 23".

c. If the number is divisible by 5 and divisible by 3 then

Display" you entered a number divisible by 5 and by 3

Else print on the screen "not divisible by 5 or by 3".

2. If the number is less than 20 or equal 20 print the number to the screen.

15. Write a program to read 3 numbers from the user and then ask the user to select from the
following options:
1- Display all odds numbers.
2- Display the average of the even numbers.
3- Count numbers divisible by 5.
4- Display the maximum number.
16. Write a program to do the following:
Ask the user to enter 3 values.
Ask the user to enter a number N.
Ask the user to enter a number M.
Calculate the average of values divisible by N.
Calculate the average of values divisible by M.
Display the difference between the two averages.

17. Write a java program to read a value and print its factorial.

18. Write a java program to read two values x and y and print xy .

19. Write a java program to read n values and find the average of the values divisible by 5.
20. Write a java program to read a value and determine if it is a prime or not.
21. Write a java program to read a value and determine if it is a perfect or not.
22. Write a java program to convert from decimal to binary.
23. Write a java program to print the multiplication table, where the multiplication table for 2, 3,
…, 12 appear after pressing enter.
24. Write an encryption/decryption program for encrypting a message using exclusive or
operator. The program asks the user about the operation (encryption or decryption), message,
and key in bit pattern, and it prints the encrypted/decrypted message.

25. Write a program that reads an unspecified number of integers, determines how many positive
and negative values have been read, and computes the total and average of the input values (not
counting zeros). Your program ends with the input 0. Display the average as a floating-point
number.

26. Suppose that the tuition for a university is $10,000 this year and increases 5% every year. In
one year, the tuition will be $10,500. Write a program that computes the tuition in ten years and
the total cost of four years’ worth of tuition after the tenth year.

27. Write a program that prompts the user to enter the number of students and each student’s
score, and finally displays the student with the highest score.

28. Write a program that displays all the numbers from 100 to 1,000, ten per line, that are
divisible by 5 and 6. Numbers are separated by exactly one space.

29. find the greatest common divisor of two integers n1 and n2 is as follows: First find d to be
the minimum of n1 and n2, then check whether d, d-1, d-2, . . . , 2, or 1 is a divisor for both n1
and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2.
Write a program that prompts the user to enter two positive integers and displays the gcd.

30. Write a program that prompts the user to enter an integer from 1 to 15 and displays a
pyramid, as
31. Use nested loops that display the following patterns in four separate programs:

32. Write a program that lets the user enter the loan amount and loan period in number of years
and displays the monthly and total payments for each interest rate starting from 5% to 8%, with
an increment of 1/8.

33. Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by
exactly one space. Also display the number of leap years in this period.

34. Write a program that prompts the user to enter a decimal integer and displays its
corresponding binary value. Don’t use Java’s IntegertoBinaryString(int) in this program.

35. Write a program that prompts the user to enter a decimal integer and displays its
corresponding octal value. Don’t use Java’s IntegertoOctalString(int) in this program.

36. Write a program that prompts the user to enter a string and displays the string in reverse
order.

37. Write a program that prompts the user to enter a string and displays the characters at odd
positions.

38. Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to
enter a string and displays the number of vowels and consonants in the string.
Scanner in=new Scanner (System.in);
int num=in.nextInt();

You might also like