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

Exercises01

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Exercises01

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Exercises with Java

Using Java, solve the following Questions.


1.Write a program to print numbers from 1 to 10. Use for loop
2. Write a program to calculate the sum of first 10 natural
number. Use while loop
3. Write a program to find the factorial value of any number
entered through the keyboard.
4. Write a do-while loop that asks the user to enter two numbers.
The numbers should be added and the sum displayed. The loop
should ask the user whether he or she wishes to perform the
operation again. If so, the loop should repeat; otherwise it should
terminate.

5. Draw the following Christmas tree. The program should ask the
user the length of the tree.

6. Write a program to read a weekday number and print weekday


name using switch statement
7. Write a program to check whether a number is negative,
positive or zero

1. Write a program to print numbers from 1 to 10. Use for loop


2. Write a program to calculate the sum of first 10 natural
number. Use while loop
3. Write a program to find the factorial value of any number
entered through the keyboard.
4. Write a do-while loop that asks the user to enter two
numbers. The numbers should be added and the sum
displayed. The loop should ask the user whether he or she
wishes to perform the operation again. If so, the loop should
repeat; otherwise it should terminate.

5. Draw the following Christmas tree. The program should ask


the user the length of the tree.

6. Write a program to read a weekday number and print


weekday name using switch statement
7. Write a program to check whether a number is negative,
positive or zero
8. Write a program in JAva to swap two numbers.
Example of output
Input 1st number : 25
Input 2nd number : 39
After swapping the 1st number is : 39
After swapping the 2nd number is : 25
9. Compute the sum of all even numbers less than 100
10. Compute the product of all odd numbers between 1 and
20.
11. Compute the product of prime numbers between 1 and
100.
12. Write a function that prints all integer pairs(a,b) greater
than 1 and less than 100 that can satisfy the hypotenuse
rule. Hypotenuse should be also integer. Display the number
of pairs found. Treat (3,4) and (4,3) as one pair.
13. Given a, b,c indices of a quadratic function , find the
roots x1, x2 of the equation using the following:

14. Write a program with a function to convert


temperature in Celsius to Fahrenheit.
Hints
How to convert Celsius temperatures to Fahrenheit
Multiply the Celsius temperature by 9/5.
Add 32o to adjust for the offset in the Fahrenheit
scale.

15. Write a program with a function to compute quotient


and remainder of two numbers accepted from keyboard.

16. Write a program with a function to check whether a


number is positive, negative or zero.

17. Write a Java program with a function to display the


current date and time
18. Write a program in jAVA with a function which accepts
the user's first and last name and print them in reverse
order with a space between them.

Sample output
Input First Name: Mugisha
Input Last Name: Sam
Name in reverse is: Sam Mugisha
19. Write a program which accepts the radius of a circle
from the user and compute the area and circumference. The
area and circumference must be in separate functions.
20. Write a program with a function to compute the volume
of the cube on user input.
21. Write a program with a function to read three integers
and to print them in ascending order
22. Write a program with a function that is required that
checks strings to see if they are palindromes. A palindrome
string that reads the same, backwards, or forwards. For
example,
I was able no on elba saw I

23. John is 1,000,000,000 seconds old; how old is John in


terms of Years.
24. Build a program with function that checks the number
of spaces in a given string.
25. Build a program with function that check if an entered
character is a digit or alpha letter.
26. Build a word count function which display the following
through input provided by the user:
i. Number of characters without spaces
ii. Number of characters with spaces
iii. Number of words
27. Build a program with function that reverse a given
string
28. Build a program with function that uses a while loop to
calculate the length of a string without using built-in
functions.
29. Write a function that checks if a given string is a
palindrome with and without built in functions.
30. Write a program with function that converts the
entered string to Uppercase
31. Write a program with function that removes all spaces
in a string and print the resulting string.
32. Write a Java program that will display the calculator
menu.
The program will prompt the user to choose the operation
choice (from 1 to 5). Then it asks the user to input two
integer values for the calculation.
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27

