Click to See Complete Forum and Search --> : iteration


susancbk
10-19-2004, 09:15 PM
im preparing for an exam on iteration. here are a couple exam review questions I was given that Im a little unsure about and would like any input you all may have on what ive come up with so far.

code segment that displayes the sum of odd integers from 1 to n
public static void main(String[] args) {
double value = 0;
double valueSum = 0;
double n = 0;

while(value >=1 && value%2 != 0) {
valueSum += value;
}
if (value > n) {
System.out.print("The sum of numbers from 1 to " + n + " is " + valueSum);
}
else {
System.out.println("No list to add");
}
}}

code segment that displays the sum of ingtegers from a to b
public static void main(String[] args) {
double value = 0;
double valueSum = 0;
double a = 0;
double b = 0;

while(value >= a) {
valueSum += value;
}
if (value > b) {
System.out.print("The sum of numbers from " + a + " to " + b + " is " + valueSum);
}
else {
System.out.println("No list to add");
}
}}

a segment that countes the number of its inputs from the standard input stream in each of these categories: positive, negative, zero i checked for negative and positive, but am unsure where to enter the zero tally.

public static void main(String[] args) {
double valuesPositive = 0;
double valuesNegative = 0;
double ValuesZero = 0;

System.out.println("Enter a number to be tallyed into categories of positive, negative or zeo");

Scanner stdin = new Scanner(System.in);
int value = stdin.nextDouble();

while (value >0){
++valuesPositive;
value = stdin.nextDouble();
}
if (value <0){
++valuesNegative;
value = stdin.nextDouble();
}
else {
System.out.print("no number to tally");
}
}}

write a code segment that displays the prime numbers in the range 1..n. A number is prime if its only factors are 1 and itself.
I have no idea how to do this.

javaNoobie
10-20-2004, 05:51 AM
Been quite awhile since I last touched java. Gonna give this a shot. ;)

Your first code segment seems to be missing a prompt for user to input n. Since the question requires you to display integer values, I don't see why you would want to use double data types. Also, your loop is kind of weird.. how is value being initialised?

Your second code segment problems are the same as of your first code segment. [missing prompt for user entry of variables a and b ,the loop and probably the data type..]

Third code segment I think requires an continuous loop to prompt for the user to keep on adding inputs until a certain loop count or when the user doesnt want to add anymore.

The last one.. well.. i made a javascript version to let you have a rough idea.[The following codes will not work in java at all{read sticky for more info}. This is just to let you have a rough idea.]

var n = prompt("Enter a number","0"); //prompt
n = Number(n);
var primeArr = new Array();
var isPrime = true;
for(var i=1;i<=n;i++){ //start from 1
for(var j = i-1; j>1;j--){ //since everything is divisible by 1, don't include 1
if(i%j==0){ //if any of the numbers between current i value and 2 is divisible, i is not a prime num
isPrime = false; //set isPrime to false to indicate current i value is not a prime number
}
}
if(isPrime){primeArr[primeArr.length]=i;} //if isPrime is true, add it to the array
isPrime = true; //reset isPrime for next loop iteration
}

for(var i=0;i<primeArr.length;i++){
alert(primeArr[i]);
}