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.
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.