0% found this document useful (0 votes)
10 views10 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that demonstrate various programming concepts including checking for Armstrong numbers, calculating sums, printing patterns, and method overloading for volume and area calculations. Each program is accompanied by example input and output. The programs cover topics such as arrays, loops, and mathematical functions.

Uploaded by

yash singh
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)
10 views10 pages

Java Programs for Basic Calculations

The document contains a series of Java programs that demonstrate various programming concepts including checking for Armstrong numbers, calculating sums, printing patterns, and method overloading for volume and area calculations. Each program is accompanied by example input and output. The programs cover topics such as arrays, loops, and mathematical functions.

Uploaded by

yash singh
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

Q1.

Write a program to check whether a number is an Armstrong number or


not.
Program:

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

Output:

Enter a number:
153
It is an Armstrong number
Q2. Write a program to input 10 numbers and print their sum.
Program:

import [Link].*;
class SumArray {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int num[] = new int[10];
int i, sum = 0;
[Link]("Enter 10 numbers:");
for(i = 0; i < 10; i++) {
num[i] = [Link]();
sum = sum + num[i];
}
[Link]("Sum is " + sum);
}
}

Output:

Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Sum is 55
Q3. Write a program to input 10 numbers and print their sum (same as above
but class name Even).
Program:

import [Link].*;
class Even {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int num[] = new int[10];
int i, sum = 0;
[Link]("Enter 10 numbers:");
for(i = 0; i < 10; i++) {
num[i] = [Link]();
sum = sum + num[i];
}
[Link]("Sum is " + sum);
}
}

Output:

Enter 10 numbers:
1 2 3 4 5 6 7 8 9 10
Sum is 55
Q4. Write a program to print the following pattern:
1
12
123
1234
Program:

import [Link].*;
class Pattern {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int i, j;
for(i = 1; i <= 4; i++) {
for(j = 1; j <= i; j++) {
[Link](j);
}
[Link]();
}
}
}

Output:

1
12
123
1234
Q5. Write a program to print square root of even numbers and cube root of odd
numbers in a given range.
Program:

import [Link].*;
class Even_odd {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int i, a, b;
double s;
[Link]("Enter the Range:");
a = [Link]();
b = [Link]();
i = a;
while(i <= b) {
if(i % 2 == 0) {
s = [Link](i);
[Link](s);
} else {
s = [Link](i);
[Link](s);
}
i++;
}
}
}

Output:

Enter the Range:


1 5
1.0
1.4142135623730951
1.2599210498948732
2.0
1.7099759466766968
Q6. Write a program to print a number pattern using nested loops.
Program:

import [Link].*;
class Pattern2 {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int i, j, k;
k = 5;
for(i = 1; i <= 5; i++) {
for(j = 5; j >= i; j--) {
[Link](k);
[Link]();
k--;
}
}
}
}

Output:

5
4
3
2
1
Q7. Write a program to calculate the volume of sphere, cylinder and cuboid
using method overloading.
Program:

import [Link].*;
class V {
double volume(double R) {
double v;
v = (4.0/3) * [Link] * R * R * R;
return v;
}
double volume(double H, double R) {
double v;
v = [Link] * R * R * H;
return v;
}
double volume(double l, double b, double h) {
double v;
v = l * b * h;
return v;
}
}

Output:

Volume of sphere with R=3: 113.097


Volume of cylinder with H=5, R=3: 141.371
Volume of cuboid with l=2, b=3, h=4: 24.0
Q8. Write a program to input 20 numbers in a 2D array (4x5) and find maximum
in each row.
Program:

import [Link].*;
class Array {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int max, i, j;
int arr[][] = new int[4][5];
[Link]("Enter 20 numbers:");
for(i = 0; i < 4; i++) {
for(j = 0; j < 5; j++) {
arr[i][j] = [Link]();
}
}
for(i = 0; i < 4; i++) {
max = arr[i][0];
for(j = 1; j < 5; j++) {
if(max < arr[i][j]) {
max = arr[i][j];
}
}
[Link]("Maximum in row " + (i+1) + " = " + max);
}
}
}

Output:

Enter 20 numbers:
(Example input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)
Maximum in row 1 = 5
Maximum in row 2 = 10
Maximum in row 3 = 15
Maximum in row 4 = 20
Q9. Write a program to find the sum of sine series up to n terms.
Program:

import [Link].*;
class Series {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int n, i;
double sum = 0;
[Link]("Enter the value of n:");
n = [Link]();
for(i = 1; i <= n; i++) {
sum = sum + [Link](i);
}
[Link]("Sum of series = " + sum);
}
}