Continue? y

The program also asks the user to decide whether he/she


wants to continue the operation. If he/she input ‘y’, the
program will prompt the user to choose the operation again.
Otherwise, the program will terminate.

PART B: MEDIUM

33. Given a number n, determine what the nth prime is. By


listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we
can see that the 6th prime is 13. If your language provides
methods in the standard library to deal with prime numbers,
pretend they don't exist and implement them yourself.
34. Write program with function named minDistance that
returns the smallest positive distance between two factors of
a number. For example, consider 13013 = 1*7*11*13. Its
factors are 1, 7, 11, 13 and 13013. minDistance(13013)
would return 2 because the smallest positive distance
between any two factors is 2 (13 - 11 = 2). As another
example, minDistance (8) would return 1 because the
factors of 8 are 1, 2, 4, 8 and the smallest positive distance
between any two factors is 1 (2 – 1 = 1).

Hint:Converting a number into a string or using any built-in


function.

35. Write a program with a function named countDigit


that returns the number of times that a given digit appears
in a positive number. For example countDigit(32121, 1)
would return 2 because there are two 1s in the number
32121. Other examples:

countDigit(33331, 3) returns 4

countDigit(33331, 6) returns 0

countDigit(3, 3) returns 1

The function should return -1 if either argument is negative,


so

countDigit(-543, 3) returns -1.

Hint:Converting a number into a string or using any built-in


function yield in zero.

36. Write a program to find the difference between the


square of the sum and the sum of the squares of the first N
natural numbers.
The square of the sum of the first ten natural numbers is (1
+ 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1²


+ 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the


first ten natural numbers and the sum of the squares of the
first ten natural numbers is 3025 - 385 = 2640.

37. An array with an odd number of elements is said to be


centered if all elements (except the middle one) are strictly
greater than the value of the middle element. Note that only
arrays with an odd number of elements have a middle
element. Write a program with a function that accepts an
integer array and array size and returns 1 if it is a centered
array, otherwise it returns 0.

Examples

If the input Then the function returns


array is

{1, 2, 3, 4, 5} 0 (the middle element 3 is not strictly less than


all other elements)

{3, 2, 1, 4, 5} 1 (the middle element 1 is strictly less than all


other elements)

{3, 2, 1, 4, 1} 0 (the middle element 1 is not strictly less than


all other elements)

{1, 2, 3, 4} 0 (no middle element)

{} 0 (no middle element)


38. Write a program with a function that takes an
array of integers as an argument and returns the
difference of the sum of odd numbers and the sum of
even numbers in the array. Let X = the sum of the odd
numbers in the array and let Y = the sum of the even
numbers. The function should return X – Y

Examples

if input array retur


is n

{1} 1

{1, 2} -1

{1, 2, 3} 2

{1, 2, 3, 4} -2

{3, 3, 4, 4} -2

{3, 2, 3, 4} 0

{4, 1, 2, 3} -2

{1, 1} 2

{} 0

39. Write a program with a function that reverse an


integer.

if the input integer is return


1234 4321
12005 50021
1 1
1000 1
0 0
-12345 -54321

40. Convert a number into a string that contains raindrop


sounds corresponding to certain potential factors. A factor is
a number that evenly divides into another number, leaving
no remainder. The simplest way to test if a one number is a
factor of another is to use the modulo operation. The rules of
raindrops are that if a given number:
● has 3 as a factor, add 'Pling' to the result.
● has 5 as a factor, add 'Plang' to the result.
● has 7 as a factor, add 'Plong' to the result.
● does not have any of 3, 5, or 7 as a factor, the result
should be the digits of the number.

Examples

● 28 has 7 as a factor, but not 3 or 5, so the result would be


"Plong".
● 30 has both 3 and 5 as factors, but not 7, so the result
would be "PlingPlang".
● 34 is not factored by 3, 5, or 7, so the result would be
"34".

You might also like