0% found this document useful (0 votes)
122 views11 pages

Java Array Operations and Calculations

The document contains a series of Java programs that demonstrate various programming concepts, including array manipulation, mathematical calculations, and user input handling. Each program addresses a specific task, such as calculating sums, checking for prime numbers, and determining geometric areas. The examples illustrate fundamental programming techniques and methods in Java.

Uploaded by

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

Java Array Operations and Calculations

The document contains a series of Java programs that demonstrate various programming concepts, including array manipulation, mathematical calculations, and user input handling. Each program addresses a specific task, such as calculating sums, checking for prime numbers, and determining geometric areas. The examples illustrate fundamental programming techniques and methods in Java.

Uploaded by

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

1.

Write a program which creates an integer array and displays sum of its elements

import [Link];
class array {
static int add(int n, int[] arr) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
[Link]("Enter size number");
int n = [Link]();
int[] arr = new int[n];
[Link]("enter array elements");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
int a = add(n, arr);
[Link](a);
}

[Link] a program which performs addition of elements which are stored in two
arrays of type double.
import [Link].*;
class doublesum {
static Double[] Doublesum(Double[] a, int n1, Double[] b, int n2)
{
int size;
if(n1>n2) {
size=n1;
}else {
size=n2;
}
Double[] sum = new Double[size];
for (int i = 0; i < size; i++) {
double k=i<[Link]?a[i]:0;
double m=i<[Link]?b[i]:0;
sum[i] = k + m;
}
return sum;
}
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
[Link]("Enter the array1 size");
int n1 = [Link]();
Double[] array1 = new Double[n1];
[Link]("Enter the array1 elements");
for (int i = 0; i < n1; i++) {
array1[i] = [Link]();
}
[Link]("Enter the array2 size");
int n2 = [Link]();
Double[] array2 = new Double[n2];
[Link]("Enter the array2 elements");
for (int k = 0; k < n2; k++) {
array2[k] = [Link]();
}
[Link]();
Double[] sum = Doublesum(array1, n1, array2, n2);
[Link]("The array sum is " +
[Link](sum));
}
}

[Link] a method that receives a name as parameter and prints on the console.
“Hello, <name>!” Example.
package object;
import [Link];
public class name {
static void name(String input){
[Link]("Hello, "+input+"!");
}

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);
String input=[Link]();
name(input);

[Link] a method GetMax(int a, int b, int c), that returns maximal of three
numbers. Write a program that reads three numbers from the console and prints the
biggest of them.
package lesson1;

import [Link];

public class max {


static int GetMax(int a, int b, int c) {
int result;
if (a > b && a > c) {
return a;
} else if (b > a && b > c) {
return b;
} else {
return c;
}
}
public static void main(String[] args) {
Scanner num = new Scanner([Link]);
[Link]("enter three numbers");
int a = [Link]();
int b = [Link]();
int c = [Link]();
int fina = GetMax(a, b, c);
[Link](fina);
}
}
[Link] a method that prints the digits of a given decimal number in a reversed
order.
package object;
import [Link];
public class Reverse_number {
static void rev(int n) {
int r,sum=0;
int temp=n;
while(n>0) {
r=n%10;
sum=sum*10+r;
n=n/10;
}
[Link]("Reverse order is "+sum);

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);
[Link]("Enter the number ");
int n=[Link]();
rev(n);

[Link] a Boolean method IsPrime(n) that check whether a given integer number n
is prime.
package lesson1;

import [Link];

public class IsPrime {


static boolean IsPrime(int n) {
if(n<=1)
{
return false;
}
int count = 0;
for (int i = 2; i < n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 1) {
return false;
} else {
return true;
}
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
int n = [Link]();
boolean result = IsPrime(n);
[Link](result);

}
[Link] a method that calculates all prime numbers in given range and returns them
as list of integers.

package lesson1;

import [Link];
import [Link];

public class rangePrime {

public static void main(String[] args) {


Scanner s=new Scanner([Link]);
[Link]("enter the range");
int n1=[Link]();
int n2=[Link]();
[Link]();
[Link](primenum(n1,n2));
}

static ArrayList<Integer> primenum(int n1,int n2){


int count;
ArrayList<Integer> a=new ArrayList<>();
for(int i=n1;i<=n2;i++)
{
count=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
count=count+1;
}
}
if(count==2)
{
[Link](i);
}
}
return a;
}
}
[Link] a program that can calculate the area of four different geometry figures -
triangle, square, rectangle and circle.
package lesson1;
import [Link];
public class Area {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("enter figure type");
[Link]("[Link] [Link] [Link] [Link]");
int n=[Link]();
switch (n){
case 1:
[Link]("enter side and height of triangle");
int s=[Link]();
int h=[Link]();
[Link]("Area of triangle is "+triangle(s,h));
break;
case 2:
[Link]("enter side of square");

int l=[Link]();
[Link]("Area of square is "+square(l));
break;

case 3:
[Link]("enter length and breadth rectangle");
int a=[Link]();
int b=[Link]();
[Link]("Area of Rectangle is "+Rectangle(a,b));
break;
case 4:
[Link]("enter radius of circle");
double r=[Link]();
[Link]("Area of circle is "+circle(r));
break;
default:
[Link]("Invalid option");
}
}
private static double circle(double r) {
return 3.44*r*r;
}
private static double Rectangle(int a, int b) {
return a*b;
}
private static double square(int l) {
return l*l;
}
private static double triangle(int s, int h) {
return 0.5*s*h;
}
}

