Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm totally new in Java programming , I wonder how may I write program that reads an arbitrary number of positive integers from the keyboard and then prints them in reverse order. The reading stops when the user inputs a negative number. An example of an execution:
Enter positive integers. End by giving a negative integer.
Integer 1: 5
Integer 2: 10
Integer 3: 15
Integer 4: 20
Integer 5: -7
Number of positive integers: 4
In reverse order: 20, 15, 10, 5
Get your input inside a do-while loop until the loop condition becomes false, means y is negative. Since you've got i value incremented before your loop condition is false, decrement i value by 1 outside loop.
Next, inside the decrementing for loop, print your variables in decreasing index of array, so it will be printed in reverse order of input.
import java.util.*;
/**
*
* #author Capt. Jack Sparrow, pirate lord of the seven seas
*/
public class Example {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner s1 = new Scanner(System.in);
int[] x = new int[50];
int i =0, y;
do
{
System.out.println("Enter new positive integer: ");
y = s1.nextInt();
x[i] = y;
i++;
}while( y >= 0);
i--;
System.out.println("Number of positive integers: "+i);
System.out.print("In reverse order: ");
for(int j = i; j >0; j--)
{
System.out.print(x[j-1] + " ");
}
System.out.println();
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So I am doing this project. And I need to
Write a program that finds the factors and number of factors for an integer.
Factors will be listed on 1 line for smallest to largest with a space separating each.
Continuously accept input until a 0 is entered, which is the sentinel. Do not factor 0.
I got the factoring part. This is the output I am currently getting.
Enter an Number
12
The factors are
1 2 3 4 6 12
It stops immediately after giving the factors. I am not sure on how to implement it to re prompt. I have tried loops but its not working. Also,How can I exclude 1 and the entered number.
This is how my output should look. It should stop once 0 is entered.
Enter a number: 12
There are 4 factors for the number 12: 2 3 4 6
Enter a number: 25
There are 1 factors for the number 25: 5
Enter a number: 100
There are 7 factors for the number 100: 2 4 5 10 20 25 50
Enter a number: 13
There are 0 factors for the number 13:
Enter a number: 0
Here is the code.
package com.FactorsProgram;
import jdk.swing.interop.SwingInterOpUtils;
import java.sql.SQLOutput;
import java.util.Scanner;
//Java Program to print all factors of a number using function
public class Main {
public static void main(String[] args) {
int N;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.println("Enter an Number");
N = scanner.nextInt();
// Calling printFactors method to print all
// factors of N
printFactors(N);
}
//This method prints all factors of N
public static void printFactors(int N) {
int i;
//Check for every number between 1 to N, whether it divides N. If K
//divides N, it means K is a factor of N
System.out.println("factors for the number " );
for (i = 1; i <= N; i++) {
if (N % i == 0) {
System.out.print(i + " ");
}
}
}
}
You had the right idea with having a loop - you need to loop and check that n isn't 0. E.g.:
System.out.println("Enter an Number");
n = scanner.nextInt();
while (n != 0) {
// Calling printFactors method to print all
// factors of N
printFactors(n);
System.out.println("Enter an Number");
n = scanner.nextInt();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
n is any integer value, given by the user. M is Multiply of all even numbers in 1..n:
M = 2 * 4 * 6 * … * ≤ n
Example :
int n = 9
output
int output = 2*4*6*8; // 384
My code:
Scanner in = new Scanner(System.in)
int n=inut.nextInt();
for(....)
In short you need to multiply all even numbers between 1 and n.
For this you can use a for-loop and if-statement. for-loop will give you all numbers between 1..n, and if-statement will reject odd numbers, leaving only even.
Last path would be to multiply all values.
int n = 9;// input;
int result = 1; // because you are multiplying, initial result must be 1
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) { // ignore all odd numbers
result *= i; // multiply result with next even value
}
}
System.out.println(result); // print the result: 384
You can look at this like an assembly line. At the start someone is generating numbers from 1 to n. Then someone called 'the filter' rejects (push to trash) all odd numbers, at the end of the line someone called 'the aggregator' multiplies all values into an result.
With Java 8 and streams this can be represented by:
int result = IntStream.range(1,n)// generate numbers from 1 to n
.filter(value->value%2==0) // reject all odd numbers
.reduce(1, (a,b)-> a*b); // multiple all values, with 1 as initial result
System.out.println(result);
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 3 years ago.
Improve this question
I have this homework. I'm having a hard time linking the first part with the second. This is what I have. I know I'm missing something at some point to indicate that from the number input it should add the next 100 numbers.
package test;
import java.util.Scanner;
public class Ex216 {
public static void main(String[] args) {
// Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen
Scanner myInput = new Scanner(System.in);
int =a
int sum;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
for (int n = a; n <= 100; n++)
System.out.printf("Sum = %d\n", sum);
}
}
This is what is casing me the trouble.
first of all,int =a is not a valid expression. it should be int a; then as you want to add next 100 number from the given value, you need to add those values into sum, such as sum = sum+number.
Here is a code snippet:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Take two integers p & q ... Sum should be printed of p to next q numbers including p .. if q <= 0 , then it will take the value of q again ...
Input :::: 3 2
Output :::: 7 (p=3 & q = 2 ..... So from 3 to next 2 numbers are 3 & 4 as it will be included in the sum ... Now we will have to print the sum of 3+4 and that's 7 )
Input ::: 4 -1 1
Output :::. 4 ( as the next number is 4 )
That means we have to start counting from the taken integer ....
Solve it and drop the solution here ......
import java.util.Scanner;
public class Ahmed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int sum=0;
if(q>=0){
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
System.out.println(sum);
}
}
if I take input 4 -1 -1 there is a error. Loop will continue until I take q input a positive number or 0;
Correct input 4 -1 -1 2 output 9.
Generally, when we have a loop and we dont know how many times it will repeat, we can use while.
Create a function like this:
private int readPositiveInt(Scanner sc){
int i = -1
while (i <= 0) {
i = sc.nextInt();
}
return i;
}
Then, you replace this line of code int q=sc.nextInt(); by this:
int q = readPositiveInt(sc);
Finally, once q will be positive for sure, you can remove these lines:
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write a program called PositiveNegative that reads an unspecified number of integers, determines how many positive and negative values have been entered, and computes the sum and average of the input values (not counting zeros). The reading of input ends when the user enters 0 (zero). Display the number of positive and negative inputs, the sum and the average. The average should be computed as a floating-point number. Design the program such that it asks the user if they want to continue with new inputs after each set of entries, ending the program only when they do not respond to the question with "yes".
Here is a sample run:
Input a list of integers (end with 0): 1 2 -1 3 0
# of positive inputs: 3
# of negative inputs: 1
The total: 5.0
The average: 1.25
Would you like to continue with new inputs? yes
Input a list of integers (end with 0): 0
No numbers were entered except 0
Would you like to continue with new inputs? no
and here is my code:
import java.util.*;
public class PositiveNegative
{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String answer;
int countpositive = 0;
int countnegative = 0;
int total = 0;
int num = 0;
System.out.print("Input a list of integers (end with 0): ");
do{
String list = input.nextLine();
for(int i = 0; ; i=i+2 ){
num = Integer.parseInt(list.substring(i,i+1));
if( num == 0)
break;
else if ( num > 0)
countpositive++;
else if ( num < 0)
countnegative--;
total = total + num;
}
double average = total/(countpositive + countnegative);
System.out.println("# of positive inputs: "+countpositive);
System.out.println("# of negative inputs: "+countnegative);
System.out.println("The total: "+total);
System.out.println("The average"+average);
System.out.println("\n ");
System.out.print("Would you like to continue with new inputs? ");
answer = input.next();
}while(answer.equalsIgnoreCase("Yes"));
}
}
I can compile file, but when i run it, i can't get result like sample run.
You are decrementing (countnegative--;) the count of negative integers instead of incrementing it (countnegative++;) when a negative integer is encountered.