0% found this document useful (0 votes)
4 views14 pages

Java Assignment1 PrimitiveDatatypes

The document outlines a Java assignment that includes various mathematical and application programs utilizing primitive data types. Each program features an algorithm, flowchart, code, and sample output, covering topics such as Armstrong numbers, palindromes, Fibonacci series, factorials, prime number checking, employee salary processing, temperature monitoring, student grade evaluation, bank account interest calculation, and mobile battery status. The assignment serves as a comprehensive guide for implementing basic programming concepts in Java.

Uploaded by

4mt24cs232
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)
4 views14 pages

Java Assignment1 PrimitiveDatatypes

The document outlines a Java assignment that includes various mathematical and application programs utilizing primitive data types. Each program features an algorithm, flowchart, code, and sample output, covering topics such as Armstrong numbers, palindromes, Fibonacci series, factorials, prime number checking, employee salary processing, temperature monitoring, student grade evaluation, bank account interest calculation, and mobile battery status. The assignment serves as a comprehensive guide for implementing basic programming concepts in Java.

Uploaded by

4mt24cs232
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

Java Assignment 1

Primitive Datatypes
Each program includes: Algorithm | Flowchart | Code | Output

Section A: Mathematical Programs

Program 1: Armstrong Number Checker


Datatypes Used: int, double
Algorithm
Step 1: START
Step 2: Declare variables: n (int), temp (int), sum (double), digit (int),
digits (int)
Step 3: Read the number n from the user
Step 4: Count number of digits: digits = length of n
Step 5: Set temp = n, sum = 0
Step 6: WHILE temp > 0:
digit = temp % 10
sum = sum + [Link](digit, digits)
temp = temp / 10
Step 7: IF (int)sum == n THEN print 'Armstrong Number'
ELSE print 'Not an Armstrong Number'
Step 8: STOP

Flowchart
START
Read n
Count digits; temp = n; sum = 0.0
WHILE temp > 0
digit = temp % 10 sum += pow(digit, digits) temp /= 10
(int)sum == n ?
YES: Armstrong Number NO: Not Armstrong Number
STOP

Code
import [Link];
class Armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int temp = n, digits = [Link](n).length();
double sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += [Link](digit, digits);
temp /= 10;
}
if ((int) sum == n)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is not an Armstrong number.");
}
}

Output
Enter a number: 153
153 is an Armstrong number.

Enter a number: 123


123 is not an Armstrong number.
Program 2: Palindrome Number Checker
Datatypes Used: int
Algorithm
Step 1:
START
Step 2:
Declare variables: n (int), temp (int), rev (int), digit (int)
Step 3:
Read n from user
Step 4:
temp = n, rev = 0
Step 5:
WHILE temp > 0:
digit = temp % 10
rev = rev * 10 + digit
temp = temp / 10
Step 6: IF rev == n THEN print 'Palindrome'
ELSE print 'Not a Palindrome'
Step 7: STOP

Flowchart
START
Read n
temp = n; rev = 0
WHILE temp > 0
digit = temp % 10 rev = rev*10 + digit temp /= 10
rev == n ?
YES: Palindrome NO: Not Palindrome
STOP

Code
import [Link];
class Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int temp = n, rev = 0;
while (temp > 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp /= 10;
}
if (rev == n)
[Link](n + " is a Palindrome.");
else
[Link](n + " is not a Palindrome.");
}
}

Output
Enter a number: 121
121 is a Palindrome.

Enter a number: 123


123 is not a Palindrome.
Program 3: Fibonacci Series Generator
Datatypes Used: int
Algorithm
Step 1:
START
Step 2:
Declare variables: n (int), a = 0, b = 1, c (int)
Step 3:
Read n (number of terms) from user
Step 4:
Print a and b
Step 5:
FOR i = 2 TO n-1:
c = a + b
Print c
a = b; b = c
Step 6: STOP

Flowchart
START
Read n; a=0, b=1
Print a, b
i = 2
i < n ?
c = a+b; Print c a=b; b=c; i++
STOP

Code
import [Link];
class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of terms: ");
int n = [Link]();
int a = 0, b = 1, c;
[Link]("Fibonacci Series: " + a + " " + b);
for (int i = 2; i < n; i++) {
c = a + b;
[Link](" " + c);
a = b;
b = c;
}
[Link]();
}
}

Output
Enter number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Program 4: Factorial of a Number
Datatypes Used: int, long
Algorithm
Step 1:
START
Step 2:
Declare n (int), fact = 1 (long)
Step 3:
Read n from user
Step 4:
FOR i = 1 TO n:
fact = fact * i
Step 5: Print fact
Step 6: STOP

Flowchart
START
Read n; fact = 1L
i = 1
i <= n ?
fact = fact * i i++
Print fact
STOP

