0% found this document useful (0 votes)
1 views

Java_Solutions_Complete

The document contains Java solutions for various programming problems, including calculating total expenses with discounts, reversing a 5-digit number, counting digits in a positive integer, and creating a simple calculator. It also includes functionalities for printing natural numbers, finding sums of even numbers, checking number properties, and calculating gross salary. Each solution is implemented in a separate Java class with user input handling and appropriate output.

Uploaded by

c58qd7v6gr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Java_Solutions_Complete

The document contains Java solutions for various programming problems, including calculating total expenses with discounts, reversing a 5-digit number, counting digits in a positive integer, and creating a simple calculator. It also includes functionalities for printing natural numbers, finding sums of even numbers, checking number properties, and calculating gross salary. Each solution is implemented in a separate Java class with user input handling and appropriate output.

Uploaded by

c58qd7v6gr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Solutions for Problems

1. Calculate total expenses with discounts

import java.util.Scanner;

public class TotalExpenses {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter quantity purchased: ");

int quantity = scanner.nextInt();

System.out.print("Enter price per item: ");

double price = scanner.nextDouble();

double total = quantity * price;

if (quantity > 100) {

total *= 0.9; // Apply 10% discount

System.out.println("Total expenses: " + total);

scanner.close();

2. Reverse a 5-digit number and check if it's a palindrome

import java.util.Scanner;
public class PalindromeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a 5-digit number: ");

int number = scanner.nextInt();

int reversed = 0, original = number;

while (number != 0) {

int digit = number % 10;

reversed = reversed * 10 + digit;

number /= 10;

System.out.println("Reversed number: " + reversed);

if (original == reversed) {

System.out.println("The number is a palindrome.");

} else {

System.out.println("The number is not a palindrome.");

scanner.close();

3. Count the number of digits in a positive integer

import java.util.Scanner;
public class DigitCounter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a positive integer: ");

int number = scanner.nextInt();

int count = 0;

while (number != 0) {

count++;

number /= 10;

System.out.println("Number of digits: " + count);

scanner.close();

4. Create a simple calculator

import java.util.Scanner;

public class SimpleCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");

double num1 = scanner.nextDouble();

System.out.print("Enter operator (+, -, *, /): ");


char operator = scanner.next().charAt(0);

System.out.print("Enter second number: ");

double num2 = scanner.nextDouble();

double result = 0;

switch (operator) {

case '+': result = num1 + num2; break;

case '-': result = num1 - num2; break;

case '*': result = num1 * num2; break;

case '/':

if (num2 != 0) result = num1 / num2;

else System.out.println("Error: Division by zero");

return;

default:

System.out.println("Invalid operator!");

return;

System.out.println("Result: " + result);

scanner.close();

5. Print all natural numbers from 1 to n

import java.util.Scanner;

public class NaturalNumbers {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter n: ");

int n = scanner.nextInt();

for (int i = 1; i <= n; i++) {

System.out.print(i + " ");

scanner.close();

6. Print tables

import java.util.Scanner;

public class PrintTables {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number to print its table: ");

int num = scanner.nextInt();

for (int i = 1; i <= 10; i++) {

System.out.println(num + " x " + i + " = " + (num * i));

scanner.close();

}
}

7. Find sum of all even numbers between 1 to n

import java.util.Scanner;

public class SumEvenNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter n: ");

int n = scanner.nextInt();

int sum = 0;

for (int i = 2; i <= n; i += 2) {

sum += i;

System.out.println("Sum of all even numbers: " + sum);

scanner.close();

8. Find maximum between two numbers

import java.util.Scanner;

public class MaxBetweenTwoNumbers {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter first number: ");

int num1 = scanner.nextInt();

System.out.print("Enter second number: ");

int num2 = scanner.nextInt();

int max = (num1 > num2) ? num1 : num2;

System.out.println("Maximum number is: " + max);

scanner.close();

9. Check if a number is positive, negative, or zero

import java.util.Scanner;

public class NumberCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = scanner.nextInt();

if (num > 0) {

System.out.println("The number is positive.");

} else if (num < 0) {

System.out.println("The number is negative.");

} else {
System.out.println("The number is zero.");

scanner.close();

10. Check if a number is divisible by 5 and 11

import java.util.Scanner;

public class DivisibilityCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = scanner.nextInt();

if (num % 5 == 0 && num % 11 == 0) {

System.out.println("The number is divisible by both 5 and 11.");

} else {

System.out.println("The number is not divisible by both 5 and 11.");

scanner.close();

11. Input month number and print month name

import java.util.Scanner;
public class MonthName {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter month number (1-12): ");

int month = scanner.nextInt();

String monthName;

switch (month) {

case 1: monthName = "January"; break;

case 2: monthName = "February"; break;

case 3: monthName = "March"; break;

case 4: monthName = "April"; break;

case 5: monthName = "May"; break;

case 6: monthName = "June"; break;

case 7: monthName = "July"; break;

case 8: monthName = "August"; break;

case 9: monthName = "September"; break;

case 10: monthName = "October"; break;

case 11: monthName = "November"; break;

case 12: monthName = "December"; break;

default: monthName = "Invalid month"; break;

System.out.println("Month name: " + monthName);

scanner.close();

}
12. Calculate profit or loss

import java.util.Scanner;

public class ProfitOrLoss {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter cost price: ");

double costPrice = scanner.nextDouble();

System.out.print("Enter selling price: ");

double sellingPrice = scanner.nextDouble();

if (sellingPrice > costPrice) {

System.out.println("Profit: " + (sellingPrice - costPrice));

} else if (sellingPrice < costPrice) {

System.out.println("Loss: " + (costPrice - sellingPrice));

} else {

System.out.println("No profit, no loss.");

scanner.close();

13. Calculate gross salary

import java.util.Scanner;
public class GrossSalary {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter basic salary: ");

double basicSalary = scanner.nextDouble();

double hra, da;

if (basicSalary <= 10000) {

hra = 0.2 * basicSalary;

da = 0.8 * basicSalary;

} else if (basicSalary <= 20000) {

hra = 0.25 * basicSalary;

da = 0.9 * basicSalary;

} else {

hra = 0.3 * basicSalary;

da = 0.95 * basicSalary;

double grossSalary = basicSalary + hra + da;

System.out.println("Gross Salary: " + grossSalary);

scanner.close();

You might also like