I am trying to do a factorial trailing zero exercise, but I keep getting array index out of bound error. Need help fixing this. Thanks!
import java.util.Scanner;
class Exercise3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] list = new int [n];
int num = 1;
int div = 5;
int count = 0;
for (int i=0;i<n;i++) {
list[i] = sc.nextInt();
}
for (int i=0;i<num;i++) {
while (list[i] > div) {
count += list[i] / div;
div=div*5;
num++;
}System.out.println(count);
}
}
You have
for (int i=0;i<num;i++) {
and then
num++;
that increments at every circle.
Thats why.
Related
Our assignment was to solve the Hackerrank question on arraylist without using 2D arrays or lists. Basically, you would need to input multiple arrays of different sizes and display an element based on the input of (array number, position). My implementation seemed to work just fine for my test cases but failed 4/6 of Hackerrank's test cases. Our lecturer's code (of course) worked perfectly. But what I fail to understand, is advantage of his approach:
My Code ::
import java.io.PrintStream;
import java.util.Scanner;
class arraylist {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int NoOfLines = sc.nextInt();
int[] input = new int[20000];//To store all input arrays one after the other in one 1D array.
int[] index = new int[NoOfLines];//Starting positions of each array input.
int[] NoOfArrayElements = new int[NoOfLines];//Sizes of each corresponding input array.
int position = 0;
int count = 0;
int arrayelementpos = 0;
//Store the input and note the size of each array and the index position.
for (int i = 0; i < NoOfLines; i++) {
int arrarLength = sc.nextInt();
NoOfArrayElements[arrayelementpos++] = arrarLength;
index[position++] = count;
for (int j = 0; j < arrarLength; j++)
input[count++] = sc.nextInt();
}
//Code to input queries (array no, element position)
int NoOfQueries = sc.nextInt();
int[] result = new int[NoOfQueries];
int pos = 0;
for (int i = 0; i < NoOfQueries; i++) {
int arrayNo = sc.nextInt();
int element = sc.nextInt();
if ((arrayNo > NoOfLines) || element > NoOfArrayElements[arrayNo - 1]) {
System.out.println("ERROR!");
continue;
}
pos = index[arrayNo - 1] + element - 1;
System.out.println("THE ELEMENT IS ::" + input[pos]);
}
}
}
My lecturer's code ::
import java.io.*;
import java.util.*;
public class arraylistsolved {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Object [] store=new Object[n];
for(int i=0; i<n;i++){
int d=sc.nextInt();
int [] ar=new int[d];
for(int j=0;j<d;j++) {
ar[j]=sc.nextInt();
}
store[i]=ar;
}
int q=sc.nextInt();
for(int i=0;i<q;i++){
int array=sc.nextInt()-1;
int element=sc.nextInt()-1;
Object obj=store[array];
int [] retrieve = (int []) obj;
if(array>n||element>retrieve.length-1)
System.out.println("ERROR!");
else
System.out.println(retrieve[element]);
}
}
}
As mentioned, both the codes are working for small test cases, but mine breaks down for very large ones for some reason. You can try copy-pasting the code here: https://www.hackerrank.com/challenges/java-arraylist/problem
**_This code is correct but failing larger input testcases
Sum of primes below not correct? what's wrong?
Sum of primes below not correct?
sum of squares of the first n primes
Summing the first N primes [closed]
Here is the code I'm fighting with, please help me.
import java.util.*;
public class Solution
{
public static void main(String[] args)
{
int a[] = new int[10000000];
int b[] = new int[1000000];
int c[] = new int[1000000];
Arrays.fill(a,1);
Scanner in = new Scanner(System.in);
for(int i=2;i<10000;i++)
if(a[i]==1)
for(int j=i*i;j<10000000;j+=i)
a[j]=0;
b[0]=b[1]=0;
int k=2;
for(int i=2;i<10000000;i++)
if(a[i]==1)
{
b[k]=i+b[k-1];
c[k]=i;
k++;
}
int t = in.nextInt();
while(t-->0)
{
int n = in.nextInt();
for(int i=2;;i++)
{
if(c[i]==n)
{
System.out.println(b[i]);
break;
}
else if(c[i]>n)
{
System.out.println(b[i-1]);
break;
}
}
}
}
}
import java.util.*;
public class Solution {
public static void main(String[] args) {
int a[] = new int[10000];
int b[] = new int[10000];
int c[] = new int[10000];
Arrays.fill(a,1);
Scanner in = new Scanner(System.in);
for(int i=2;i<1000;i++)
if(a[i]==1)
for(int j=i*i;j<10000;j+=i)
a[j]=0;
b[0]=0;
int k=1;
for(int i=2;i<10000;i++)
if(a[i]==1)
{
b[k]=i+b[k-1];
c[k]=i;
k++;
}
int t = in.nextInt();
while(t-->0)
{
int n = in.nextInt();
System.out.println(b[n]);
}
}
}
This is the correct code and U can do sum upto 'nearly 1200 (<1300)' prime number sum .
If we want to get beyond that we can increase it in the code ..
Before
int t = in.nextInt();
while(t-->0)
I put
System.out.println("** b[1000] = " + b[1000]) ;
and got 3682913!
Your sum of first 1000 prime numbers is correct.
So, check your printing loop!
Could anyone help me out with this error?
I couldn't rectify this code.
I can't understand the error.
import java.io.*;
import java.util.Scanner;
public class findMax{
public static int findMax(int[] arr){
int max = 0;
for (int i = 1; i < value.length; i++){
if (value[i] > max){
max = value[i];
}
}
return max;
}
public static void main(String[] args){
int value[];
Scanner in = new Scanner(System.in);
value = in.nextInt();
findMax(value);
}
}
In the function findMax, you need to be consistent with your array variable name (you're currently passing int[] arr but accessing value). Also, you don't want to default max to 0 (you could use arr[0]). Something like,
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
Next, you need to instantiate and assign values into your array (and do something with the result of findMax as is the result isn't used). There are a few ways to do that. One might be,
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] value = new int[] { in.nextInt() };
int max = findMax(value);
System.out.printf("The max value in %s is %d.%n", Arrays.toString(value), max);
}
Alternatively, you could create the array like
int[] value = new int[1];
value[0] = in.nextInt();
Also, you could eliminate the if if you use Math.max(int, int) in findMax like
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
max = Math.max(max, arr[i]);
}
return max;
}
import java.util.ArrayList;
import java.util.Scanner;
public class Stack{ public static int findMax(ArrayList<Integer> arr){
int max = 0;
for (int i = 1; i < arr.size(); i++){
if (arr.get(i) > max){
max = arr.get(i);
}
}
return max;
}
public static void main(String[] args){;
Scanner in = new Scanner(System.in);
ArrayList<Integer> value = new ArrayList<Integer>();
System.out.print("Enter integers please ");
System.out.println("(EOF or non-integer to terminate): ");
while (in.hasNextInt()) {
value.add(in.nextInt());
}
System.out.println(findMax(value));
}}
Try this
public static int findMax(ArrayList<Integer> arr){
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++){
if (arr.get(i) > max){
max = arr.get(i);
}
}
return max;
}
public static void main(String[] args){;
Scanner in = new Scanner(System.in);
ArrayList<Integer> value = new ArrayList<Integer>();
System.out.print("Enter integers please ");
System.out.println("(EOF or non-integer to terminate): ");
while (in.hasNextInt()) {
value.add(in.nextInt());
}
System.out.println(findMax(value));
}
I'm trying to get num to create multiple scenarios and get 10 inputed variables from the user which I will then utilize to print out. Once the variables are printed out I need to average them but I got stuck. Could you give me some tips?
final static int num = 10;
static Scanner scan = new Scanner(System.in);
public static void main (String[]args)
{
int[] list = new int [num];
for(int i = 0; i<=num; i++)
{
System.out.println("Enter an integer");
int num[] = scan.nextInt();
}
System.out.println(Print_it(list) / 10);
}
public static double Print_it(int[] list)
{
int number = 0;
for (int i = 0; i<10; i++)
{
number = number + list[i];
}
return(number);
}
The for loops should be using the correct test. 'i<=num' will cause an exception when i == num.
The "int num[] = scan.nextInt()" statement has illegal syntax.
You should be using 'num' wherever you currently use 10, and it should be named 'NUM' to conform to standard Java naming conventions for constants.
Print_it (which should be named more like 'printIt') is not printing anything.
Here is a better implementation:
import java.util.Scanner;
public class Main {
final static int NUM = 10;
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
final int[] list = new int[NUM];
for (int i = 0; i < NUM; i++) {
System.out.println("Enter an integer");
list[i] = scan.nextInt();
}
printIt(list);
}
public static void printIt(int[] list) {
final int length = list.length;
int total = 0;
for (int i = 0; i < length; i++) {
final int n = list[i];
System.out.println(n);
total += n;
}
System.out.println("Average: " + (double)total / length);
}
}
public class AssignmentChapter8
{
public static void main(String[] args)
{
int randomNumbers = new int[100];
int counter = 0;
while(counter < randomNumbers.length)
{
randomNumbers[counter] = (int)(Math.random() * 25);
counter++;
}
int oddNumbers[] = new int[100];
oddNumbers[] = getOddNumbers(randomNumbers);
int evenNumbers[] = new int[100];
evenNumbers[] = getEvenNumbers(randomNumbers);
System.out.println("The odd numbers are:");
for(int k = 0; k < oddNumbers.length; k++)
System.out.print("\t" + oddNumbers[k]);
System.out.println("The even numbers are:");
for(int l = 0; l < evenNumbers.length; l++)
System.out.print("\t" + evenNumbers[l]);
}
public static int getOddNumbers(int randomNumbers)
{
int oddNumbers[] = new int[100];
int counterA = 0;
int counterB = 0;
int counter = 0;
int placeholder;
while(counter < randomNumbers.length)
{
if(randomNumbers[counterA] % 2 > 0)
{
oddNumbers[counterB] = randomNumbers[counterA];
counterB++;
}
counterA++;
counter++;
}
return oddNumbers;
}
public static int getEvenNumbers(int randomNumbers)
{
int evenNumbers[] = new int[100];
int counterA = 0;
int counterB = 0;
int counter = 0;
int placeholder;
while(counter < randomNumbers.length)
{
if(randomNumbers[counterA] % 2 > 0)
{
evenNumbers[counterB] = randomNumbers[counterA];
counterB++;
}
counterA++;
counter++;
}
return evenNumbers;
}
}
I have been trying to execute a program to sort variables in arrays, but I keep getting a ';' expected error in the line after declaration of the array where the program is supposed to retrieve an array from a function. Any help would be appreciated.
This is bad syntax (which causes the ';' expected error ):
oddNumbers[] = getOddNumbers(randomNumbers);
The brackets are not needed. You can do this:
oddNumbers = getOddNumbers(randomNumbers);
Besides that, you have plenty of errors:
int randomNumbers[] = new int[100]; // you need the brackets
Your return value in the method declaration is wrong (you are returning an array, not an int):
public static int[] getEvenNumbers(int randomNumbers)
int oddNumbers[] = new int[100];
By initializing oddNumbers become an array. when you want to assign values,
oddNumbers={elements of array}
Both left and right hand side both should arrays.
May be you should use IDE for coding then it may help you to understand some issues like this.