0% found this document useful (0 votes)
14 views12 pages

Java Array Sum and Basic Calculations

The document contains multiple Java programs demonstrating various concepts such as array summation, arithmetic operations, magic squares, basic calculators, and banking systems. Each program is designed to illustrate specific functionalities including user input handling, mathematical calculations, and data structures like arrays. Overall, it serves as a comprehensive guide for beginners to understand Java programming through practical examples.
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)
14 views12 pages

Java Array Sum and Basic Calculations

The document contains multiple Java programs demonstrating various concepts such as array summation, arithmetic operations, magic squares, basic calculators, and banking systems. Each program is designed to illustrate specific functionalities including user input handling, mathematical calculations, and data structures like arrays. Overall, it serves as a comprehensive guide for beginners to understand Java programming through practical examples.
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

JAVA ARRAY SUM

package [Link];
import [Link];
public class SumOfTwoArrayExample2 {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter the size of the Array: ");
int n = [Link]();
int arr1[] = new int[n];
int arr2[] = new int[n];
[Link]("Enter " + n + " element(s) of the Array1: ");
for (int i = 0; i < n; i++) {
arr1[i] = [Link]();
}
[Link]("Enter " + n + " element(s) of the Array2: ");
for (int i = 0; i < n; i++) {
arr2[i] = [Link]();
}
int arr3[] = new int[[Link]];
for (int i = 0; i < [Link]; i++) {
arr3[i] = arr1[i] + arr2[i];
}
for (int i = 0; i < [Link]; i++) {
[Link](arr3[i] + " ");
}
}
}

Arithmatic tools
public class toolsTwoNumbers {

public static void main(String[] args) {

int x=45;

int y=12;

int multiplication,sum,sub;

float d;

multiplication = x * y;

sum=x+y;

d=x/y;

sub=x-y;

[Link]("The Multiplication and add of two Numbers: " + multiplication );


[Link]("The add of two Numbers: " +sum );

[Link]("The divide of two Numbers: " + d );

[Link]("The subteract of two Numbers: " + sub );

Size of square
import [Link].*;
public class Magic {
static final int maxsize = 50;
public static void main (String [] args) throws IOException{
int i, j, k, l, n, key;
boolean n_ok;
String line;
int [] [] square = new int [maxsize] [maxsize];
BufferedReader KeybIn = new BufferedReader(new InputStreamReader([Link]));
try{
[Link]("Size of square? ");
line = [Link]();
n = [Link](line);
n_ok = (1<=n) & (n<=maxsize+1) & (n%2==1);
if ( n_ok ){
for (i=0;i<n;i++)
for (j=0;j<n;j++) square[i][j] = 0;
square[0][(int)(n-1)/2] = 1;
key = 2;
i = 0;
j = (int)(n-1)/2;
while ( key <= n*n ){
k = i - 1;
if ( k < 0 ) k = k + n;
l = j - 1;
if ( l < 0 ) l = l + n;
if ( square[k][l] != 0 ) i = (i+1) % n;
else { i = k; j = l; }
square[i][j] = key;
key = key + 1;
}

[Link]("Magic square of size " + n);


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

for (j=0;j<n;j++)
[Link]("\t"+square[i][j]);
[Link]();
}
}
}catch (NumberFormatException e){
[Link]("Error in number, try again.");

}
}
}

Using Arithmetic Operators

import [Link].*;
import [Link].*;
import [Link];
import [Link];

public class BasicCalculator


{
public static void main(String[] args)
{
double num1, num2;

Scanner sc = new Scanner([Link]);

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

num1 = [Link]();
num2 = [Link]();

[Link]("Enter the operator (+,-,*,/):");

char op = [Link]().charAt(0);
double o = 0;

switch (op) {
case '+':
o = num1 + num2;
break;

case '-':
o = num1 - num2;
break;
case '*':
o = num1 * num2;
break;

case '/':
o = num1 / num2;
break;

default:
[Link]("You enter wrong input");
}

[Link]("The final result:");


[Link]();

[Link](num1 + " " + op + " " + num2


+ " = " + o);
}
}

Using For Loop


import [Link];
public class example
{
public static void main(String[] args)
{
[Link]("First ten numbers are: ");
for (int i=1; i <=10; i++){
[Link](i);
}
}
}
Enter the String to be revers
import [Link];

public class example {

public static void main(String[] args) {

String original, reverse = "";


[Link]("Enter the string to be reversed");

Scanner in = new Scanner([Link]);


original = [Link]();

int length = [Link]();


for(int i=length-1; i>=0; i--)
{
reverse = reverse + [Link](i);
}
[Link](reverse);
}

Create Mark sheet with Per and Grd

import [Link];

public class Marksheet


{
public static String calculateGrade(double percentage)
{
if (percentage >= 90) return "A+";
else if (percentage >= 80) return "A";
else if (percentage >= 70) return "B";
else if (percentage >= 60) return "C";
else if (percentage >= 50) return "D";
else return "F"; }

public static void main(String[] args)


{
Scanner sc = new Scanner([Link]);
Scanner scanner = new Scanner([Link]);

[Link]("Enter your name: ");


String name = [Link]();

[Link]("Enter number of subjects: ");


int n;
try {
n = [Link]();
if (n <= 0) {
[Link]("Number of subjects must be positive.");
return;
}
} catch (Exception e) {
[Link]("Invalid input. Please enter an integer.");
return;
}

double[] marks = new double[n];


double total = 0;

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


[Link]("Enter marks for subject " + (i + 1) + " (0-100): ");
try {
double mark = [Link]();
if (mark < 0 || mark > 100) {
[Link]("Marks must be between 0 and 100.");
i--;
continue;
}
marks[i] = mark;
total += mark;
} catch (Exception e) {
[Link]("Invalid input. Please enter a number.");
[Link]();
i--; }
}

double percentage = total / n;

String grade = calculateGrade(percentage);


[Link]("\n===== MARKSHEET =====");
[Link]("Student Name : " + name );

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


[Link]("Subject %d: %.2f\n", (i + 1), marks[i]);
}
[Link]("Total Marks: %.2f\n", total);
[Link]("Percentage: %.2f%%\n", percentage);
[Link]("Grade: " + grade);

[Link]();
}
}

