Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Hello all! I have a assignment due tomorrow morning and I am totally clueless. I have tried reading a bunch of things on while loops but dont seem to understand it fully. This is my directions for the assignment:

Write a program that uses a while loop to perform the following steps:
Comment by labelling each part: //Part A, //Part B, etc...
A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
B.)Output all odd numbers between firstNum and secondNum.
C.)Output the sum of all even numbers between firstNum and secondNum.
D.)Output the numbers and their square between 1 and 10.
E.)Output the sum of the square of odd numbers between firstNum and secondNum.
F.)Output all uppercase letters.

Again I am new to while loops and I am totally lost. I have tried reading a bunch but I am better at learning if someone shows me how to do something. I am open to all suggestions. I have just completed 8 other programs using if else statements and now trying to get the hang of loops. Thanks!

This is my pathetic attempt so far LOL

```import java.util.Scanner;

public class whileLoop
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A
int firstNum = 10;
int secondNum = 20;

int oddNum = keyboard.nextInt();

//Part B
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}
}
}```

One problem I see is that statements in the loops and the if statement are not enclosed in {}s. That makes the code harder to read and understand and dangerous to change.

Also nested statements inside the loop and if statements need to be indented to make the code easier to read and understand.

Thanks for the tips!

You need to update the code by adding the {}s and indentations to make the code readable.

Okay I tried correcting it

You got part of it. There should be {}s for the loops and if statements

Can you please just help me besides critizing my curly braces? I understand its important but I need help with the actual program. I will figure out the format of it later. I need help with the logic ASAP

--- Update ---

Okay I think I fixed it. But i need to properly initialize firstNum and secondNum. I am so lost lol..

--- Update ---

updated a little. Im just trying to piece it together with what i know. If someone could help me clean it up and help with the rest that would be awesome!

```import java.util.Scanner;

public class whileLoop
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A
int firstNum;
int secondNum;
int i;

//Part B
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}
}
}

//Part C
int i=firstnumber;
int sum=0;
int sumofsqr=0;

while(i<=secondnumber)
{
if(i%2==0)
{
System.out.println(i);
sum+=i;
}
else
{
//Part E
sumofsqr+=i*i;
}
i++;
}
System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
}
}```

Hello all! I have a assignment due tomorrow morning and I am totally clueless. I have tried reading a bunch of things on while loops but dont seem to understand it fully. This is my directions for the assignment:

Write a program that uses a while loop to perform the following steps:
Comment by labelling each part: //Part A, //Part B, etc...
A.)Prompt the user to input 2 integers: firstNum and secondNum. Use 10 and 20.
B.)Output all odd numbers between firstNum and secondNum.
C.)Output the sum of all even numbers between firstNum and secondNum.
D.)Output the numbers and their square between 1 and 10.
E.)Output the sum of the square of odd numbers between firstNum and secondNum.
F.)Output all uppercase letters.

Again I am new to while loops and I am totally lost. I have tried reading a bunch but I am better at learning if someone shows me how to do something. I am open to all suggestions. I have just completed 8 other programs using if else statements and now trying to get the hang of loops. Thanks!

This is my pathetic attempt so far LOL its all pieced together from what i know and its missing a lot. If someone could show me how to make it all work together that would be awesome!

```import java.util.Scanner;

public class whileLoop
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A
int firstNum;
int secondNum;
int i;

//Part B
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}
}
}

//Part C
int i=firstnumber;
int sum=0;
int sumofsqr=0;

while(i<=secondnumber)
{
if(i%2==0)
{
System.out.println(i);
sum+=i;
}
else
{
//Part E
sumofsqr+=i*i;
}
i++;
}
System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
}
}```

Okay, I read the problem A.) only. then I looked at your code, you did not manage to prompt for user's input right?
Let's fix your problem from the top 1 by 1.
Do you know how to use the Scanner for user's input?

The curly braces are very important to organize the code and ensure proper execution.

need to properly initialize firstNum and secondNum.
What values should those two variables have? Where do those values come from? Is the Scanner class object: keyboard supposed to be used to read the user's input. The Scanner class has methods that will read the user's input.

--- Update ---

Yes I am familiar with Scanner. I prompted the user to input two integers but now i need to read from the keyboard the values they entered which should be 10 and 20. Above I posted the directions for the assignment in my original post. I need to initialize firstNum (10) and secondNum (20) but not sure where to initialize them.

Originally Posted by dicdic
Okay, I read the problem A.) only. then I looked at your code, you did not manage to prompt for user's input right?
Let's fix your problem from the top 1 by 1.
Do you know how to use the Scanner for user's input?

