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

Java Programs With Comments

The document contains several Java programs with detailed comments and variable descriptions. It includes a menu-driven program for calculating a series or displaying a pattern, a special number checker, a school election voting system, a program for summing even integers and multiplying odd integers, and an electricity bill calculator. Each program is designed to demonstrate specific functionalities and user interactions.

Uploaded by

guramritsinghop
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)
4 views5 pages

Java Programs With Comments

The document contains several Java programs with detailed comments and variable descriptions. It includes a menu-driven program for calculating a series or displaying a pattern, a special number checker, a school election voting system, a program for summing even integers and multiplying odd integers, and an electricity bill calculator. Each program is designed to demonstrate specific functionalities and user interactions.

Uploaded by

guramritsinghop
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
You are on page 1/ 5

Java Programs with Detailed Comments

and Variable Descriptions


1. Menu Driven Program (Series & Pattern)
import java.util.Scanner; // Importing Scanner class for user input

/**
* This program offers a menu with two options:
* 1. Calculate a mathematical series
* 2. Print a pattern with 2s
*/

public class MenuProgram {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Creating Scanner object
for input
int choice; // Variable to store user's menu choice

System.out.println("Menu:");
System.out.println("1. Calculate Series S = x1 - x2 + x3 - ... -
x20");
System.out.println("2. Display Pattern: 2 22 222 2222...");
System.out.print("Enter your choice (1 or 2): ");
choice = sc.nextInt(); // Accepting menu choice from user

switch (choice) {
case 1:
System.out.print("Enter the value of x: ");
int x = sc.nextInt(); // Accept x from user
int sum = 0; // Variable to store result of the series

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


int term = (int) Math.pow(x, i); // Calculate x
raised to the power i

if (i % 2 == 0)
sum -= term;
else
sum += term;
}

System.out.println("Sum of the series = " + sum);


break;

case 2:
String pattern = ""; // String to build pattern

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


pattern += "2"; // Append '2' each time
System.out.print(pattern + "\t"); // Print current
pattern with tab space
}
break;

default:
System.out.println("Invalid choice.");
}
}
}

2. Special Number Check


import java.util.Scanner; // Import Scanner for input

/**
* A number is special if the sum of the factorial of its digits equals
the number itself.
* This program checks for that condition.
*/

public class SpecialNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner for input
System.out.print("Enter an integer: ");
int num = sc.nextInt(); // Read input number

int sum = 0; // To store sum of digit factorials


int digit;
int copy = num;

while (copy > 0) {


digit = copy % 10;
int fact = 1;

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


fact *= i;
}

sum += fact;
copy /= 10;
}

if (sum == num)
System.out.println(num + " is a Special Number.");
else
System.out.println(num + " is not a Special Number.");
}
}

3. School Election Voting System


import java.util.Scanner; // Import Scanner

/**
* This program simulates a school election.
* It counts votes for 3 candidates with symbols $, &, and #
* It also calculates the percentage of valid votes per candidate.
*/

public class Election {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int totalVoters = 1252;
int dollar = 0, ampersand = 0, hash = 0, invalid = 0;
char vote;

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


System.out.print("Enter vote symbol ($, &, #): ");
vote = sc.next().charAt(0);

if (vote == '$') dollar++;


else if (vote == '&') ampersand++;
else if (vote == '#') hash++;
else invalid++;
}

int validVotes = dollar + ampersand + hash;

System.out.println("Valid Votes: " + validVotes);


System.out.println("Invalid Votes: " + invalid);

System.out.println("Percentage for $: " + (dollar * 100.0 /


validVotes) + "%");
System.out.println("Percentage for &: " + (ampersand * 100.0 /
validVotes) + "%");
System.out.println("Percentage for #: " + (hash * 100.0 /
validVotes) + "%");
}
}
4. Sum of Even and Product of Odd Integers
import java.util.Scanner; // Import Scanner

/**
* This program takes a list of integers from the user.
* It calculates the sum of even numbers and the product of odd numbers.
* Input stops when 0 is entered.
*/

public class EvenOddSumProduct {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
int evenSum = 0;
int oddProduct = 1;

System.out.println("Enter numbers (0 to end):");

while (true) {
num = sc.nextInt();
if (num == 0)
break;

if (num % 2 == 0)
evenSum += num;
else
oddProduct *= num;
}

System.out.println("Sum of even numbers = " + evenSum);


System.out.println("Product of odd numbers = " + oddProduct);
}
}

5. Electricity Bill Calculation


import java.util.Scanner; // Import Scanner

/**
* This program calculates electricity bill based on number of units.
* It applies a fixed charge and tiered unit pricing.
*/

public class ElectricityBill {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print("Enter units of electricity consumed: ");


int units = sc.nextInt();

double amount = 400.0;

if (units <= 200) {


amount += units * 4.0;
} else if (units <= 500) {
amount += 200 * 4.0 + (units - 200) * 5.0;
} else if (units <= 1000) {
amount += 200 * 4.0 + 300 * 5.0 + (units - 500) * 6.0;
} else {
amount += 200 * 4.0 + 300 * 5.0 + 500 * 6.0 + (units - 1000)
* 6.0;
}

System.out.println("Total amount payable: Rs. " + amount);


}
}

You might also like