🄰🄼🄸🅁♕ SulTaN, [Aug 22, 2023 at 10:44 PM]
Network programming assignment
1.
java
import java.util.Scanner;
public class CommonDivisor {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value for a: ");
int a = scanner.nextInt();
System.out.print("Enter the value for b: ");
int b = scanner.nextInt();
System.out.print("Enter the value for c: ");
int c = scanner.nextInt();
if (a != 0 && (b % a == 0) && (c % a == 0)) {
System.out.println(a + " is a common divisor of " + b + " and " + c);
} else {
System.out.println(a + " is not a common divisor of " + b + " and " + c);
scanner.close();
}
2-
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a temperature value: ");
double temperature = scanner.nextDouble();
System.out.print("Enter 'F' for Fahrenheit or 'C' for Celsius: ");
char unit = scanner.next().charAt(0);
if (unit == 'F' || unit == 'f') {
double celsius = (temperature - 32) * 5 / 9;
System.out.println(temperature + " degrees Fahrenheit is equivalent to " + celsius + " degrees
Celsius.");
} else if (unit == 'C' || unit == 'c') {
double fahrenheit = (temperature * 9 / 5) + 32;
System.out.println(temperature + " degrees Celsius is equivalent to " + fahrenheit + " degrees
Fahrenheit.");
} else {
System.out.println("Invalid input. Please enter 'F' or 'C'.");
System.exit(0);
scanner.close();
}
3.
java
import java.util.Scanner;
public class MovieCategory {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Display menu of movie categories
System.out.println("Movie Categories:");
System.out.println("A - Adventure movies");
System.out.println("C - Comedy movies");
System.out.println("F - Family movies");
System.out.println("H - Horror movies");
System.out.println("S - Science Fiction movies");
// Get user input
System.out.print("Enter the code for movie type: ");
char code = scanner.next().charAt(0);
// Display the category based on the code
switch (code) {
case 'A':
System.out.println("Adventure movies");
break;
case 'C':
System.out.println("Comedy movies");
break;
case 'F':
System.out.println("Family movies");
break;
case 'H':
System.out.println("Horror movies");
break;
case 'S':
System.out.println("Science Fiction movies");
break;
default:
System.out.println("Invalid code");
scanner.close();
4.
java
import java.util.Scanner;
public class IncomeTaxCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get user input for salary
System.out.print("Enter the salary: ");
double salary = scanner.nextDouble();
double incomeTax = 0;
// Calculate income tax based on salary
if (salary > 10000 && salary <= 50000) {
incomeTax = salary * 0.02;
} else if (salary > 50000 && salary <= 1000000) {
incomeTax = salary * 0.05;
} else if (salary > 1000000) {
incomeTax = salary * 0.10;
double netSalary = salary - incomeTax;
// Display salary, income tax, and net salary
System.out.println("Salary: " + salary);
System.out.println("Income Tax: " + incomeTax);
System.out.println("Net Salary: " + netSalary);
scanner.close();
5.
java
public class AlphabetDisplay {
🄰🄼🄸🅁♕ SulTaN, [Aug 22, 2023 at 10:44 PM]
public static void main(String[] args) {
for (char letter = 'A'; letter <= 'Z'; letter++) {
System.out.print(letter + " ");
}
}
6.
java
public class NumberFormat {
public static void main(String[] args) {
int num = 11;
int sum = 0;
System.out.println("____________");
System.out.println("num sum");
for (int i = 0; i < 4; i++) {
sum += num;
System.out.println(num);
num += i + 1;
System.out.println(sum + " " + (sum - 1));
7.
java
public class NumberAnalyzer {
public static void main(String[] args) {
// Check if 10 numbers are provided as command line arguments
if (args.length != 10) {
System.out.println("Please provide 10 whole numbers as command line arguments.");
return;
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
int sum = 0;
// Iterate through the command line arguments
for (String arg : args) {
int number = Integer.parseInt(arg);
// Update the largest number
if (number > largest) {
largest = number;
// Update the smallest number
if (number < smallest) {
smallest = number;
// Calculate the sum
sum += number;
// Display the largest, smallest, and sum
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
System.out.println("Sum: " + sum);
8.
java
public class FactorialSum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
sum += factorial;
System.out.println("Sum of the series: " + sum);
9.
java
public class NumberPattern {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
System.out.println();
10.
java
import java.util.Scanner;
public class AlphabetTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the height of the triangle: ");
int height = scanner.nextInt();
scanner.close();
// ASCII value of 'a'
int alphabet = 97;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((char) alphabet);
alphabet++;
}
System.out.println();
11.
java
public class Pattern {
public static void main(String[] args) {
int n = 9;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print("&");
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print(" ");
for (int j = 1; j <= n - i; j++) {
System.out.print("&");
System.out.println();
In this program, we use a loop to iterate over the rows of the pattern. The variable n represents the
number of rows in the pattern, which is set to 9 in this example.
Inside the loop, we have three nested loops.
The first loop (`j`) is responsible for printing the & symbol n - i times, where n is the total number of
rows and i is the current row number.
The second loop (`k`) is responsible for printing the spaces between the & symbols. The number of
spaces is calculated as 2 * i - 1, where i represents the current row number.
The third loop (`j`) is similar to the first loop and is responsible for printing the & symbol n - i times
again.