You are reading the article Java Program To Calculate Length Of Chord Of The Circle updated in December 2023 on the website Daihoichemgio.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Java Program To Calculate Length Of Chord Of The Circle
A circle is a round shape two-dimensional diagram which has no corners. Every circle has an origin point and every point on the circle maintains equal distance from the origin. The distance between the origin and a point in a circle is known as Radius of the circle. And similarly, if we draw a line from one edge to another edge of the circle and the origin is held in the middle of it, that line is known as diameter of the circle. Basically, the diameter is double of the length of the radius.
Chord of the circle refers to a line touching from one endpoint of circle to another end point of circle. Or simply we can say a chord refers to the line whose endpoints lie on the circle. A chord divides the circle into two parts.
As per the problem statement we have to find the length of the chord when the radius of circle and angle subtended at centre by the chord is given.
Formula to find length of chord −
Length = 2 * r * sin(a/2)Where, ‘r’ refers to the radius of the circle and ‘a’ refers to the angle subtended at centre by the chord.
So, let’s explore.
To show you some instances − Instance-1 Let say radius (r) of circle is 3. And angle (a) subtended at centre by the chord is 60 Then by using the formula, length of the chord is 5.19. Instance-2 Let say radius (r) of circle is 4. And angle (a) subtended at centre by the chord is 50 Then by using the formula, length of the chord is 6.12. Instance-3 Let say radius (r) of circle is 4. And angle (a) subtended at centre by the chord is 40. Then by using the formula, length of the chord is 5.1. AlgorithmStep-1 − Get the radius of circle and angle at the centre subtended by the chord either by static input or by user input.
Step-2 − Find the length of the chord by using the formula.
Step-3 − Print the result.
Multiple ApproachesWe have provided the solution in different approaches.
By Using Static Input Values.
By Using User Input Values.
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input ValueIn this approach, we initialize the radius and angle value in the program. Then by using the algorithm we can find the length of the chord.
Example public class Main { public static void main(String[] args) { double r = 4; double a = 40; double length = 2 * r * Math.sin(a * (3.14 / 180)); System.out.println("Length of Chord: "+length); } } Output Length of Chord: 5.140131589369607 Approach-2: By Using User Defined Method with Static Input Value.In this approach, we take user input of radius and angle value in the program. Then call a user defined method by passing these values as parameters and inside the method by using the algorithm we can find the length of the chord.
Example public class Main { public static void main(String[] args) { double r = 4; double a = 40; length_of_chord(r, a); } static void length_of_chord(double r, double a) { double length = 2 * r * Math.sin(a * (3.14 / 180)); System.out.println("Length of Chord: "+length); } } Output Length of Chord: 5.140131589369607In this article, we explored how to find the length of the chord when the radius ofcircle and angle subtended by chord to centre is given by using different approaches in Java.
You're reading Java Program To Calculate Length Of Chord Of The Circle
Golang Program To Calculate The Sum Of Columns Of Matrix Elements
A matrix is a collection of numbers arranged in rows and columns, a two-dimensional array, each of the values of this matrix is known as an element. Here we will use three methods to find the sum of column elements and compare each method for the same using go programming.
Here is an example of a matrix and the sum value of it’s columns −
The given matrix is −
0 1 2 4 5 6 8 9 7Sum of elements in column 1 is 12
Sum of elements in column 2 is 15
Sum of elements in column 3 is 15
Algorithm
Step 1 − Import the fmt package.
Step 2 − Now we need to start the main() function.
Step 3 − Then we are creating a matrix naming matrix and assign elements to it.
Step 4 − Print the matrix on the screen using fmt.Println() function.
Step 5 − Initialize a new variable called sum of type int to hold the resultant sum.
Step 6 − To find sum of the column elements use the for loop to iterate over the matrix.
Step 7 − Using the first for loop is used to get the column of the matrix while the second for loop gives us the column of the matrix.
Step 8 − Once the loop gets over the matrix elements update the sum variable by adding values to it.
Step 9 − Print the sum of the matrix on the screen.
Example 1In the following example, we will use a for loop to iterate over the matrix and find the sum of its elements and print it on the screen.
package main import "fmt" func main() { matrix := [][]int{ {0, 1, 2}, {4, 5, 6}, {8, 9, 7}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix[0]); i++ { sum := 0 for j := 0; j < len(matrix); j++ { sum += matrix[j][i] } fmt.Printf("Sum of elements in column %d is %dn", i+1, sum) } } Output The given matrix is: 0 1 2 4 5 6 8 9 7 Sum of elements in column 1 is 12 Sum of elements in column 2 is 15 Sum of elements in column 3 is 15 Example 2In this example we will find the sum of the columns of matrix elements using range function.
package main import "fmt" func main() { matrix := [][]int{ {10, 1, 2}, {4, 50, 6}, {8, 9, 7}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix[0]); i++ { sum := 0 for _, row := range matrix { sum += row[i] } fmt.Printf("Sum of elements in column %d is %dn", i+1, sum) } } Output The given matrix is: 10 1 2 4 50 6 8 9 7 Sum of elements in column 1 is 22 Sum of elements in column 2 is 60 Sum of elements in column 3 is 15 Example 3In this example we will use the recursion approach to find the sum of columns of matrix elements.
package main import "fmt" func colSum(matrix [][]int, col int) int { if col == len(matrix[0]) { return 0 } sum := 0 for i := range matrix { sum += matrix[i][col] } return sum } func main() { matrix := [][]int{ {20, 1, 22}, {43, 5, 16}, {86, 91, 10}, } fmt.Println("The given matrix is:") for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt.Print(matrix[i][j], "t") } fmt.Println() } fmt.Println() for i := 0; i < len(matrix[0]); i++ { fmt.Printf("Sum of elements in column %d is %dn", i+1, colSum(matrix, i)) } } Output The given matrix is: 20 1 22 43 5 16 86 91 10 Sum of elements in column 1 is 149 Sum of elements in column 2 is 97 Sum of elements in column 3 is 48 ConclusionWe have successfully compiled and executed a go language program to calculate the sum of columns of matrix elements along with examples. In the first and second example we have used for loop and range functions respectively in the main() section of the program while in the last example we have used a separate function to implement the logic.
Swift Program To Calculate The Sum Of Rows Of Matrix Elements
A matrix is an arrangement of numbers in rows and columns. Matrix can be of various type like square matrix, horizontal matrix, vertical matrix etc.
In this article, we will learn how to write a swift program to calculate the sum of rows of matrix elements. Here we are using the following methods to find the sum of the rows elements −
Using nested for-in loop
Using in-built function
Method 1: Using nested for-in loopHere we use nested for-in loop to find the sum of rows of matrix elements.
AlgorithmStep 1 − Create a function.
Step 2 − Create a variable named sum to store the sum. The initial value of sum = 0.
Step 3 − Find the number of rows and columns.
Step 4 − Run nested for-in loop to iterate through each row and column.
Step 5 − In this nested loop, add elements row-wise and store the result into sum variable.
Step 6 − After each row reset the value of sum = 0 for the sum of next row elements.
Step 7 − Create a matrix and pass it in the function.
Step 8 − Print the output.
ExampleFollowing Swift program to calculate the sum of rows of matrix elements.
import Foundation import Glibc func rowElementsSum(matrix: [[Int]]) { var sum = 0 let noRow = matrix.count let noColumn = matrix[0].count for R in 0..<noRow { for C in 0..<noColumn { sum += matrix[R][C] } print("Sum of row (R) = (sum)") sum = 0 } } let M = [[1, 2, 2], [1, 1, 6], [7, 8, 3], [2, 4, 2]] print("Matrix is:") for x in 0..<M.count { for y in 0..<M[0].count { print(M[x][y], terminator:" ") } print("n") } rowElementsSum(matrix: M) Output Matrix is: 1 2 2 1 1 6 7 8 3 2 4 2 Sum of row 0 = 5 Sum of row 1 = 8 Sum of row 2 = 18 Sum of row 3 = 8Here in the above code, we have a 4×3 matrix and then pass it in the function named rowElementsSum() to find the sum of the row’s elements of the matrix. In this function, we use nested for loop to iterate through each element of the input matrix and then add it to the corresponding row sum. After finishing the nested for loop this function return the row sums that is row 0 = 5, row 1 = 8, row 2 = 18 and row 3 = 8(according to the input matrix).
Method 2: Using in-built functionTo find the sum of the rows of the given matrix elements we uses reduce(_:_:) function. This function returns a result by combining the elements of the array or any sequence using the given closure.
Syntax func reduce(_initial: Value, _next: Value)Here, initial represent the value to use as an initial accumulating value. It passes to the next for the first time the closure is executed. And next represent a closure that combines an accumulating value and an element of the array into a new accumulating value which if further used for next call of the next closure or return to the caller.
AlgorithmStep 1 − Create a matrix.
Step 2 − Print the matrix.
Step 3 − Create an 1-D array to store the sum of the rows.
Step 4 − Run a for loop to iterate through each row.
Step 5 − Find the sum of the rows elements using reduce() function.
let sum = row.reduce(0, +)Step 6 − Now store the sum of each row in the array using append() function.
RowsSum.append(sum)Step 7 − Print the resultant array.
ExampleFollowing Swift program to calculate the sum of rows of matrix elements.
import Foundation import Glibc let matrix = [[1, 2, 3, 4, 6], [4, 5, 6, 1, 1], [7, 8, 9, 5, 5]] print("Matrix is:") for x in 0..<matrix.count { for y in 0..<matrix[0].count { print(matrix[x][y], terminator:" ") } print("n") } var RowsSum = [Int]() for row in matrix { let sum = row.reduce(0, +) RowsSum.append(sum) } print("So the sums of the individual rows are:", RowsSum) Output Matrix is: 1 2 3 4 6 4 5 6 1 1 7 8 9 5 5 So the sums of the individual rows are: [16, 17, 34]Here in the above code, we have a 5×3 matrix. Then create an empty 1-D array to store the sum of each row. The using for loop we iterate through each row of the given matrix and adds the elements of the row using reduce() function. This sum is then store in the RowsSum array. So according to the given matrix the final result is [16, 17, 34]. Here 16 is the sum of row 0 elements(1+2+3+4+6), 17 is the sum of row 1 elements(4+5+6+1+1) and so on.
ConclusionSo this is how we can calculate the sum of rows of matrix elements. Here using the above methods we can calculate the sum of any type of matrix like 6×6, 9×9, 4×3, 6×7, etc.
Swift Program To Calculate The Sum Of All Odd Numbers Up To N
This tutorial will discuss how to write a Swift program to find the sum of all odd numbers upto N.
A number that is not divisible by 2 or we can say that when an odd number is divided by 2 then it leaves some remainder such type of number is known as an odd number. For example, when 2 divides by 2 leave remainder 0 whereas 3 divides by 2 leaves remainder 1. So it means 2 is an even number and 3 is an odd number.
List of odd numbers is −
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, ……We can calculate the sum of all the odd numbers upto N by adding all the odd numbers present in the given list.
Below is a demonstration of the same −
Input
Suppose our given input is −
Number - 10Output
The desired output would be
1+3+5+7+9 = 25 Sum of odd numbers are 25 AlgorithmFollowing is the algorithm −
Step 1 − Create a function.
Step 2 − Declare a variable to store the sum of the odd numbers upto N – sum = 0.
Step 3 − Run a for loop from 0 to N.
Step 4 − Check if the given number is odd number or not.
if j % 2 != 0Here using % operator, we find the remainder. If the remainder is not equal to 0 then the number is an odd number.
Step 5 − Calculate the sum of odd numbers.
sum += j
Step 6 − Return the sum.
Step 7 − Create a variable named “num” to store the value of N. Here the value of num can be user-defined or pre-defined.
Step 8 − Call the function and pass “num” as a argument in it.
Step 9 − Print the output
Example 1The following program shows how to find the sum of all odd numbers upto N.
import
Glibc
var
sum
=
0
(
“Odd numbers from 0 to (a):”
)
for
j
in
1.
.
.
a
{
if
j
%
2
!=
0
{
sum
+=
j
(
j
,
terminator
:
” ,”
)
}
}
return
sum
}
var
num
=
16
(
“nSum of all the odd numbers from 0 to (num): “
,
sumOddNum
(
a
:
num
)
)
Output Odd numbers from 0 to 16: 1 ,3 ,5 ,7 ,9 ,11 ,13 ,15 , Sum of all the odd numbers from 0 to 16: 64In the above code, we create a function named sumOddNum() function to find the sum of all the odd numbers upto N. This function takes one argument. So the working of the sumOddNum() function is −
sumOddNum
(
16
)
sum
=
0
1st
Iteration
:
for
j
in
0.
..
16
if
0
%
2
!=
0
// false
sum
=
sum
+
j
(
j
,
terminator
:
" ,”) sum = 0 2nd Iteration: for j in 0...16 if 1 % 2 != 0 sum = 0 + 1 = 1 print(j, terminator: "
,”)
// print 1
sum
=
1
3rd
Iteration
:
for
j
in
0.
..
16
if
2
%
2
!=
0
// false
sum
=
sum
+
j
(
j
,
terminator
:
" ,”) sum = 1 4th Iteration: for j in 0...16 if 3 % 2 != 0 sum = 1 + 3 = 4 print(j, terminator: "
,”)
// print 3
sum
=
4
...
so on till
16.
Now we display the sum of all the odd numbers present between 0 to 16 is 64(1+3+5+7+9+11+13+15 = 64).
Example 2The following program shows how to find the sum of all odd numbers upto N.
import
Foundationimport
Glibcvar
sum=
0
(
"Odd numbers from 0 to (a):"
)
for
jin
1.
.
.
a{
if
j%
2
!=
0
{
sum+=
j(
j,
terminator:
" ,"
)
}
}
return
sum}
(
"Please enter the number(N):"
)
var
num=
Int
(
readLine
(
)
!
)
!
(
"nSum of all the odd numbers from 0 to (num): "
,
sumOddNum
(
a:
num)
)
STDIN Input Please enter the number(N): 10 Output Odd numbers from 0 to 10: 1 ,3 ,5 ,7 ,9 , Sum of all the odd numbers from 0 to 10: 25Here the working of the above code is same as the Example 1 the only difference is here we take the value of “num” from the user using readLine() function and convert the input value into integer type using Int() function. So here user enter number 10 so the sum of all the odd numbers present in between 0 to 10 is 25 (1+3+5+7+9= 25)
Java Program To Interchange The Diagonals
In this article, we will understand how to interchange the diagonals. The matrix has a row and column arrangement of its elements. A matrix with m rows and n columns can be called as m × n matrix.
Individual entries in the matrix are called element and can be represented by a[i][j] which suggests that the element a is present in the ith row and jth column.
Below is a demonstration of the same −
Suppose our input is −
The matrix is defined as: 4 5 6 1 2 3 7 8 9The desired output would be −
The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7 Algorithm Step 1 - START Step 2 - Declare an integer matrix namely input_matrix, and two integer value namely matrix_size and temp. Step 3 - Define the values. Step 4 - Iterate over each element of the matrix using multiple for-loops and swap the required elements of the matrix using a temporary variable. Step 5 - Display the result Step 5 - Stop Example 1Here, we bind all the operations together under the ‘main’ function.
public class InterchangeDiagonals { public static int matrix_size = 3; public static void main (String[] args) { int input_matrix[][] = { {4, 5, 6}, {1, 2, 3}, {7, 8, 9} }; System.out.println("The matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } for (int i = 0; i < matrix_size; ++i) if (i != matrix_size / 2) { int temp = input_matrix[i][i]; input_matrix[i][i] = input_matrix[i][matrix_size - i - 1]; input_matrix[i][matrix_size - i - 1] = temp; } System.out.println("nThe matrix after interchanging the elements: "); for (int i = 0; i < matrix_size; ++i) { for (int j = 0; j < matrix_size; ++j) System.out.print(input_matrix[i][j]+" "); System.out.println(); } } } Output The matrix is defined as: 4 5 6 1 2 3 7 8 9 The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7 Example 2Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class InterchangeDiagonals { public static int matrix_size = 3; static void interchange_diagonals(int input_matrix[][]) { for (int i = 0; i < matrix_size; ++i) if (i != matrix_size / 2) { int temp = input_matrix[i][i]; input_matrix[i][i] = input_matrix[i][matrix_size - i - 1]; input_matrix[i][matrix_size - i - 1] = temp; } System.out.println("nThe matrix after interchanging the elements: "); for (int i = 0; i < matrix_size; ++i) { for (int j = 0; j < matrix_size; ++j) System.out.print(input_matrix[i][j]+" "); System.out.println(); } } public static void main (String[] args) { int input_matrix[][] = { {4, 5, 6}, {1, 2, 3}, {7, 8, 9} }; System.out.println("The matrix is defined as: "); for (int i = 0; i < matrix_size; i++) { for (int j = 0; j < matrix_size; j++) { System.out.print(input_matrix[i][j] + " "); } System.out.println(); } interchange_diagonals(input_matrix); } } Output The matrix is defined as: 4 5 6 1 2 3 7 8 9 The matrix after interchanging the elements: 6 5 4 1 2 3 9 8 7Java Program To Reverse A List
What is a Reverse List?
Reverse a list is an operation of swapping or interchanging the elements position into a particular list. While you are writing a code in Java, you can easily reverse the order of a certain flow. It is a conventional way of any programming language in computer science. The reverse () method is a collection of class which reverses the position of first element to last element. Last element will earn the first position itself after the termination of the process.
A list is an interface where the class method is denoted as a signature with no definition. The class will be implemented from this by which a method gets a particular definition. Today in this article, we will learn about how to reverse a list using Java conditions with different methods.
How to Reverse a List Using Java?In this method, we have to mention a pointer to perform the reversing process on a linked list by changing nodes.
Reverse an ArrayList in Java can be used by a class reverse method aka Collections.reverse(). In this method the list of the array will be in a linear time with a time complexity of O(n). This method accepts a List type argument to execute a program.
Sometimes when you are writing a code using Java, it’s needed to start the operation from the last element to reverse an array. By altering the position of first and last elements, it’s needed to get a grip that the process will run until the mid-elements swap their positions.
Print the array in a reverse manner, the coder needs to use a for-loop to initiate the printing operation from the end of that particular set of the data aka array. It is a conventional way to reverse a list.
There are so many interfaces in Java by which you can reverse a List but the in-place reversal is a better option to save the machine memory.
Algorithm to Reverse a ListHere is the general algorithm to reverse a linked list by using Java −
Step 1 − Create a new arraylist.
Step 2 − Put some data as input by using add(E e) API.
Step 3 − Reverse those elements of the list and use invoke reverse(List list) API.
Syntax import java.util.Collections; (the Java Package) Collections.reverse(the class_obj);Reverse() method of class collection suggests as iereversing the elements in which they can be sorted.
There are several methods to reverse a list using Java −
Approach 1 − Reverse Array Printing Collections.reverse() method
Approach 2 − Use for loop to reverse an array
Approach 3 − In Place Method to reverse an array
Approach 4 − By Java 8 stream API
Approach 5 − By ListIterator
Reverse Array Printing By Using Collections.reverse() Method public class reverseclassArray { static void reverse(int a[], int n){ int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } System.out.println("Here the reversed array is:"); for (int k = 0; k < n; k++) { System.out.println(b[k]); } } public static void main(String[] args){ int [] arr12 = {101, 202, 303, 404, 505}; reverse (arr12, arr12.length); } } Output Here the reversed array is: 505 404 303 202 101 Reverse an Array Using for LoopWe can use a for loop to reverse an array. In this method a new array is injected with the existing array and by this it will be in a reverse manner.
Example public class Main { static void reverse_array(char char_array[], int a) { char[] dest_array = new char[a]; int j = a; for (int i = 0; i < a; i++) { dest_array[j - 1] = char_array[i]; j = j - 1; } System.out.println("Reversed array from this operation is: "); for (int r = 0; r < a; r++){ System.out.print(dest_array[r] + " "); } } public static void main(String[] args){ char [] char_array = {'I','N','D','I','A'}; System.out.println("Original array print after the operation: "); for (int s = 0; s <char_array.length; s++) { System.out.print(char_array[s] + " "); } System.out.println(); reverse_array(char_array, char_array.length); } } Output Original array print after the operation: I N D I A Reversed array from this operation is: A I D N I In Place Method to Reverse an ArrayThe operation can be done without using another separate type of array. Method can be followed by the swapping between the first and last data of the array.
Example public class reverseArray { static void reverse(int a[], int n){ int[] b = new int[n]; int j = n; for (int i = 0; i < n; i++) { b[j - 1] = a[i]; j = j - 1; } System.out.println("Reversed array is after the operation:"); for (int l = 0; l < n; l++) { System.out.println(b[l]); } } public static void main(String[] args){ int [] arr = {1000, 2000, 3000, 4000, 5000}; reverse(arr, arr.length); } } Output Reversed array is after the operation: 5000 4000 3000 2000 1000 By Using Java 8 Stream APIUsing the stream API, create an int stream which represents the indexes of the list.
Example import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class FavFashion{ public static void main(String[] args) { clothesfavv.add("Raymond Shirt"); clothesfavv.add("Impact Pants"); clothesfavv.add("Socks of Champion"); clothesfavv.add("Shoes By Bata"); System.out.println("Before reversing the whole data:"); System.out.println(clothesfavv); System.out.println("After reversing the whole data:"); System.out.println(reverseClothesfavv); } } Output Before reversing the whole data: [Raymond Shirt, Impact Pants, Socks of Champion, Shoes By Bata] After reversing the whole data: [Shoes By Bata, Socks of Champion, Impact Pants, Raymond Shirt] By List IteratorThe Java environment has an iterator class, and can be used to iterate different collections of the data.
Example import java.util.*; public class FashionCollector{ public static void main(String[] args) { clothesstore2023.add("T-shirt Of Raymond"); clothesstore2023.add("Pants By Impact"); clothesstore2023.add("Socks Of Champion"); clothesstore2023.add("Shoes Of Bata"); while(listIterator.hasPrevious()){ String elemenString = listIterator.previous(); reverseclothesstore2023.add(elemenString); } System.out.println("Before reversing the storage data:"); System.out.println(clothesstore2023); System.out.println("After reversing the storage data:"); System.out.println(reverseclothesstore2023); } } Output Before reversing the storage data: [T-shirt Of Raymond, Pants By Impact, Socks Of Champion, Shoes Of Bata] After reversing the storage data: [Shoes Of Bata, Socks Of Champion, Pants By Impact, T-shirt Of Raymond] ConclusionThus, from the above discussion, we have found how to reverse a list using by using Java. After implementation of various coding methods. It’s recommended to understand those approach carefully.
While we are trying to reverse a list using Java, there are several problems we can encounter. But here is the solution by which a coder can face those problems in smart way.
Update the detailed information about Java Program To Calculate Length Of Chord Of The Circle on the Daihoichemgio.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!