Assignment Work Codes
Assignment Work Codes
Example Output
Example Output
Example Output
Example Output
Example Output
Example Output
a = a + b;
b = a - b;
a = a - b;
sc.close();
}
}
Example Output
java
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter length: ");
double length = sc.nextDouble();
System.out.print("Enter breadth: ");
double breadth = sc.nextDouble();
System.out.print("Enter height: ");
double height = sc.nextDouble();
double cuboidVolume = length * breadth * height;
System.out.println("Volume of Cuboid: " + cuboidVolume);
break;
case 2:
System.out.print("Enter radius: ");
double radius = sc.nextDouble();
System.out.print("Enter height: ");
height = sc.nextDouble();
double cylinderVolume = Math.PI * radius * radius * height;
System.out.println("Volume of Cylinder: " + cylinderVolume);
break;
case 3:
System.out.print("Enter radius: ");
radius = sc.nextDouble();
System.out.print("Enter height: ");
height = sc.nextDouble();
double coneVolume = (1.0/3) * Math.PI * radius * radius * height;
System.out.println("Volume of Cone: " + coneVolume);
break;
default:
System.out.println("Invalid choice!");
}
sc.close();
}
}
Example Output
java
import java.util.Scanner;
Example Output:
Enter value of p: 4
Enter value of q: 2
p^q: 16.0
Square root of p: 2.0
Cube root of q: 1.2599210498948732
java
import java.util.Scanner;
Example Output:
java
import java.util.Scanner;
if (num > 0) {
posCount++;
posSum += num;
} else if (num < 0) {
negCount++;
}
}
Example Output:
java
import java.util.Scanner;
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
Example Output:
java
import java.util.Scanner;
if (sum == originalNum)
System.out.println(originalNum + " is an Armstrong number.");
else
System.out.println(originalNum + " is not an Armstrong number.");
}
}
Variable Type Description
num int Input number
originalNum int Copy of the input number
sum int Sum of powered digits
digits int Number of digits in the input
digit int Extracted digit from input
Example Output:
java
import java.util.Scanner;
int reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
System.out.println("Reversed number: " + reversed);
}
}
Variable Type Description
num Input number
int
reversed int Reversed number
Example Output:
java
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isComposite = false;
if (isComposite)
System.out.println(num + " is a composite number.");
else
System.out.println(num + " is not a composite number.");
break;
case 2:
System.out.print("Enter a number: ");
int number = sc.nextInt();
int smallest = 9;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
Variable Type Description
choice int User's selected menu option
Variable Type Description
num int Input number for composite check
isComposite boolean Flag indicating if the number is composite
number int Input number for smallest digit check
smallest int Stores smallest digit found
Example Output:
Menu:
1) Check if a number is composite
2) Find the smallest digit in a number
Enter your choice: 1
Enter a number: 9
9 is a composite number.
Menu:
1) Check if a number is composite
2) Find the smallest digit in a number
Enter your choice: 2
Enter a number: 6524
Smallest digit is: 2
java
public class PatternGenerator {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((j % 2 == 1) ? "*" : "#");
System.out.print(" ");
}
System.out.println();
}
}
}
Variable Type Description
rows int Number of rows in the pattern
i int Loop for rows
j int Loop for alternating characters
Example Output:
*
* #
* # *
* # * #
* # * # *
java
public class NumberSequencePattern {
public static void main(String[] args) {
int num = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
Variable Type Description
num int Number sequence counter
i int Loop for rows
j int Loop for numbers in each row
Example Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15