0% found this document useful (0 votes)
2 views9 pages

Java Programs for Input and Calculations

The document contains three Java programs: the first takes command line arguments for a student's name, university roll number, course, and semester, displaying the information; the second is a menu-driven program that calculates maturity amounts for term and recurring deposits based on user input; the third checks if two numbers are a friendly pair (amicable) based on their abundance ratios. Each program includes source code and prompts for user input. The document also includes output sections indicating the results of the programs.

Uploaded by

Aditya Kalura
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Java Programs for Input and Calculations

The document contains three Java programs: the first takes command line arguments for a student's name, university roll number, course, and semester, displaying the information; the second is a menu-driven program that calculates maturity amounts for term and recurring deposits based on user input; the third checks if two numbers are a friendly pair (amicable) based on their abundance ratios. Each program includes source code and prompts for user input. The document also includes output sections indicating the results of the programs.

Uploaded by

Aditya Kalura
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Write a java program to take input as a command line argument.

Your name, course,


universityrollno and semester. Display the information.
Name:
UniversityRollNo:
Course:
Semester:

SOURCE CODE:
class Xyz {
public static void main(String args[]) {

if ([Link] < 4) {
[Link]("Usage: java Xyz roll_number");
[Link]("Usage: java Xyz semester");
[Link]("Usage: java Xyz name");
[Link]("Usage: java Xyz course");
return;
}

try {
int uniroll = [Link](args[0]);
int sem = [Link](args[1]);
String name = args[2];
String course = args[3];

[Link]("Roll Number: " + uniroll);


[Link]("Semester: " + sem);
[Link]("Name: " + name);
[Link]("Course: " + course);
} catch (NumberFormatException e) {
[Link]("Error: Roll number and semester must be integers.");
}
}
}
1
Page

LAVANYA MAITHANI 34 B2
LAVANYA MAITHANI 34 B2
OUTPUT:

2
Page

LAVANYA MAITHANI 34 B2
2) Using the switch statement, write a menu-driven program to calculate the maturity amount
of a bank deposit.
The user is
(i) Term Deposit
(ii) Recurring Deposit
For option (i) accept Principal (p), rate of interest (r) and time period in years (n). Calculate
and output the maturity amount (a) receivable using the formula a = p[1 + r / 100]n.
For option (ii) accept monthly installment (p), rate of interest (r) and time period in months
(n). Calculate and output the maturity amount (a) receivable using the formula a = p * n + p *
n(n + 1) / 2 * r / 100 * 1 / 12.
For an incorrect option, an appropriate error message should be displayed.

SOURCE CODE:

import [Link];

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

while (true) {
[Link]("Enter 1 for Term Deposit: ");
[Link]("Enter 2 for Recurring Deposit: ");
[Link]("Enter 3 to Exit: ");
int choice = [Link]();

if (choice == 3) {
[Link]("Exiting program...");
break;
}

[Link]("Enter principal amount: ");


3

int p = [Link]();
Page

LAVANYA MAITHANI 34 B2
[Link]("Enter rate: ");
double r = [Link]();
[Link]("Enter time period: ");
int n = [Link]();

double amount = 0;

switch (choice) {
case 1:
amount = p * [Link]((1 + r / 100), n);
[Link]("Amount for Term Deposit is: " + amount);
break;

case 2:
amount = (p * n) + ((p * n * (n + 1)) / 2.0) * (r / 100.0) * (1.0 / 12.0);
[Link]("Amount for Recurring Deposit is: " + amount);
break;

default:
[Link]("Invalid choice. Please enter 1, 2, or 3.");
}
}

[Link]();
}
}
4
Page

LAVANYA MAITHANI 34 B2
OUTPUT:

5
Page

LAVANYA MAITHANI 34 B2
3) Program to find if the given numbers are Friendly pair or not (Amicable or not). Friendly
Pair are two or more numbers with a common abundance.

SOURCE CODE:
import [Link];

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

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


int num1 = [Link]();

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


int num2 = [Link]();

int sum1 = 0;
int sum2 = 0;

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


if (num1 % i == 0) {
sum1 += i;
}
}

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


if (num2 % i == 0) {
sum2 += i;
6

}
Page

LAVANYA MAITHANI 34 B2
}

double ratio1 = (double) sum1 / num1;


double ratio2 = (double) sum2 / num2;

if (ratio1 == ratio2) {
[Link]("Friendly pair");
} else {
[Link]("Not a friendly pair");
}

[Link]();
}
}

7
Page

LAVANYA MAITHANI 34 B2
OUTPUT:

8
Page

LAVANYA MAITHANI 34 B2

You might also like