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

Java Programs for Basic Calculations

The document contains multiple Java programs that perform various tasks. These include calculating item totals based on user input, computing distances and slopes between coordinates, solving quadratic equations, generating arithmetic sequences, and calculating sums and averages of even numbers. Each program uses a Scanner for input and implements loops and conditional statements to achieve its functionality.

Uploaded by

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

Java Programs for Basic Calculations

The document contains multiple Java programs that perform various tasks. These include calculating item totals based on user input, computing distances and slopes between coordinates, solving quadratic equations, generating arithmetic sequences, and calculating sums and averages of even numbers. Each program uses a Scanner for input and implements loops and conditional statements to achieve its functionality.

Uploaded by

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

import [Link].

Scanner;

public class prob2{


public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter the item code : ");
String code = [Link]();
[Link]("Enter the how many of the item you want : ");
int n = [Link]();
double total = 0;

if ([Link]("1")){
[Link]("Item : Hot Dog");
total = n*4.00;

}
if ([Link]("2")){
[Link]("Item : X Salad");
total = n*4.50;
}
if ([Link]("3")){
[Link]("Item : X Bacon");
total = n*5.00;
}
if ([Link]("4")){
[Link]("Item : Toast");
total = n*2.00;
}
if ([Link]("5")){
[Link]("Item : Soda");
total = n*1.50;
}
[Link]("Quantity : " + n);
[Link]("Total : $" + total);

}
}

import [Link];

public class prob3{


public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter the first x co-ordinate : ");
int x1 = [Link]();
[Link]("Enter the first y co-ordinate: ");
int y1 = [Link]();
[Link]("Enter the second x co-ordinate : ");
int x2 = [Link]();
[Link]("Enter the second y co-ordinate: ");
int y2 = [Link]();

double distance = [Link](([Link]( (x2-x1),2))+([Link]( (y2-y1),2)));


double slope = (double)(y2-y1)/(x2-x1);
[Link] ("Distance : " + distance);
[Link] ("Slope : " + slope);
if (slope>=0){
[Link] ("Slope is positive " );
}else{
[Link] ("Slope is negative " );
}

}
}

import [Link];

public class prob4{


public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("A = ");
float A = [Link]();
[Link]("B = ");
float B = [Link]();
[Link]("C = ");
float C = [Link]();

double i = ([Link](B,2))-(4*A*C);

double root1 = ((-B)+ ([Link] (i)))/(2*A);


double root2= ((-B)- ([Link] (i)))/(2*A);

if (i<0) {
[Link]("Impossible to calculate");
}
else if (A==0){
[Link]("Impossible to calculate");
}

else {
[Link]("Root#1 = " + root1);
[Link]("Root#2 = " + root2);
}

}
}

import [Link];

public class prob4{


public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("A = ");
float A = [Link]();
[Link]("B = ");
float B = [Link]();
[Link]("C = ");
float C = [Link]();

double i = ([Link](B,2))-(4*A*C);

double root1 = ((-B)+ ([Link] (i)))/(2*A);


double root2= ((-B)- ([Link] (i)))/(2*A);

if (i<0) {
[Link]("Impossible to calculate");
}
else if (A==0){
[Link]("Impossible to calculate");
}

else {
[Link]("Root#1 = " + root1);
[Link]("Root#2 = " + root2);
}

}
}

//LOOP
import [Link];
public class prob5{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter the first term: ");
int a = [Link]();
[Link]("Enter the change: ");
int d = [Link]();
[Link]("Enter the last term: ");
int l = [Link]();
int n = 1;
int term = 0;
while (term<=l){
term = a + (n-1)*d;
if ((term+d)>l){
[Link](term);
}else {
[Link](term + " , ");
}
n++;
term = a+ (n-1)*d;
}

}
}
//LOOP
import [Link];
public class prob6{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
double y = 0;
int counter =1;

while (counter<=n){

if (counter%4 ==0){
y+= (-1.0/counter);

}else {
y+= (1.0/counter);
}

counter++;
}
[Link] ( "y = " + y);
}
}

//LOOP
import [Link];
public class prob7{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter the value of N: ");
int n = [Link]();
int counter =1;
int y = 0;
int sum2 =0;
int sum1=0;

while (counter<=n) {
y = (2*counter)+1;
if (counter%2 ==0){
sum2+=y;
}else {
sum1+=y;
}
counter++;
}
y= sum1 -sum2;

[Link] ( "y = " + y);


}
}

