Write a short Java function that takes an integer n and returns the sum of all t
ID: 3674174 • Letter: W
Question
Write a short Java function that takes an integer n and returns the sum of all the integers smaller than n. Problem R-1.12. pg. 57 Write a short Java function that takes an integer n and returns the sum of all the odd integers smaller than n. Problem R-1.13. pg. 57 Write a short Java function that takes an array of ini values and determines if there is a pair of numbers in the array whose product is even. Problem C-1.2. pg. 57 Write a short Java method that takes an array of float values and determines if all the numbers arc different from each other (that is. if they arc distinct). Problem C-1.3 pg. 57 Write a short Java program that takes two arrays a and b of length n storing int values, and returns the dot product of a and b. That is. it returns an array c of length n such that c(i) = a(i). b[i]. for i= 0.1.2.... n-1. Problem C-1.7 pg. 57. What is the COMPLEXITY (in big-Oh notation) of each algorithm?Explanation / Answer
Function1:
import java.util.*;
public class Sum{
public static void main(String []args){
Scanner sc=new Scanner(System.in);// creates object of scanner class
System.out.println("enter a number n");
int n=sc.nextInt();//read any number
int sum=0;
for(int i=1;i<n;i++)//loop to calculate sum of all numbers less than n.
sum+=i;
System.out.println("Sum="+sum);
}
}
Function 2:
import java.util.*;
public class SumOdd{
public static void main(String []args){
Scanner sc=new Scanner(System.in);// creates object of scanner class
System.out.println("enter a number n");
int n=sc.nextInt();//read any number
int sum=0;
for(int i=1;i<n;i++)//loop to calculate sum of all numbers less than n.
if(i%2==0)
sum+=i;//if i is divisible by 2 then it is added to sum
System.out.println("Sum="+sum);
}
}
Function 3:
boolean EvenProduct(int[] arr)
{
for(int i=0;i<arr.length();i++)
for(int j=0;j<arr.length();j++)
{
if(i==j)
continue;
if((arr[i]*arr[j])%2==0)
return true;
}
return false;
}
Function 4:
boolean Equality(float[] arr)
{
for(int i=0;i<arr.length();i++)
for(int j=0;j<arr.length();j++)
{
if(i==j)
continue;
if(arr[i]==arr[j])
return true;
}
return false;
}
Function 5:
import java.util.*;
public class DotProduct{
public static void main(String []args){
Scanner sc=new Scanner(System.in);// creates object of scanner class
System.out.println("enter size of an array");
int n=sc.nextInt();//read size of arrays
int []c=new int[n];
int []a=new int[n];
int []b=new int[n];
System.out.println("enter elements of array a");
for(int i=0;i<n;i++)//loop to read a[n]
a[i]=sc.nextInt();
System.out.println("enter elements of array b");
for(int i=0;i<n;i++)//loop to read b[n]
b[i]=sc.nextInt();
System.out.println("DOT PRODUCT");
for(int i=0;i<n;i++)//loop to calculate dotproduct c[i]=a[i].b[i] and print c[i]
{
c[i]=a[i]*b[i];
System.out.println(a[i]+"*"+b[i]+"="+c[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.