(Done in Eclipse Java)
1. Given an integer between 1—100 captured from a user, perform the following conditional actions: • If is odd, print Weird • If is even and in the inclusive range of 2 to 5, print Not Weird • If is even and in the inclusive range of 6 to 20, print Weird • If is even and greater than 20, print Not Weird Note: Validate that your algorithm works for all cases.
2. Read an integer from stdin and save it to a variable size which denotes some number of integers. • Use the variable size to initialize an array of length size • Next, read in integers from stdin and save each incoming integer to the next available position in your array ◦ Continue until your array is full. Note: you will need to conditionally check to see if you have received the number of inputs you need from the user in order to fill the array • Finally, Print the contents of the full array
3. A prime number is a number which is divisible by only two
numbers: 1 and itself. So, if any number is divisible by any other
number, it is not a prime number.
Write a method which takes one integer as a parameter and identifies
if the number is prime or not. Print if the number is prime or not.
• Expected result: ◦ 13 is prime, while ◦ 8 is not prime • Run with
both prime numbers and numbers which are not prime to verify
1
4. Create a method which takes an integer array as a parameter and reverses the elements in the array. Print the reversed array. • Expected result for int[] testArr = [5, 33, 0, 8, 2] is that it would become the array [2, 8, 0, 33, 5] after the call to your reversal method.
5. Create a method which prints the distance between the two closest numbers in a given array. • Expected result for int[] testArr = [3, 9, 50, 15, 99, 7, 98, 65] is 1, as the two closest numbers in testArr are 98 and 99
6. Given an array of numbers such as int[] testArr = [3, 9, 50,
15, 99, 7, 98, 65], use a loop to sort it from largest to smallest.
Print the sorted array. • Expected result for the above array would
be [99, 98, 65, 50, 15, 9, 7, 3]
//code for case
1----------------------------------------------------------------------
import java.util.*;
public class CheckEvenOdd {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("enter number
between 1 to 100:");
int n=s.nextInt();
if(n%2==0){
if( n>=2
&& n<=5){System.out.println("Not Weird"); }
else if( n>=6
&& n<=20){System.out.println("Weird"); }
else{System.out.println("Not Weird");}
}
else{
System.out.println(" Weird");
}
}
}
//code for case
2------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class CheckEvenOdd {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("enter length of
array:");
int n=s.nextInt();
int[] arr=new int[n];
System.out.println("enter element
in the array:");
for(int i
=0;i<arr.length;i++){
arr[i]=s.nextInt();
}
System.out.println("entered
Array:");
for(int j:arr){
System.out.print(j+" ");
}
}
}
//code for case 3( method that identifies a number is prime or not) ----------------------------------------------------------
void CheckPrime(int n){
int i;
int flag=0;
for(i=2;i<n;i++){
if(n%i==0){
flag=1;
}
}
if(flag==0){
System.out.println(n+" is prime number.");
}
else{
System.out.println(n+" is not prime number."); }
}
//code for case 4(method that reversed the element of given array)----------------------------------------------
void reversal(int[] arr){
int[] rev=new
int[arr.length];
int j=0;
for(int
i=arr.length-1;i>-1;i--){
rev[j]=arr[i];
j++;
}
System.out.println("reversed
array:");
for(int k:rev)
System.out.print(k+" ");
}
//code for case 5(minimum distance between 2 elements of array )-------------------------
static int findMinDiff(int[] arr, int n)
{
// Initialize difference as infinite
int diff = Integer.MAX_VALUE;
// Find the min diff by comparing difference
// of all possible pairs in given array
for (int i=0; i<n-1; i++)
for (int j=i+1; j<n; j++)
if (Math.abs((arr[i] - arr[j]) )< diff)
diff = Math.abs((arr[i] - arr[j]));
// Return min diff
return diff;
}
//code for case 6(sorted array)-------------------------
import java.util.*;
class prime{
void sorted(int[] arr){
int i,j,t;
for(i=0;i<arr.length;i++){
for(j=i+1;j<arr.length;j++){
if(arr[i]<arr[j]){
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
System.out.println("sorted
array:");
for(int k:arr){
System.out.print(k+" ");
}
}
}
public class CheckEvenOdd {
static Scanner s=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("enter
number:");
int n=s.nextInt();
int[] arr=new int[n];
for(int
i=0;i<arr.length;i++)
arr[i]=s.nextInt();
prime p=new prime();
p.sorted(arr);
}
}
(Done in Eclipse Java) 1. Given an integer between 1—100 captured from a user, perform the...
In Java*
Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...
Make a program using Java that asks the user to input an integer
"size". That integer makes and prints out an evenly spaced, size by
size 2D array (ex: 7 should make an index of 0-6 for col and rows).
The array must be filled with random positive integers less than
100. Then, using recursion, find a "peak" and print out its number
and location. (A peak is basically a number that is bigger than all
of its "neighbors" (above,...
Program Requirements First, find all the prime numbers between 2 and a user-inputted number from the console (inclusive). The identified prime numbers must be stored in an array . The inputted number should be between 0 and 1000 (inclusive). array is large enough to o Make sure your hold all the prim e numbers. Dynamic memory allocation is not required for this assignment, so you can have unused space in your array Make sure you can handle the cases of...
The main(int argc, char *argv[]) function will perform the following: • Evaluate whether value in argc is greater or equal to 3. • If it is not, then print the following message: Incorrect number of arguments - please call with assignment min max where assignment is the name of your executable and min/max define the range of numbers for your array. • If argc is ≥ 3 convert the values for min and max into integers and store them in...
java Write an application that input a number from the user and checks if all digits are prime numbers using method prime. The number entered can be of any size. If all digits are prime your program should stop. Use do/while for reading the input and any loop format to test if the number is prime. When checking the prime numbers don’t use an if to check numbers from 1 – 9; you need to find an algorithm to check...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
C++
this program will get two integers from the user. The program will 1. call getProduct (int, int, int &); where the first two arguments are the two input integers and third one is the product of these 2 integers. 2. call printEvenAscending (int, int); where the two arguments are the two input integers. This function will print all -even numbers between the two arguments in an ascending order. Note, the two input integers could be in any order (such...
Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() { int size = (int) (Math.random() * 10) + 1; int[] array = new int [size]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 10 ) + 1; } return array; } Assignment...
In C++ 1. Write a program that allows a user to enter 10 integer values from the keyboard. The values should be stored in an array. 2. The program should then ask the user for a number to search for in the array: The program should use a binary search to determine if the number exists in a list of values that are stored in an array (from Step #1). If the user enters a number that is in the...
Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...