Simple Banking System using basic methods


import [Link];

public class BasicBankingSystem {


private double balance;
public BasicBankingSystem() {
balance = 0;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
[Link]("Deposited: " + amount);
} else {
[Link]("Invalid deposit amount.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Withdrew: " + amount);
} else {
[Link]("Invalid or insufficient funds.");
}
}

public void checkBalance() {


[Link]("Current Balance: " + balance);
}

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);
BasicBankingSystem bank = new BasicBankingSystem();
boolean exit = false;

while (!exit) {
[Link]("Enter command (1: Deposit, 2: Withdraw,
3: Check Balance, 4: Exit):");
int command = [Link]();
switch (command) {
case 1:
[Link]("Enter deposit amount:");
double depositAmount = [Link]();
[Link](depositAmount);
break;
case 2:
[Link]("Enter withdraw amount:");
double withdrawAmount = [Link]();
[Link](withdrawAmount);
break;
case 3:
[Link]();
break;
case 4:
exit = true;
[Link]("Exiting the banking system.");
break;
default:
[Link]("Invalid command.");
break;
}
}
[Link]();
}
}

Find squered root


public class SquareRootJava {
public static void main(String args[])

{
double positiveNum = 144;
double negativeNum = -100.00;
double zero = 0.0;
double zeroByZero = 0.0/0.0;
double numByZero = 49.0/0.0;

[Link]("Square Root of Positive Number == " +


[Link](positiveNum));

[Link]("Square Root of Negative Number == " +


[Link](negativeNum));

[Link]("Square Root of Zero == " +


[Link](zero));

[Link]("Square Root of zeroByZero == " +


[Link](zeroByZero));

[Link]("Square Root of numByZero == " +


[Link](numByZero));

}
Three dim Array
package arraysProgram;
public class ThreeDArray {
public static void main(String[] args)
{
String[ ] department = {"Electronics", "CS", "IT"};
int dept, st, sc, total = 0;
double perc = 0;

int[ ][ ][ ] scores = {
{{75, 87, 69}, {90, 87, 85},{56, 67, 76}},
{{78, 67, 75}, {87, 98, 76}, {67, 56, 66}},
{{72, 63, 72}, {82, 91, 71}, {64, 56, 66}}
};
for(dept = 0; dept < 3; dept++)
{
for(int i = 0; i < 3; i++)
{
[Link]("\nDepartment " +department[i]+ ": ");
for(st = 0; st < 3; st++)
{
[Link]("\nStudent" +(st + 1)+ " scores: ");
for(sc = 0; sc < 3; sc++)
{
[Link](scores[dept][st][sc]+ " ");
total += scores[dept][st][sc];
perc = (double)total/3;
}
[Link]("\nTotal scores: " +total);
[Link]("Percentage: " +perc);
total = 0;
}
[Link]();
}
break;
}
}
}
Two Dim Array
import [Link];

public class TwoDimensionalArrayExample {

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);

int rows = 0, cols = 0;

while (true) {
[Link]("Enter number of rows (positive integer): ");
if ([Link]()) {
rows = [Link]();
if (rows > 0) break;
else [Link]("Rows must be greater than 0.");
} else {
[Link]("Invalid input. Please enter an integer.");
[Link](); // clear invalid input
}
}

while (true) {
[Link]("Enter number of columns (positive integer):
");
if ([Link]()) {
cols = [Link]();
if (cols > 0) break;
else [Link]("Columns must be greater than 0.");
} else {
[Link]("Invalid input. Please enter an integer.");
[Link]();
}
}

int[][] matrix = new int[rows][cols];

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


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
while (true) {
[Link]("Element [%d][%d]: ", i, j);
if ([Link]()) {
matrix[i][j] = [Link]();
break;
} else {
[Link]("Invalid input. Please enter an
integer.");
[Link]();
}
}
}
}
[Link]("\nMatrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}

[Link]();
}
}

*********

You might also like