//LOOP
import [Link];
public class prob8{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter a number : ");
int n = [Link]();

while (n>1) {
[Link]( n + " , ");

if (n%2==0){
n/=2;

}else{
n= (3*n + 1);

}
[Link] (1);

//LOOP
import [Link];
public class prob9{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter a number : ");
int n = [Link]();
int store = n;
int digits = 0 ;
int sum =0;
int power = 0;
int value = 0;

while(n>0){
n/=10;
digits++;
}
while (store>0){
power = (int) ([Link](10,(digits-1)));
value = store/power;
sum+=value;
store%=power;
digits--;
}
if (sum%2==0){
[Link] ("Sum is even");
}else {
[Link] ("Sum is odd");
}
}

//LOOP
import [Link];
public class prob9anotherway{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter a number : ");
int n = [Link]();
int answer = 0 ;
int sum = 0;

while(n>0){
answer = n%10;
sum+=answer;
n/=10;

}
if (sum%2==0){
[Link] ("Sum is even");
}else {
[Link] ("Sum is odd");
}

/*LOOP
import [Link];
public class prob10{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link] ( "Enter a number: " );
int num = [Link]();
int store = num;
int dig = 0;
int power = 0;
int power2 = 0;
int value=0;
int ans =0;
int sum=0;

while(num>0){
num/=10;
dig++;
}

while (store>0){
power = dig-1;
power2 =(int) [Link] ( 10,power);
value = store/power2;
ans = ((int)[Link] (2,power))*value;
sum+=ans;
dig--;

}
[Link] ( sum);

}*/

import [Link];

public class prob10 {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a binary number: ");
int num = [Link]();
int store = num;
int sum = 0;
int power = 0;

while (store > 0) {


int digit = store % 10;
sum += digit *((int) [Link](2, power));
store /= 10;
power++;
}

[Link]("Decimal equivalent: " + sum);

[Link]();
}
}

//LOOP
import [Link];
public class prob11{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);
[Link]("Enter how many numbers you want : ");
int n = [Link]();
int counter = 1;
int max = 0;
int min=0;
int sum =0;
int even=0;

while (counter<=n){
[Link]("Enter a number : ");
int number = [Link]();

if(number >=0 && number%2==0){


even++;
sum+=number;
if(counter==1){
max= number;
min = number;
}

if(number>max){
max = number;
}
if(number<min){
min = number;
}

}
counter++;
}
[Link] ("Max : " + max);
[Link] ("Min : " + min);
double average = sum/even ;
[Link] ("Average : " + average);

//LOOP
import [Link];
public class prob12{
public static void main (String[]args){
Scanner sc = new Scanner ([Link]);

int counter = 1;
int max = 0;
int min=0;
int sum =0;
int even=0;

while (true){
[Link]("Enter a number : ");
int number = [Link]();
if (number==0){
break;
}

if(number >=0 && number%2==0){


even++;
sum+=number;
if(counter==1){
max= number;
min = number;
}

if(number>max){
max = number;
}
if(number<min){
min = number;
}
counter++;
}

}
[Link] ("Max : " + max);
[Link] ("Min : " + min);
double average = sum/even ;
[Link] ("Average : " + average);

Common questions

Powered by AI

The programs utilize `while` loops to repeatedly execute logic until specific end conditions are met, such as reaching the end of a series or achieving a terminating input value. They ensure efficient computation and handling of dynamic input, enabling flexible length processing without pre-defining the number of iterations, making them adaptable for series calculations or real-time data processing .

The program calculates the distance using the Euclidean distance formula: `sqrt((x2-x1)^2 + (y2-y1)^2)` and the slope using the formula: `(y2-y1)/(x2-x1)`. It checks the sign of the slope to determine if it is positive or negative; if the slope is 0 or greater, it indicates a positive slope, otherwise, a negative slope .

The program calculates terms of an arithmetic sequence using `term = a + (n-1)*d` in a loop until `term` exceeds `l`. Each term is printed with a trailing comma unless the next calculated term plus the difference `d` would exceed `l`, in which case it's printed without a comma, indicating the end of the sequence .

The program converts a binary number to a decimal by iteratively extracting the least significant digit using modulus division (`store % 10`), then adding its contribution by multiplying with `2^power`, incrementing the `power`, and reducing the number by division (`store /= 10`) until the number is fully processed .

The program iterates over `n` numbers entered by the user, updating `max` and `min` for non-negative even numbers. It sums these numbers and calculates their average by dividing `sum` by `even` (the count of such numbers), then prints these values .

The program calculates the discriminant `i = B^2 - 4*A*C`. If `i < 0` or `A == 0`, it prints 'Impossible to calculate' because complex roots or division by zero would occur. Otherwise, it calculates the roots using the quadratic formula: `root1 = (-B + sqrt(i)) / (2*A)` and `root2 = (-B - sqrt(i)) / (2*A)` and prints them .

The program employs a `while (true)` loop to accept numbers continuously, where entering zero breaks the loop, evaluating properties of non-negative even entered numbers. This design is useful for real-time data analysis as it handles streams and stops based on a predefined condition effectively .

The program sums reciprocals from 1 to `n` using the expression `y += (1.0/counter)` for counts not divisible by 4, and `y += (-1.0/counter)` for counts divisible by 4. This results in every fourth term being subtracted rather than added, thus altering the usual series sum pattern .

In the first method, digits are successively determined in decreasing place values using division and modulus, then summed up to evaluate parity. The second method extracts digits from the least significant to most by repeated modulus and division by 10, summing these directly. Both determine the sum’s parity through `sum%2`, but differ in digit extraction order .

The program calculates a sequence where, for each `counter`, `y = (2*counter) + 1`, summing results from odd indices differently than even ones. It differentiates by accumulating sums for odd `counter` in `sum1` and even in `sum2`, finally calculating `y = sum1 - sum2`, thus alternating the accumulation method .

You might also like