Trouble with printing positive integers in java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am practicing and i need help with this code . I need to read integers from the keyboard and print how many are positive Any help in what im doing wrong in my code below?
int size = 10;
int count = 0;
int cuenta = 0;
int[] numbers = new int[size];
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter 10 digits: ");
while (count < size) {
numbers[count] = keyboard.nextInt();
count++;
}
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] >= 0) {
cuenta++;
System.out.println("There are " + cuenta);
}
}
}
}

You have your logic to check for positive integers right. To point you in the right direction think about your print statement and whether it need to be within the for loop.
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] >= 0) {
cuenta++;
System.out.println("There are " + cuenta);
}
}

you need to print out the count after for loop so that it has right answer
System.out.println("There are " + cuenta);

Related

Program to find the maximum product of two numbers in a given integer array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Kindly help me with the underlying program as I am stuck. I'm a newbie programmer.
import java.util.*;
public class Source
{
static int maxProduct(int arr[]) {
int n = arr.length ;
if (n < 2)
{
System.out.println("NA");
return Integer.MIN_VALUE;
}
int a = arr[0];
int b = arr[1];
for(int i = 0; i<n; i++) {
for (int j = i+1; j<n; j++) {
if (arr[i]*arr[j] > arr[0]*arr[1]) {
a = arr[i];
b = arr[j];
}
}
}
return maxProduct;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] arr = new int[size];
for(int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
int answer = maxProduct(arr);
System.out.print(answer);
}
}
You should change
if (arr[i]*arr[j] > arr[0]*arr[1])
to
if (arr[i]*arr[j] > a * b)
Since arr[0]*arr[1] is just the original max product, so you shouldn't be comparing against it.
Also note that your solution is not as efficient as it can be, since you are using a nested loop, which requires O(n^2) running time.
You can achieve linear (O(n)) running time if you use the fact that the max product is either the product of the two highest positive values or the product of the two lowest negative values. This means that if you find these 4 numbers, which can be done with a single loop, you'll find the max product.

Loop for odd number wont close [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I enter an even number, the code works and asks for an odd number, but when I input an odd number, it never closes and carries on the sum for every odd number.
import java.util.Scanner;
public class OddSums {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter an odd number");
int oddSumMax = in.nextInt();
int oddSum = 0;
do {
if (oddSumMax % 2 == 1) {
for(int i=1; i<=oddSumMax; i++) {
if (i % 2 == 1){
oddSum = oddSum + i;
}
}
System.out.println(oddSum);
} else if(oddSumMax % 2 == 0) {
System.out.println("This is even Please enter an odd number");
oddSumMax = in.nextInt();
}
} while (oddSumMax % 2 == 1 );
}
}
You should separate the loops. One loop to ensure the user inputs an odd number and a second one to do the calculations.
Scanner in = new Scanner(System.in);
int oddSumMax;
// Get user input
do {
// TODO: handle case where user does not enter a number
System.out.println("Enter an odd number");
oddSumMax = in.nextInt();
} while (oddSumMax % 2 == 0);
// Calculate
int oddSum = 0;
for (int i = 1; i <= oddSumMax; i += 2) {
oddSum += i;
}
System.out.println(oddSum);

Loop fails to start [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
public class Sum_of_Numbers {
public static void main( String [] args) {
int sumOfEven = 0;
int sumOfOdd = 1;
int even_Times = 0;
int odd_Times = 0;
while ((even_Times < 12) || (odd_Times < 13)); {
sumOfEven = sumOfEven+2;
even_Times = even_Times+1;
sumOfOdd = sumOfOdd + 2;
odd_Times = odd_Times + 1;
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
System.out.println("The sum of even integers is " + sumOfEven);
System.out.println("The sum of odd integers is " + sumOfOdd);
}
}
When I run this code, the loop fails to start and I don't know why.
You've used the wrong syntax with the while statement and it's in an infinite loop
while ((even_Times < 12) || (odd_Times < 13)); {
The semi-colon is closing the statement, so only the conditions within the while loop are executed. even_Times and odd_Times don't increment, so it loops forever.
When the semi-colon is removed, the following { } block will execute within the while loop.
while ((even_Times < 12) || (odd_Times < 13)) {

To convert decimal value into its corresponding binary [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
import java.io.*;
import java.util.*;
public class Binary {
public static void main(String args[]) {
int i = 0, j = 0, num;
Scanner in = new Scanner(System.in);
int arr[] = new int[100];
System.out.println("enter the number");
num = in.nextInt();
while (num != 1) {
j = num % 2;
num = num / 2;
arr[i] = j;
i++;
}
for (i = i; i <= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
}
}
I wrote this programme to convert decimal input to its corresponding binary value, the programme takes the input but it does not show the output i.e. the binary value. please help
As it was already pointed out, you need to change the condition of
while (num != 1) {
to
while (num > 0) {
since your version is prone to an infinite cycle due to the possibility of num being 2.
Change the for cycle, like this:
for (i = arr.length - 1; i >= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
but to be able to do this, you need to know how many elements will you need to use, so change the declaration of arr to this
int arr[] = new int[(int)Math.ceil(Math.log(num) / Math.log(2))];
But to be able to do this, you need to initialize num before you declare arr. Code is untested, let me know if there are any typos.
condition should be while(num!=0){ //do calculations}
and change the condition of for loop into for(i=i;i>=0;i--){}
You could use the Integer class and it's static method toBinaryString(int i). This method converts an int into its binary value and returns it as a String.
If I correctly understood what you are trying to achieve, you could just write:
Scanner in = new Scanner(System.in);
System.out.println("enter the number");
int num = in.nextInt();
String binary = Integer.toBinaryString(num);
System.out.print("The binary number: " + binary);

Adding Elements in an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;

Categories

Resources