0% found this document useful (0 votes)
39 views4 pages

Java Test

The document contains 5 Java code snippets that demonstrate different programming concepts: 1. A calculator program that takes user input for two numbers and an operator to perform addition, subtraction, multiplication, division, or remainder calculations. 2. A program that calculates the area and perimeter of a rectangle given user input for length and width. 3. A program that adds two binary numbers input by the user. 4. A program that counts the letters, spaces, numbers, and other characters in a user-input string. 5. A program that prints all unique three-digit numbers using nested for loops.

Uploaded by

Patnala Ramchand
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)
39 views4 pages

Java Test

The document contains 5 Java code snippets that demonstrate different programming concepts: 1. A calculator program that takes user input for two numbers and an operator to perform addition, subtraction, multiplication, division, or remainder calculations. 2. A program that calculates the area and perimeter of a rectangle given user input for length and width. 3. A program that adds two binary numbers input by the user. 4. A program that counts the letters, spaces, numbers, and other characters in a user-input string. 5. A program that prints all unique three-digit numbers using nested for loops.

Uploaded by

Patnala Ramchand
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

1

import [Link];

public class Calculator {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);
double number1, number2;
[Link]("Enter first number: ");
number1 = [Link]();
[Link]("Enter second number: ");
number2 = [Link]();

[Link]("Choose an operator: ");


[Link]("1. Addition (+)");
[Link]("2. Subtraction (-)");
[Link]("3. Multiplication (*)");
[Link]("4. Division (/)");
[Link]("5. Remainder (%)");
int operator = [Link]();

double result = 0;

switch (operator) {
case 1:
result = number1 + number2;
[Link]("Sum: " + result);
break;
case 2:
result = number1 - number2;
[Link]("Difference: " + result);
break;
case 3:
result = number1 * number2;
[Link]("Product: " + result);
break;
case 4:
result = number1 / number2;
[Link]("Quotient: " + result);
break;
case 5:
result = number1 % number2;
[Link]("Remainder: " + result);
break;
default:
[Link]("Invalid operator!");
break;
}
}
}
2

import [Link];

public class Rectangle {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter the length of the rectangle: ");


double length = [Link]();

[Link]("Enter the width of the rectangle: ");


double width = [Link]();

double area = length * width;


double perimeter = 2 * (length + width);

[Link]("Area of the rectangle: " + area);


[Link]("Perimeter of the rectangle: " + perimeter);

[Link]();
}
}

import [Link];

public class AddBinaryNumbers {


public static void main(String[] args) {
long binary1, binary2;
int i = 0, carry = 0;
int[] sum = new int[20];

Scanner scanner = new Scanner([Link]);

[Link]("Enter first binary number: ");


binary1 = [Link]();

[Link]("Enter second binary number: ");


binary2 = [Link]();
[Link]();

while (binary1 != 0 || binary2 != 0) {


sum[i++] = (int) ((binary1 % 10 + binary2 % 10 + carry) % 2);
carry = (int) ((binary1 % 10 + binary2 % 10 + carry) / 2);
binary1 = binary1 / 10;
binary2 = binary2 / 10;
}

if (carry != 0) {
sum[i++] = carry;
}

i--;

[Link]("Sum of two binary numbers: ");


while (i >= 0) {
[Link](sum[i--]);
}
}
}

import [Link];

public class CountCharacters {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Enter a string: ");


String input = [Link]();

int letters = 0;
int spaces = 0;
int numbers = 0;
int other = 0;

for (char ch : [Link]()) {


if ([Link](ch)) {
letters++;
} else if ([Link](ch)) {
numbers++;
} else if ([Link](ch)) {
spaces++;
} else {
other++;
}
}

[Link]("Number of letters: " + letters);


[Link]("Number of spaces: " + spaces);
[Link]("Number of numbers: " + numbers);
[Link]("Number of other characters: " + other);

[Link]();
}
}

public class UniqueThreeDigitNumbers {


public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 4; k++) {
if (i != j && j != k && k != i) {
[Link](i + "" + j + "" + k);
count++;
}
}
}
}
[Link]("Total number of the three-digit-number is " + count);
}
}

You might also like