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

Java Programs for Basic Calculations

The document contains several Java programs demonstrating basic operations such as calculating the remainder, area, perimeter, diagonal, checking even or odd numbers, swapping two numbers, and converting seconds to minutes and hours. Each program includes sample output showcasing the results of the computations. The programs utilize standard input and output methods to interact with the user.

Uploaded by

everlovingmansi
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 for Basic Calculations

The document contains several Java programs demonstrating basic operations such as calculating the remainder, area, perimeter, diagonal, checking even or odd numbers, swapping two numbers, and converting seconds to minutes and hours. Each program includes sample output showcasing the results of the computations. The programs utilize standard input and output methods to interact with the user.

Uploaded by

everlovingmansi
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

Remainder Program:

public class Remainder


{
public static void main(String args[])
{
int a = 25;
int b =4;
int remainder = a % b;
[Link](" Remainder when " + a + " is divided
by " + b + " is: " + remainder);

}
}
Output:
Remainder when 25 is divided by 4 is: 1
public class Area
{
public static void main(String args[])
{
int a = 17;
int b = 13;
int Area = a*b;
int Perimeter = 2*(a+b);
double diagonal = [Link]((a*a)+(b*b));
[Link](" Area " +Area);
[Link](" Perimeter " +Perimeter);
[Link](" Diagonal " +diagonal);

}
}

Output
Area 221
Perimeter 60
Diagonal 21.400934559032695
Program Even_Oddcheck:
public class Even_Oddcheck
{
public static void main(String args[])
{
int a =78;

String b;
b = (a%2==0)?"Even":"Odd";
[Link]( b );

}
}
Output:
Even

Program Swapping:
import [Link];

public class Swapping


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter two unequal numbers");
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
a = a + b;
b = a - b;
a = a - b;
[Link]("a = " + a + " b = " + b);
}
}
Output:
Enter two unequal numbers
Enter first number: 78
Enter second number: 56, a = 56 b = 78
Program Time :
import [Link];

public class Time


{
public static void main( String args[])
{
Scanner in = new Scanner([Link]);
[Link](" Enter the time in secs");
int a = [Link]();
double b;
b= a/60;
double c;
c=b/60;
[Link](" The time in minute is " +b);
[Link](" The time in hours is " +c);
}
}
Output:
Enter the time in secs
3600
The time in minute is 60.0, The time in hours is 1.0

Common questions

Powered by AI

The Even_Oddcheck program can handle negative numbers correctly without modification since the modulus operation is valid for negative numbers in Java. The evaluation 'a%2==0' works uniformly across positive and negative integers. Thus, 'b = (a%2==0)?"Even":"Odd";' would correctly classify negative integers as even or odd .

The Even_Oddcheck program determines if a number is even or odd by checking the remainder when the number is divided by 2 using the modulus operator (%). If the remainder is 0 ('a%2==0'), the number is even, otherwise it is odd. In this program, 'a' is 78 and since 78 % 2 equals 0, the output is 'Even' .

The Remainder program calculates the remainder of an integer division by using the modulus operator (%). In the given program, the expression 'a % b' computes the remainder when 'a' (which is 25) is divided by 'b' (which is 4). The result of this operation is 1, indicating that when 25 is divided by 4, the remainder is 1.

The Area class uses the Pythagorean theorem to calculate the diagonal of a rectangle. The formula 'Math.sqrt((a*a)+(b*b))' calculates the diagonal by taking the square root of the sum of the squares of the lengths of the two sides, 'a' and 'b' . In this case, with 'a' as 17 and 'b' as 13, the diagonal is approximately 21.4009.

The Swapping program swaps two integers 'a' and 'b' without using a temporary variable by applying arithmetic operations. The process involves three steps: First, it adds 'b' to 'a' ('a = a + b'), then subtracts 'b' from this result to get the original value of 'a' and assigns it to 'b' ('b = a - b'), and finally, it subtracts the new 'b' from 'a' to assign the original value of 'b' to 'a' ('a = a - b'). This method efficiently swaps their values .

The Time program converts seconds into minutes and hours using integer division, which can lead to precision loss and inaccuracies in time representation. The issue arises from the use of integer arithmetic where fractions are discarded. For example, converting 3600 seconds results in 60.0 minutes or 1.0 hours, but deviations in seconds could be significant when not managed precisely . Precision could be improved by consistently casting integers to doubles before division.

You might also like