Code
import [Link];
class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
[Link]("Factorial of " + n + " = " + fact);
}
}

Output
Enter a number: 10
Factorial of 10 = 3628800
Program 5: Prime Number Checker
Datatypes Used: int, boolean
Algorithm
Step 1:
START
Step 2:
Declare n (int), isPrime (boolean) = true
Step 3:
Read n from user
Step 4:
IF n <= 1 THEN isPrime = false
Step 5:
ELSE FOR i = 2 TO sqrt(n):
IF n % i == 0 THEN isPrime = false; BREAK
Step 6: IF isPrime THEN print 'Prime' ELSE print 'Not Prime'
Step 7: STOP

Flowchart
START
Read n; isPrime = true
n <= 1 ?
YES: isPrime = false NO: i=2; i<=sqrt(n); i++
n % i == 0 ?
YES: isPrime=false; break NO: continue
isPrime ? YES: Prime / NO: Not Prime
STOP

Code
import [Link];
class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
boolean isPrime = true;
if (n <= 1) isPrime = false;
else {
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) { isPrime = false; break; }
}
}
[Link](n + (isPrime ? " is a Prime number." : " is not a Prime
number."));
}
}

Output
Enter a number: 17
17 is a Prime number.

Enter a number: 12
12 is not a Prime number.
Section B: Application Programs

Program 6: Employee ID & Salary Processing


Datatypes Used: long, double, boolean
Algorithm
Step 1: START
Step 2: Declare empId (long), salary (double), isActive (boolean)
Step 3: Assign empId = 1001234567, salary = 75000.50, isActive = true
Step 4: IF isActive THEN calculate bonus = salary * 0.10
Step 5: Compute netSalary = salary + bonus
Step 6: Display empId, salary, bonus, netSalary
Step 7: STOP

Flowchart
START
Assign empId, salary, isActive
isActive == true ?
YES: bonus = salary*0.10 netSalary = NO: bonus = 0
salary+bonus
Display all values
STOP

Code
class EmployeeSalary {
public static void main(String[] args) {
long empId = 1001234567L;
double salary = 75000.50;
boolean isActive = true;
double bonus = 0, netSalary;
if (isActive) bonus = salary * 0.10;
netSalary = salary + bonus;
[Link]("Employee ID : " + empId);
[Link]("Basic Salary : Rs. " + salary);
[Link]("Active Status : " + isActive);
[Link]("Bonus (10%) : Rs. " + bonus);
[Link]("Net Salary : Rs. " + netSalary);
}
}

Output
Employee ID : 1001234567
Basic Salary : Rs. 75000.5
Active Status : true
Bonus (10%) : Rs. 7500.05
Net Salary : Rs. 82500.55
Program 7: Temperature Monitoring System
Datatypes Used: float, boolean
Algorithm
Step 1: START
Step 2: Declare temp (float), isOverheating (boolean)
Step 3: Read temperature value
Step 4: IF temp > 80.0f THEN isOverheating = true ELSE isOverheating = false
Step 5: Display temperature and status
Step 6: STOP

Flowchart
START
Read temp (float)
temp > 80.0 ?
YES: isOverheating = true NO: isOverheating = false
Display temp & status
STOP

Code
import [Link];
class TempMonitor {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter temperature (Celsius): ");
float temp = [Link]();
boolean isOverheating = temp > 80.0f;
[Link]("Temperature : " + temp + "°C");
[Link]("Overheating : " + isOverheating);
[Link]("Status : " + (isOverheating ? "ALERT! Too Hot!" :
"Normal"));
}
}

Output
Enter temperature (Celsius): 95.5
Temperature : 95.5°C
Overheating : true
Status : ALERT! Too Hot!
Program 8: Student Grade Evaluation Using Character
Datatypes Used: char, int
Algorithm
Step 1:
START
Step 2:
Declare marks (int), grade (char)
Step 3:
Read marks from user
Step 4:
IF marks >= 90 THEN grade = 'A'
ELSE IF marks >= 80 THEN grade = 'B'
ELSE IF marks >= 70 THEN grade = 'C'
ELSE IF marks >= 60 THEN grade = 'D'
ELSE grade = 'F'
Step 5: Display grade
Step 6: STOP

Flowchart
START
Read marks (int)
marks >= 90 ? -> A marks >= 80 ? -> B marks >= 70 ? -> C marks >= 60 ? -> D Else
-> F
Display grade (char)
STOP

Code
import [Link];
class StudentGrade {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter marks (0-100): ");
int marks = [Link]();
char grade;
if (marks >= 90) grade = 'A';
else if (marks >= 80) grade = 'B';
else if (marks >= 70) grade = 'C';
else if (marks >= 60) grade = 'D';
else grade = 'F';
[Link]("Marks : " + marks);
[Link]("Grade : " + grade);
}
}