[Link] a method which accepts two integer arrays and returns an array of unique
elements.
import [Link];
import [Link];
import [Link];
import [Link];
public class uniqueElement {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int arr1[]= {10,5,20,15,25,30};
int arr2[]= {50,12,5,30,15,70};
[Link]("Elements in Array1 is: "+[Link](arr1));
[Link]("Elements in Array2 is: "+[Link](arr2));
uniqElements(arr1,arr2);
[Link]();
}
private static void uniqElements(int[] arr1, int[] arr2){
boolean contains = false;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (arr1[i] == arr2[j]) {
contains = true;
break;
}
}
if(!contains){
[Link](arr1[i]);
}
else{
contains = false;
}
}
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < [Link]; j++) {
if (arr2[i] == arr1[j]) {
contains = true;
break;
}
}
if(!contains){
[Link](arr2[i]);
}
else{
contains = false;
}
}
[Link](list);
}
}

[Link] a method which accepts two matrices of Size N X N and returns summation
of resultant Matrix.

package lesson1;

import [Link];

public class summationMatrix {


public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
[Link]("enter row and column of matrix");
int n1=[Link]();
int n2=[Link]();
int arr1[][]=new int[n1][n2];
int arr2[][]=new int[n1][n2];
int res[][]=new int[n1][n2];
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
arr1[i][j]=[Link]();
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
arr2[i][j]=[Link]();
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
res[i][j]=arr1[i][j]+arr2[i][j];
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
[Link](res[i][j]+" ");
}
[Link]();
}

[Link]();
}
}

[Link] a method public static boolean isRowMagic(int[][] a) that checks if the array
is row-magic (this means that every row has the same row sum).
package lesson1;

import [Link];

public class isRowMagic {


public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
int arr1[][]=new int[a][b];
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
arr1[i][j]=[Link]();
}
}
[Link](rowMagic(arr1,a,b));
[Link]();

private static boolean rowMagic(int[][] arr1,int n1,int n2) {


int res[]=new int[n1];
int sum=0;
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
sum+=arr1[i][j];
}
res[i]=sum;
}
for(int i=0;i<n1-1;i++){
if(res[i]!=res[i+1]){
return false;
}
}
return true;
}
}

Common questions

Powered by AI

The "rev" method handles integer input by systematically tearing apart its digits through modular and division operations, constructing their reverse via multiplication and addition. Constraints include handling negative numbers, overflow scenarios, and ensuring the initial integer type's capacity is viable for reversed outcomes, especially with large numbers susceptible to overflow when reversed .

The method reverses digits by iterating through the number while it is greater than zero. In each iteration, it takes the remainder of the number when divided by 10 (giving the last digit), adds this to a "sum" variable multiplied by 10 (to shift previous digits left), and then divides the original number by 10 to remove the last digit. The process continues until all digits are reversed in the sum .

The method evaluates row-magic status by calculating the sum of each row and storing it in an array. It then checks if each row's sum matches that of every other row. This can be improved by computing the first row sum and then verifying subsequent rows' sums against this, rather than populating an array and looping through it entirely afterward .

The "summationMatrix" method first initializes matrices arr1 and arr2 of the same size with user-input values. It then creates a result matrix res of the same dimensions. The method iterates through each element location (i, j), adding the corresponding elements of arr1 and arr2, storing the sums in res, which gives the resultant matrix that can be summed .

The Doublesum method sums elements of two arrays of potentially differing lengths by iterating up to the size of the larger array, defaulting to 0 for missing values in the shorter array. Considerations include ensuring bounds checking to avoid IndexOutOfBounds exceptions and understanding how this default zero affects resulting sums. It highlights the importance of carefully managing and checking data sizes, especially in mixed-type operations .

The "uniqueElements" method compares each element in the first array with every element in the second. If an element doesn't match any in the other array (checking with a boolean flag), it's added to a list of unique elements. The process is repeated for each element of the second array against the first. This method results in an inefficient O(n*m) complexity due to its nested loops .

The method prompts the user to input a number corresponding to a shape (triangle, square, rectangle, circle). It uses a switch-case structure to call the appropriate area calculation method based on user input. Each case contains a unique formula for its shape's area: triangle uses 0.5*base*height, square uses side^2, rectangle uses length*breadth, and circle uses 3.44*radius^2 as a constant for π approximation .

The "IsPrime" method checks primality by iterating from 2 to n-1 and counting divisors. If n has more than one divisor other than 1 and itself (count>1), it returns false; otherwise, true. However, this is inefficient as it checks unnecessarily many divisions. Optimizing it could involve iterating only up to the square root of n and returning false immediately upon finding the first divisor instead of continuing to count .

The rangePrime method uses a nested loop to check each number in the range from n1 to n2 for primality, counting divisors of each number. This approach is inefficient because it calculates divisors unnecessarily for non-prime numbers. Optimizations could include a sieve method to preemptively eliminate multiples or reducing divisors checked to the square root of the candidate .

The "GetMax" method uses conditional statements to compare three integers a, b, and c. It initially checks if a is greater than both b and c. If true, it returns a. If not, it compares b against a and c. If b is greater, it returns b. Otherwise, it defaults to returning c. This ensures the largest number among the three is returned .

You might also like