Programming Fundamentals Assignments

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Programming Fundamentals Assignments

Share Link:
https://goo.gl/E7X1z3
A. Print following patterns on screen with appropriate logic:

1.

1
12
123
1234
12345

2.

12345
1234
123
12
1

3.

*
**
***
****
*****

4.

12345
2345
345

Page 1 of 5
45
5

5. Tables of numbers 1 to 10.

B. Complete the following assignments:

6. Write a program to print factorial of a given number.

7. Write a program to print prime numbers between 1 and 100.

8. Write a program to check whether a given number is palindrome or not.

C. Write code for the following problems .

1. Find bigger number between two given numbers.


2. Find smaller number between two given numbers using conditional operator.
3. Accept an integer number for the user and print:
a. Smallest even number, not smaller than the given number
b. Biggest odd number, not bigger than the given number
4. Generate five Random integer numbers between 1 to 100
5. Accept students marks from user and display a congratulations message according to the
marks obtained by student.
6. Find SUM AND PRODUCT of the digits in a given number.
7. Find Factorial of the Given number.
8. Reverse the given number.
9. Find Fibonacci series of a given number.
10. Find sum of all integers greater than 100 and less than 200 that are divisible by 7.
11. Display Multiplication Table of the given number.
12. Swap the values of two given numbers.
13. Convert given number of days into months and days.
a. (Assume that each month is of 30 days)
14. Generate a Triangle.
15. Find whether number is Prime or Not.

Page 2 of 5
D. Write Java programs for the following case studies:

Case Study -1

Suppose you want to develop a program that changes a given amount of money into smaller
monetary units. The program lets the user enter an amount as a double value representing a
total in dollars and cents, and outputs a report listing the monetary equivalent in the maximum
number of dollars, quarters, dimes, nickels, and pennies, in this order, to result in the minimum number
of coins.
Here are the steps in developing the program:

1. Prompt the user to enter the amount as a decimal number, such as 11.56.
2. Convert the amount (e.g., 11.56) into cents (1156).
3. Divide the cents by 100to find the number of dollars. Obtain the remaining cents using
the cents remainder 100.
4. Divide the remaining cents by 25 to find the number of quarters. Obtain the remaining
cents using the remaining cents remainder 25.
5. Divide the remaining cents by 10 to find the number of dimes. Obtain the remaining
cents using the remaining cents remainder 10.
6. Divide the remaining cents by 5to find the number of nickels. Obtain the remaining
cents using the remaining cents remainder 5.
7. The remaining cents are the pennies.
8. Display the result.

Case Study-2(Determining Leap year)

A year is a leap year if it is divisible by 4but not by 100, or if it is divisible by 400.


You can use the following Boolean expressions to check whether a year is a leap year:
// A leap year is divisible by 4

booleanisLeapYear = (year % 4== 0);


// A leap year is divisible by 4 but not by 100
isLeapYear = isLeapYear && (year % 100!= 0);
// A leap year is divisible by 4 but not by 100 or divisible by 400
isLeapYear = isLeapYear || (year % 400== 0);
Or you can combine all these expressions into one like this:
isLeapYear = (year % 4== 0&& year % 100!= 0) || (year % 400== 0);

Case Study-3: Guessing Birthdays

You can find out the date of the month when your friend was born by asking five questions.
Each question asks whether the day is in one of the five sets of numbers.
Key

Page 3 of 5
The birthday is the sum of the first numbers in the sets where the day appears. For example,
if the birthday is 19, it appears in Set1, Set2, and Set5. The first numbers in these three sets
are1,2, and 16. Their sum is 19.
Listing 4.3 gives a program that prompts the user to answer whether the day is in Set1
(lines 41–44), in Set2 (lines 50–53), in Set3 (lines 59–62), in Set4 (lines 68–71), and in Set5
(lines 77–80). If the number is in the set, the program adds the first number in the set to day
(lines 47, 56, 65, 74, 83)

Case Study-4: Finding the Greatest Common Divisor

The greatest common divisor (gcd) of the two integers 4and 2is 2. The greatest common
divisor of the two integers 16 and 24is 8. How would you write this program to find the greatest common
divisor? Would you immediately begin to write the code? No. It is important to
think before you code. Thinking enables you to generate a logical solution for the problem
without concern about how to write the code.

Let the two input integers be n1 and n2. You know that number 1 is a common divisor, but
it may not be the greatest common divisor. So, you can check whether k(for k=2,3,4, and
so on) is a common divisor for n1 and n2, until kis greater than n1 or n2. Store the common
divisor in a variable named gcd. Initially, gcd is 1. Whenever a new common divisor is found,
it becomes the new gcd. When you have checked all the possible common divisors from 2up
To n1 or n2, the value in variable gcd is the greatest common divisor. Once you have a logical
solution, type the code to translate the solution into a Java program as follows:
Int gcd = 1;// Initial gcd is 1
intk = 2;// Possible gcd
while(k <= n1 && k <= n2) {
if(n1 % k == 0&& n2 % k == 0)
gcd = k; // Update gcd
k++; // Next possible gcd
}
// After the loop, gcd is the greatest common divisor for n1 and n2
Case Study – 5(Analyzing Numbers(Arrays))

The problem is to read 100 numbers, get the average of these numbers, and find the
number of the items greater than the average. To be flexible for handling any number of input, we

Page 4 of 5

You might also like