0% found this document useful (0 votes)
11 views3 pages

Java Programming Assignment 1

The document provides 14 Java programming assignments involving variables, conditionals, loops, functions, and other basic Java concepts. The assignments include swapping variable values, calculating averages and ranks, solving linear equations, finding factorials and Fibonacci numbers, managing student data, determining number of days in a month, finding greatest common divisors, printing multiplication tables, and printing various patterns using loops and nested loops.

Uploaded by

datmtpd10529
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)
11 views3 pages

Java Programming Assignment 1

The document provides 14 Java programming assignments involving variables, conditionals, loops, functions, and other basic Java concepts. The assignments include swapping variable values, calculating averages and ranks, solving linear equations, finding factorials and Fibonacci numbers, managing student data, determining number of days in a month, finding greatest common divisors, printing multiplication tables, and printing various patterns using loops and nested loops.

Uploaded by

datmtpd10529
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

Java Assignment

JAVA ASSIGNMENT -1

1. Create two integer variables (a and b) after that assign value for them. Then swap
value of a and b.
Hint: Using a c variable to transfer value temporary.

2. Write a program to input three numbers as math, physics, and chemistry. Output
average and rank according to the following:

AVG Rank

>=8.0 A

>=6.5 B

>=5.0 C

<5.0 D

Hint: Declare average and rank variable and calculate value for them.

3. Write a program to find x from ax + b =0 equation with a and b are input from
keyboards.

4. Write a program to find x from ax2 + bx + c =0 equation with a and b are input from
keyboards.

5. Write a program to calculate sum of first 50 integer number.

6. Write a program accepts an integer number (n) from user the calculate n!(factorial of
n) then display the result to the screen.

7. Write a program to print to screen first 20 numbers of Fibonacci numbers.

8. Create an application to manage student’s information as below:


- Create variables to save a student’s information like: studentCode, studentName,
studentAge, studentSex and set value for them.
- Print student’s information to screen.
- Edit program to permit user inputs data from keyboard.

1
Professional Programmer Training Center
- Edit program to permit user inputs a list of student from keyboard until “n” key
pressed.

9. Write a program to accepts a number n from 1 to 12 and then print


number of days in month n of the current year. If user enter a number
out of range an error message will display.
Hint: Use switch case statement

10. Write program to accepts two integer numbers (a and b), then calculate
and display their greatest common divisor.
Hint: define function int greatestCommonDivisor(int a, int b)
Example: greatestCommonDivisor (18, 12) = 6;
(Ước số chung lớn nhất – Có thể xem thuật toán trên mạng)

11. Write a program print multiplication table.

12. Write a program to accept an integer number and print the rectangle as
below: (use loop statement)
Example:
N=3
***
***
***
N=4
****
****
****
****

13. Write a program to accept an integer number and print the triangle as
below: (use loop statement)
Example:
N=3
*
**
***

N=4
*
**

2 92-Quang Trung –Da Nang. Tel: 05113.888 279


Java Assignment
***
****

14. Write a program to accept an integer number and print the triangle as
below: (use loop statement)

Example:
N=3
*
**
***

N=4
*
**
***
****
The End

Common questions

Powered by AI

Use a loop to iterate 20 times, maintaining variables for previous two Fibonacci numbers and the current number. Start with initial values 0 and 1, then repeatedly add them to generate the next number in the sequence, updating the previous numbers each iteration. Print each number to the console as it is generated .

Input a month number from the user, 1 through 12. Implement a switch case structure where each case corresponds to a month and returns the number of days for that month. Include a default case for values outside the range to display an error message. Adjust for leap years in February using additional logic if the year is identified .

Create a class to store student information like studentCode, studentName, studentAge, and studentSex. Use a list or array to manage multiple students. Implement user input using a loop, prompting the user to enter details for each student until the 'n' key is pressed to stop. Input data should populate instances of the student class, and you should implement functionality to print the student list .

First, accept an integer input 'n' from the user. Initialize a long variable to 1, and use a loop to multiply this variable by each integer from 1 up to 'n'. This loops accordingly to calculate the factorial of 'n'. Output the resulting factorial value to the user .

First, gather inputs for 'a', 'b', and 'c' from the user. Calculate the discriminant using b^2 - 4ac. Depending on the discriminant value, calculate the roots: if positive, use the quadratic formula to compute two real roots; if zero, compute one real root; if negative, compute two complex roots. Output the calculated roots .

To find the greatest common divisor (GCD) of two numbers 'a' and 'b', you can use the Euclidean algorithm. Implement a recursive method 'int greatestCommonDivisor(int a, int b)' that returns 'b' when 'b' is zero, otherwise calls the method with parameters 'b' and 'a % b'. This efficiently calculates the GCD .

First, input values for 'a' and 'b' from the user. Check the condition that 'a' should not be zero to ensure it's a valid equation. If 'a' is not zero, solve for x using x = -b/a. Finally, output the value of x .

To swap two integer variables, say 'a' and 'b', using a third temporary variable 'c', you would perform the following steps: assign the value of 'a' to 'c', assign the value of 'b' to 'a', and finally assign the value of 'c' to 'b'. This effectively swaps the values of 'a' and 'b' .

Use a nested loop structure where the outer loop iterates over each row and the inner loop prints the required number of stars per row based on user input. For N rows of stars, each with N stars per row, iterate N times in both loops, printing a star followed by a newline character at the end of each inner loop .

First, create variables to store the three grades—math, physics, and chemistry. Calculate their average by dividing their sum by three. Then, use conditional statements to determine the rank based on the average: 'A' for average >= 8.0, 'B' for >= 6.5 and < 8.0, 'C' for >= 5.0 and < 6.5, and 'D' for averages below 5.0 .

You might also like