I need to initialize firstNum (10) and secondNum (20)
You can initialize them those variables with those values on the same lines when you define them just like you did with the variable: temp.

Okay I got it to work and output all odd numbers between 10 and 20... onto the next part!
```import java.util.Scanner;

public class whileLoop
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A

//Part B
int firstNum = keyboard.nextInt();
int secondNum = keyboard.nextInt();
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}
}
}```

Glad you are making progress. As dicdic said, do it one step at a time.

Part C) I need to output the sum of all even numbers between firstNum and secondNum (10,20). How would I transition into that? I have only done simple one part programs.

Parts of the code could be used for that: the loop that goes from one number to the other.
What parts of Part C) are you having problems with? Make a list of the steps needed to do it.

```		int i = firstNum;
int sum = 0;

while(i <= secondNum)
{
if(i % 2 == 0)
{
System.out.println(i);
sum += i;
}
System.out.println("Sum of even " + sum + ");
}
}```

--- Update ---

I guess that is how I would get the sum of all even numbers between first and second Num. But not sure how to tie it into the program as a whole

If there is a loop that goes from the first to the last number, there could be more than one section of code in that loop that used the sequence of numbers used in the loop.

begin loop from start to end
test1 number and do something
test2 number and do something else
test3 number and do another thing
end loop
Report results for the tests

Can you show me a example? not with my assignment but something general? I am a visual learner. I am not asking for someone to do my assignment becasue i want to learn it but i also need something visual

Merge the code you have written in post#17 (the if statement for even numbers etc) with that in post#13
something visual
See the pseudo code in post#18.

This is my whole program so far... steps A through E. The directions again are posted above. I am just piecing together what I know. The logic is there but the format definitely isnt.
```import java.util.Scanner;

public class whileLoop
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A
System.out.println("Please enter two integers, the first must be less than second: ");

//Part B
int firstNum = keyboard.nextInt();
int secondNum = keyboard.nextInt();
int temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 != 0)
{
System.out.println(""+temp);
}
temp++;
}

//Part C
int i = firstNum;
int sum = 0;

while(i <= secondNum)
{
if(i % 2 == 0)
{
System.out.println(i);
sum += i;
}
//Part D
for (int i= 1; i <= 10; i++)
{
System.out.println(i+" "+i*i);
{
//Part E
while(firstNum <= secondNum)

sumofsqr += i * i;
}
i++;
}
System.out.println("Sum of even " + sum + " Sum of sqr of odd " + sumofsqr);
}
}```

Just use one loop that goes from start to end and has inside of the loop the tests, etc

See post#18

just use if and if else statments to tie it all together?

If the separate tests are related, then they could be tied together. Like even vs odd number: if not even then must be odd.

The comments in the code should say what the code is doing:
//Part D
does not tell anyone reading the code what the code following that comment is doing. It's better to have it all in one place and not require someone to hunt for where Part D is defined.

I will give you credit for trying to comment the code. Most students don't any at all.

```import java.util.Scanner;

public class twoIntegers
{
public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);

//Part A
//Input two integers

+ "the first must be less than second: ");

//Part B
//All odd numbers between firstNum and secondNum

int firstNum = keyboard.nextInt();
int secondNum = keyboard.nextInt();
int temp = firstNum;
int sum = 0;
String oddNum = "";
int sumSqr = 0;

while(temp <= secondNum)
{
if(temp % 2 != 0)
{
oddNum = oddNum + temp + " ";
}
temp++;
}
System.out.println("The odd numbers are: " + oddNum);

//Part C
//Sum of all even numbers between firstNum and secondNum

temp = firstNum;
while(temp <= secondNum)
{
if(temp % 2 == 0)
{
sum += temp;
}
temp++;
}
System.out.println("The sum of all even numbers: " + sum);

//Part D
//Output the numbers and their square between 1 and 10

int i = 1;

while(i <= 10)
{
System.out.println("" + (i * i));
i++;
}

//Part E
//Output the sum of square of odd numbers

temp = firstNum;

while(temp <= secondNum)
{
if(temp % 2 != 0)
{
sumSqr += temp * temp;
}
temp++;
}
System.out.println("Sum of the square of odd numbers: " + sumSqr);

//Part F
//Output all uppercase letter.

i = 0;

while(i < 26)
{
System.out.println((char) (i + 65));
i++;
}
}
}```

--- Update ---

This is my finished program. However the output is correct but messy. How would I output the output horizontally instead of vertically?

Page 1 of 2 12 Last