0% found this document useful (0 votes)
6 views8 pages

Java Programs for Math and Conversion Tasks

Java programs

Uploaded by

kanagavalli.math
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)
6 views8 pages

Java Programs for Math and Conversion Tasks

Java programs

Uploaded by

kanagavalli.math
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

a.

Sum of Individual Digits of a Positive Integer

import [Link];

public class SumOfDigits {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a positive integer: ");

int num = [Link]();

int sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

[Link]("Sum of digits: " + sum);

[Link]();

b. Generate the First n Terms of a Sequence (Fibonacci Series as an Example)

import [Link];

public class FibonacciSequence {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of terms: ");

int n = [Link]();
int a = 0, b = 1;

[Link]("Fibonacci Sequence: " + a + " " + b + " ");

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

int next = a + b;

[Link](next + " ");

a = b;

b = next;

[Link]();

c. Generate All Prime Numbers Between 1 and n

import [Link];

public class PrimeNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the value of n: ");

int n = [Link]();

[Link]("Prime numbers between 1 and " + n + ":");

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

if (isPrime(i)) {

[Link](i + " ");

}
[Link]();

public static boolean isPrime(int num) {

if (num < 2) return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return false;

return true;

d. Find the Largest and Smallest Number in a List

import [Link];

public class MinMaxFinder {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of elements: ");

int n = [Link]();

int[] numbers = new int[n];

[Link]("Enter the numbers:");

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

numbers[i] = [Link]();

int min = numbers[0], max = numbers[0];

for (int num : numbers) {


if (num < min) min = num;

if (num > max) max = num;

[Link]("Smallest number: " + min);

[Link]("Largest number: " + max);

[Link]();

e. Package for Currency, Distance, and Time Converter

1. Create a Package Named converters

Inside the converters package, create separate classes for Currency, Distance, and Time conversion.

2. [Link]

package converters;

public class CurrencyConverter {

public static double usdToInr(double usd) {

return usd * 83.0; // Example conversion rate

public static double inrToUsd(double inr) {

return inr / 83.0;

3. [Link]

package converters;
public class DistanceConverter {

public static double kmToMiles(double km) {

return km * 0.621371;

public static double milesToKm(double miles) {

return miles / 0.621371;

4. [Link]

package converters;

public class TimeConverter {

public static double hoursToMinutes(double hours) {

return hours * 60;

public static double minutesToHours(double minutes) {

return minutes / 60;

5. Main Program to Use the Package

import [Link];

import [Link];

import [Link];

import [Link];
public class ConverterApp {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Choose conversion type: ");

[Link]("1. Currency (USD <-> INR)");

[Link]("2. Distance (KM <-> Miles)");

[Link]("3. Time (Hours <-> Minutes)");

int choice = [Link]();

switch (choice) {

case 1:

[Link]("Enter amount in USD: ");

double usd = [Link]();

[Link](usd + " USD = " + [Link](usd) + " INR");

[Link]("Enter amount in INR: ");

double inr = [Link]();

[Link](inr + " INR = " + [Link](inr) + " USD");

break;

case 2:

[Link]("Enter distance in KM: ");

double km = [Link]();

[Link](km + " KM = " + [Link](km) + " Miles");

[Link]("Enter distance in Miles: ");

double miles = [Link]();

[Link](miles + " Miles = " + [Link](miles) + " KM");


break;

case 3:

[Link]("Enter time in Hours: ");

double hours = [Link]();

[Link](hours + " Hours = " + [Link](hours) + " Minutes");

[Link]("Enter time in Minutes: ");

double minutes = [Link]();

[Link](minutes + " Minutes = " + [Link](minutes) + "


Hours");

break;

default:

[Link]("Invalid choice.");

[Link]();

How to Run the Package Code

1. Save the [Link], [Link], and [Link] in a folder


named converters.

2. Compile the package:

3. javac -d . converters/*.java

4. Compile and run the ConverterApp:

5. javac [Link]

6. java ConverterApp

You might also like