Output:

Enter the value of n:


3
Sum of series = 1.7507684116335782
Q10. Write a program to find the area of triangle, trapezium and rhombus using
method overloading.
Program:

import [Link].*;
class A {
void area(double a, double b, double c) {
double s, ar;
s = (a + b + c) / 2;
ar = [Link](s * (s - a) * (s - b) * (s - c));
[Link](ar);
}
void area(int a, int b, int h) {
double ar;
ar = 0.5 * (a + b) * h;
[Link](ar);
}
void area(double d1, double d2) {
double ar;
ar = 0.5 * (d1 * d2);
[Link](ar);
}
}

Output:

Area of triangle with sides 3,4,5: 6.0


Area of trapezium with a=3, b=5, h=4: 16.0
Area of rhombus with diagonals 6 and 8: 24.0

Common questions

Powered by AI

The program differentiates the volume calculations through method overloading by using different signatures for each shape. For a sphere, it accepts a single parameter `R` (radius) and uses the formula `(4.0/3) * Math.PI * R * R * R`. For a cylinder, it uses two parameters, `H` (height) and `R` (radius), applying the formula `Math.PI * R * R * H`. For a cuboid, it takes three parameters `l`, `b`, and `h` (length, breadth, height) and calculates the volume using `l * b * h` .

Storing numbers in a 2D array allows structured storage and manipulation of data, enabling the program to efficiently access and process each row individually. It uses nested loops to iterate over each row and column, initialising `max` with the first element of each row and updating it by comparing with subsequent elements. This structured approach facilitates calculating and displaying the maximum in each row effectively .

The implications of a decrementing nested loop in pattern printing include the control over reverse or reducing sequences. A loop that decrements an index allows printing patterns that visually shrink or count down in value or size with each iteration. It affects the structure of the output pattern; for instance, decreasing indices can lead to outputs that start large and reduce, critical for designing descending or mirrored patterns .

In the pattern printing program, nested loops control the number of iterations needed to print each line of the pattern. The outer loop determines the number of lines (or rows) to print, while the inner loop handles the printing of numbers within each line. The inner loop iterates from 1 to the current value of the outer loop index `i`, allowing each row to consist of numbers ranging from 1 up to the current row number. This produces the incremental pattern output .

The program identifies even and odd numbers using the modulus operator `%`. It iterates through each number within a given range and uses `i % 2 == 0` to check for even numbers, for which it calculates the square root using `Math.sqrt(i)`. For numbers where this condition is false (odd numbers), it applies the cube root using `Math.cbrt(i)`. This conditional logic ensures the correct mathematical functions are applied to each number based on its parity .

The Armstrong number program determines if a number is an Armstrong number by first taking an integer input and initializing a variable `sum` to 0. It then iterates through each digit of the number, calculates the cube of the digit (`a * a * a`) and adds it to `sum`. It checks whether the final `sum` equals the original number (`n1`). If they are equal, the number is an Armstrong number; otherwise, it is not .

Method overloading is useful in calculating areas of different geometrical figures because it allows the same method name to handle different types or numbers of arguments, corresponding to different shapes. This enhances readability and organization by grouping related functionality under a single method name, `area`, while maintaining distinct logic for triangles, trapeziums, and rhombuses based on the number and type of parameters provided .

Using `Math.PI` in geometric calculations within the method overloading program ensures accuracy and standardization of results, as `Math.PI` provides a precise representation of π. It is vital for computations involving circles or spheres, where precise pi values are crucial for accuracy. This constant promotes consistency across different Java implementations, reducing the risk of errors that occur with manual approximations of π. However, developers must consider the trade-off between precision and computation time in contexts where approximation may suffice .

Changing control flow statements in the Armstrong number program, such as using `for` instead of `while` loops, can affect readability and performance but not the outcome, as long as the logic remains intact. For example, modifying to `for`, iterating over digits directly might improve clarity. These changes can lead to efficiency improvements or compromises depending on how loop exits and condition checks are handled, potentially affecting the calculation steps or, if not careful with logic preservation, causing incorrect Armstrong results .

The sine series summation program uses a loop to compute the sum of sine values up to term `n`. The efficiency depends on the size of `n` due to linear complexity: more terms increase computation time. Practically, precision and performance may vary based on hardware and the implementation of `Math.sin()`. Floating-point arithmetic introduces minor precision errors, relevant for applications demanding high accuracy. Thus, computational efficiency and precision are key considerations for optimizing such series computations .

You might also like