Output
Enter marks (0-100): 85
Marks : 85
Grade : B
Program 9: Bank Account Balance with Interest
Datatypes Used: float, double
Algorithm
Step 1: START
Step 2: Declare principal (float), rate (double), time (double)
Step 3: Read principal, rate, and time
Step 4: Calculate simpleInterest = (principal * rate * time) / 100
Step 5: totalBalance = principal + simpleInterest
Step 6: Display principal, interest, and total balance
Step 7: STOP

Flowchart
START
Read principal (float), rate (double), time (double)
SI = (principal * rate * time) / 100
totalBalance = principal + SI
Display SI and totalBalance
STOP

Code
import [Link];
class BankAccount {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Principal (Rs): ");
float principal = [Link]();
[Link]("Enter Rate of Interest (%): ");
double rate = [Link]();
[Link]("Enter Time (years): ");
double time = [Link]();
double si = (principal * rate * time) / 100;
double total = principal + si;
[Link]("Principal : Rs. " + principal);
[Link]("Simple Interest: Rs. " + si);
[Link]("Total Balance : Rs. " + total);
}
}

Output
Enter Principal (Rs): 10000
Enter Rate of Interest (%): 8.5
Enter Time (years): 3
Principal : Rs. 10000.0
Simple Interest: Rs. 2550.0
Total Balance : Rs. 12550.0
Program 10: Mobile Battery Status Indicator
Datatypes Used: byte, boolean
Algorithm
Step 1: START
Step 2: Declare batteryLevel (byte), isCharging (boolean), isLow (boolean)
Step 3: Read batteryLevel from user
Step 4: IF batteryLevel <= 20 THEN isLow = true ELSE isLow = false
Step 5: Display battery percentage and status
Step 6: STOP

Flowchart
START
Read batteryLevel (byte) isCharging = false
batteryLevel <= 20 ?
YES: isLow = true NO: isLow = false
Display level & status
STOP

Code
import [Link];
class BatteryStatus {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter battery level (0-100): ");
byte batteryLevel = [Link]();
boolean isCharging = false;
boolean isLow = batteryLevel <= 20;
[Link]("Battery Level : " + batteryLevel + "%");
[Link]("Charging : " + isCharging);
[Link]("Low Battery : " + isLow);
[Link]("Status : " + (isLow ? "Please charge!" : "Battery OK"));
}
}

Output
Enter battery level (0-100): 15
Battery Level : 15%
Charging : false
Low Battery : true
Status : Please charge!
Program 11: Population Counter
Datatypes Used: long
Algorithm
Step 1: START
Step 2: Declare population (long), births (long), deaths (long)
Step 3: Assign initial population = 1400000000L
Step 4: Read births and deaths per year
Step 5: newPopulation = population + births - deaths
Step 6: Display initial and updated population
Step 7: STOP

Flowchart
START
population = 1400000000L
Read births (long), deaths (long)
newPop = population + births - deaths
Display initial pop and newPop
STOP

Code
import [Link];
class PopulationCounter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
long population = 1400000000L;
[Link]("Enter annual births: ");
long births = [Link]();
[Link]("Enter annual deaths: ");
long deaths = [Link]();
long newPopulation = population + births - deaths;
[Link]("Initial Population : " + population);
[Link]("Births : " + births);
[Link]("Deaths : " + deaths);
[Link]("Updated Population : " + newPopulation);
}
}

Output
Enter annual births: 25000000
Enter annual deaths: 10000000
Initial Population : 1400000000
Births : 25000000
Deaths : 10000000
Updated Population : 1415000000
Program 12: Currency Conversion System
Datatypes Used: double
Algorithm
Step 1: START
Step 2: Declare amountInUSD (double), exchangeRate (double)
Step 3: Read amountInUSD from user
Step 4: Set exchangeRate = 83.50 (USD to INR)
Step 5: amountInINR = amountInUSD * exchangeRate
Step 6: Display the converted amount
Step 7: STOP

Flowchart
START
Read amountInUSD (double)
exchangeRate = 83.50
amountInINR = amountInUSD * exchangeRate
Display amountInINR
STOP

Code
import [Link];
class CurrencyConverter {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter amount in USD: ");
double amountInUSD = [Link]();
double exchangeRate = 83.50;
double amountInINR = amountInUSD * exchangeRate;
[Link]("Amount in USD : $%.2f%n", amountInUSD);
[Link]("Exchange Rate : 1 USD = Rs. %.2f%n", exchangeRate);
[Link]("Amount in INR : Rs. %.2f%n", amountInINR);
}
}

Output
Enter amount in USD: 150.75
Amount in USD : $150.75
Exchange Rate : 1 USD = Rs. 83.50
Amount in INR : Rs. 12587.63

You might also like