so how would you find the largest and smallest number here?
import java.util.Scanner;
public class Loops2 {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
}
}
}
Assuming that what you are trying to do is find the largest and smallest integers are in an array of integers:
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//I will create the array here...
int[] nums = new int[10];
//assigning numbers/ints
for(int i = 0; i < 10; i++) {
nums[i] = input.nextInt();
}
//now to find the largest and smallest (in this order)
int largest = 0;
for(int j = 0; j < nums.length; j++)//usage of the 1-line rule :)
if(nums[j] > largest)
largest = nums[j];
int smallest = largest;
//I'm doing this, so that it keeps checking for something lower than the largest number...
for(int k = 0; k < nums.length; k++)//usage of the 1-line rule again :)
if(nums[k] < smallest)
smallest = nums[k];
System.out.println("Largest: " + largest);
System.out.println("Smallest: " + smallest);
}
Hope this helps!
This is probably the best way to implement it
import java.util.Scanner;
public class FindLargestSmallestNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
//array of 10 numbers
int numbers[] = new int[10];
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
numbers[i] = number ;
}
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
import java.util.Scanner;
public class LargestSmallestNumbers {
private static Scanner input;
public static void main(String[] args) {
int count,items;
int newnum =0 ;
int highest=0;
int lowest =0;
input = new Scanner(System.in);
System.out.println("How many numbers you want to enter?");
items = input.nextInt();
System.out.println("Enter "+items+" numbers: ");
for (count=0; count<items; count++){
newnum = input.nextInt();
if (highest<newnum)
highest=newnum;
if (lowest==0)
lowest=newnum;
else if (newnum<=lowest)
lowest=newnum;
}
System.out.println("The highest number is "+highest);
System.out.println("The lowest number is "+lowest);
}
}
List<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Type 10 numbers");
for (int i = 0; i < 10; i++) {
int number = input.nextInt();
System.out.println(number);
list.add(number);
}
Collections.sort(list);
System.out.println("the small: "+ list.get(0));
System.out.println("the big: "+list.get(list.size() - 1));
Related
I have just started to do programming ...I am trying to sort an array in ascending order.. but not getting desired result , please point where i am doing wrong..
public static void main(String[] args) {
int count, temp;
// User inputs the array size
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of elements you want in the array: ");
count = scan.nextInt();
int num[] = new int[count];
System.out.println("Enter array elements:");
for (int i = 0; i < count; i++) {
num[i] = scan.nextInt();
}
scan.close();
{
int i = 0;
while (i <= count) {
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
i++;
}
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++) {
System.out.print(num[i] + ", ");
}
System.out.print(num[count - 1]);
}
you should increment the i outside the for loop or you can get rid of the while loop and use for loop too, you can find the code below :
import java.util.Scanner;
public class Ascending _Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array:5
Enter all the elements: 4 3 2 6 1
Ascending Order:1,2,3,4,6
or you can use the method sort :
import java.util.Scanner;
import java.util.Arrays;
public class Ascending_Order {
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
Arrays.sort(a);
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array:5
Enter all the elements: 4 3 2 6 1
Ascending Order:1,2,3,4,6
I have generated a program that calculates the fibonacci series with a limit given by the user by keyboard, but I have a problem, since I have asked that the series start at 1, and not at zero as I had done. How can I do that? my fibonacci series shows me from 1 and not from 0?
int maxNumber = 0;
int previousNumber = 0;
int nextNumber = 1;
System.out.println("How many numbers you want in Fibonacci:");
Scanner scanner = new Scanner(System.in);
maxNumber = scanner.nextInt();
System.out.print("Fibonacci Series of "+maxNumber+" numbers:");
for (int i = 1; i <= maxNumber; ++i)
{
System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
}
Use int previousNumber = 1; and it will start from 1.
Just make a simple change and print the number after calculation.
for (int i = 1; i <= maxNumber; ++i)
{
// System.out.print(previousNumber+" ");
int sum = previousNumber + nextNumber;
previousNumber = nextNumber;
nextNumber = sum;
System.out.print(previousNumber+" ");
}
source: Programmers Interview
/*Java Program to find the FibonacciSeries */
public class FibonacciSeries {
public static void main(String a[]){
int limit = 15; /* give limit here */
int[] feb = new int[limit];
feb[0] = 0;
feb[1] = 1;
/* store all elements in array feb[] */
for(int i=2; i < limit; i++){
feb[i] = feb[i-1] + feb[i-2];
}
/* print all array elements */
for(int i=0; i< limit; i++){
System.out.print(feb[i] + " ");
}
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int count=scanner.nextInt();
scanner.close();
int firstTerm=1;
int secondTerm=2;
int nextTerm;
System.out.println("-----------------------");
System.out.print("Fibonacci Series: ");
System.out.print(firstTerm+","+secondTerm);
for(int i=3; i<=count; i++) {
nextTerm=firstTerm+secondTerm;
System.out.print(","+nextTerm);
firstTerm=secondTerm;
secondTerm=nextTerm;
}
}
so in my program is where the user enters list of numbers one at a time, and when I would end the list numbers with the "end" statement which is set to -1, and once I do that I get my average, and maximum, and minimum, my problem is that when I do get the minimum output it would be -1 everytime, I'm having trouble to remove the -1 from the array, any ideas???
import java.util.Scanner; //import scanner to user scanner tool
public class Average { //creating public class
public static void main(String[]args) { //creating public static main
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20]; //creating 20 count array
double sum = 0;
int count = 0;
double average;
int end = -1;
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
numbers[i] = input.nextDouble();
if(numbers[i]== end) {
break;
}
sum+= numbers[i];
count++;
}
// gets average from user input of numbers
average = sum/count;
System.out.println("Average is: " + average);
double max = maxim(numbers);
System.out.println("Max: " + max);
double min = minim(numbers);
System.out.println("Min: " + min);
}
//method for finding out maximum number from user input
public static double maxim(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
//method for finding out minimum number from user input
public static double minim(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
}
You need to docouple part of code that responsible for reading user input from code that compute statistics. And for remove -1 from resulting array you need simple don't put this value to result. When user input random number first check if it is not -1, and after that put it in result. Something like that:
import java.util.Scanner;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
System.out.println("Average is: " + average(numbers));
System.out.println("Max: " + max(numbers));
System.out.println("Min: " + min(numbers));
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
public static double max(double[] array) {
double maxNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] > maxNum) {
maxNum = array[i];
}
}
return maxNum;
}
public static double min(double[] array) {
double minNum = array[0];
for(int i = 1; i<array.length; i++) {
if(array[i] < minNum && array[i]!=-1) {
minNum = array[i];
}
}
return minNum;
}
public static double average(double[] numbers) {
double sum = 0;
for(int i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
return sum / numbers.length;
}
}
In jdk since 8 version your could be simplify this task like this:
import java.util.Scanner;
import java.util.stream.*;
public class Average {
public static void main(String[]args) { //creating public static main
double[] numbers = readInputNumbers();
DoubleSummaryStatistics statistics = DoubleStream.of(numbers).summaryStatistics();
System.out.println("Average is: " + statistics.getAverage()));
System.out.println("Max: " + statistics.getMax());
System.out.println("Min: " + statistics.getMin());
}
public double[] readInputNumbers() {
Scanner input = new Scanner(System.in); //creating scanner input to grab user input
System.out.println("Please enter a list of numbers, entering -1 to end the list: ");
double[] numbers = new double[20];
final int endInput = -1;
for(int i = 0; i < numbers.length; i++) {
System.out.print("Enter a number: ");
double nextNumber = input.nextDouble();
if(nextNumber == endInput) {
break;
} else {
numbers[i] = nextNumber;
}
}
return numbers;
}
}
You could take the input into a temp variables and store it in the array only if it isn't the end flag (-1). E.g.:
for(int i = 0; i<numbers.length; i++) {
System.out.print("Enter a number: ");
double temp = input.nextDouble();
if(temp == end) {
break;
}
numbers[i] = temp;
sum += numbers[i];
count++;
}
I know that this question has been asked before, but not in the the format that I'm writing my code.. Just started taking java classes so I am not familiar with any complex java.. the code below consists of basically all the java I know. Please help! Thanks in advance.
import java.util.Scanner;
public class problem2try {
public static void main(String[] args) {
//declarations
Scanner keyboard = new Scanner (System.in);
int [] inputList = new int [10];
int [] distinctArray = new int [10];
int num;
int counter = 0;
//input
System.out.print("Please enter in 10 integers: ");
for (int i = 0; i < inputList.length; i++)
{
num = keyboard.nextInt();
inputList[i] = num;
}
//processing
distinctArray[0] = inputList[0];
for (int i = 1; i < inputList.length; i++)
{
for (int j = 0; j < inputList.length; j++)
{
if (inputList[i] == inputList[j])
{
counter++;
continue;
}
else
{
distinctArray[i] = inputList[i];
}
}
}
//output
System.out.println("The number of distinct numbers is " + counter);
System.out.print("The distict numbers are: ");
for (int x=0; x<distinctArray.length; x++)
{
if (distinctArray[x] != 0)
System.out.print(distinctArray[x] + " ");
}
}
}
Your logic in the "processing" block seemed off. I modified it to check the current number (outer loop) to all of the known numbers (inner loop). If no match was found, it is appended to the list of known numbers and the count is incremented.
I also modified the "output" code to print the first counter numbers from the list of known numbers. Values past that index are uninitialized.
import java.util.Scanner;
public class problem2try {
public static void main(String[] args) {
//declarations
Scanner keyboard = new Scanner (System.in);
int [] inputList = new int [10];
int [] distinctArray = new int [10];
int num;
int counter = 0;
//input
System.out.print("Please enter in 10 integers: ");
for (int i = 0; i < inputList.length; i++)
{
num = keyboard.nextInt();
inputList[i] = num;
}
//processing
for (int i = 0; i < inputList.length; i++)
{
boolean found = false;
for (int j = 0; j < counter; j++)
{
if (inputList[i] == distinctArray[j])
{
found = true;
break;
}
}
if (!found)
{
distinctArray[counter++] = inputList[i];
}
}
//output
System.out.println("The number of distinct numbers is " + counter);
System.out.print("The distict numbers are: ");
for (int x=0; x<counter; x++)
{
System.out.print(distinctArray[x] + " ");
}
}
}
So I have to swap two elements in an array with 5 values but the two to swap have to be taken from the keyboard. The previous question was to swap to specific ones and I got it done but I'm not sure how to get the numbers from the keyboard to be used in the swap. Some of this is left from the previous one where I knew the elements I had to swap.
import java.util.Scanner;
public class Swap2 {
public static void main(String []args) {
Scanner keyboardIn = new Scanner (System.in);
int[] numbers = new int []{12,9,33,28,5};
int temp = 0, first, second;
System.out.println ("Before the swap: ");
for(int i=0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println();
System.out.println ("Enter the first number to swap:");
first = keyboardIn.nextInt();
System.out.println ("Enter the second number to swap:");
second = keyboardIn.nextInt();
System.out.println ();
temp = numbers[3];
numbers [3] = numbers [1];
numbers [1] = temp;
System.out.println ("After the swap:");
for (int i = 0; i < numbers.length; i++) {
System.out.print (numbers[i] + " ");
}
}
}
you have to define index...
import java.util.Scanner;
public class Swap {
public static void main(String[] args) {
Scanner keyboardIn = new Scanner(System.in);
/* enter code here */int[] numbers = new int[] { 12, 9, 33, 28, 5 };
int temp = 0, first, second;
int index1 = 0 , index2 = 0;
int j , k ;
System.out.println("Before the swap: ");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
System.out.println();
System.out.println("Enter the first number to swap:");
first = keyboardIn.nextInt();
System.out.println("Enter the second number to swap:");
second = keyboardIn.nextInt();
System.out.println();
for(k = 0 ; k < numbers.length; k++){
if(numbers[k] == first)
index1 = k;
}
for(j = 0 ; j < numbers.length; j++){
if(numbers[j] == second)
index2 = j;
}
temp = numbers[index1];
numbers[index1] = numbers[index2];
numbers[index2] = temp;
System.out.println("After the swap:");
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
}
}
You're not actually swapping the numbers which user enters, you're always swapping 4th element with the 1st.
To swap the elements you need to find out the index where they belong.
public static int indexOf(int []arr, int ele)
{
for(int i = 0; i < arr.length; ++i)
if(arr[i] == ele)
return i;
return -1;
}
Now to swap those elements, call this function.
int first = sc.nextInt();
int firstIndex = indexOf(arr, first);
int second = sc.nextInt();
int secondIndex = indexOf(arr, second);
int temp;
temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
Why don't you try this. Please keep in mind that array indices start at 0.
import java.util.Scanner;
public class Swap2
{
public static void main(String []args)
{
Scanner keyboardIn = new Scanner (System.in);
int[] numbers = new int []{12,9,33,28,5};
int temp, first, second;
System.out.println ("Before the swap: ");
for(int i=0; i < numbers.length; i++)
{
System.out.print(numbers[i] + " ");
}
System.out.println ();
System.out.println ("Enter the first number to swap:");
first = keyboardIn.nextInt();
System.out.println ("Enter the second number to swap:");
second = keyboardIn.nextInt();
System.out.println ();
temp = numbers[first];
numbers [first] = numbers [second];
numbers [second] = temp;
System.out.println ("After the swap:");
for (int i = 0; i < numbers.length; i++)
{
System.out.print (numbers[i] + " ");
}
}
}