Hi every body
I have about while loops and I just answered Q: a & b plz help me

**Write a program that user while loops to perform the following steps:
a)Prompt the user to input two integers: firstNum and secondNum.
(firstNum must be less than secondNum.) .
b)Output all the odd numbers between firstNum and secondNum inclusive.
c)Output the sum of all the even number between firstNum and secondNum inclusive.
d)Output all the numbers and their squares between 1 and 10 .
e)Output the sum of the squares of all the odd numbers between firstNum and secondNum inclusive.
f)Output all the uppercase letters.

Recommended Answers

All 5 Replies

Show us what you have so far and explain what kind of difficulty you're having, and maybe someone will help.

Here are somethings to start with.
1. you need realize what goes in the while loop and set a checker to get out of the loop based on user input
2. b-f should all be individual methods that your main() calls so you can set and make sure each one works

import java.util.*;
public class Main {
    public static void main(String[] args) {
      Scanner input = new Scanner (System.in);
      int firstNum ;//The firstNum must be less than the secondNum .
      int secondNum ;
      int odd ;
      System.out.print("Enter the first number = " + "  ");
      firstNum = input.nextInt();
      System.out.print("Enter the second number = " + "  ");
      secondNum = input.nextInt();
      System.out.println("The odd number = " );

      while(firstNum < secondNum) {
      if(firstNum%2 != 0) {
      System.out.println(firstNum);
      }
        firstNum++;
      }



    }

}

I did that but I cann't do Q:c I know that even number (if (x % 2 == 0))

1. This is java and you are postin in C++ section, I just want to make sure that you are not turning in a java program for a C++ class.

2.

while(firstNum < secondNum) {
      if(firstNum%2 != 0) {
      System.out.println(firstNum);
      }
        firstNum++;
      }

should be in a methods that gets called; right now as soon as you hit this while loop your program forgets what the initial 2 numbers were.

3. So this is b again and this code goes before public class main(){...}

class Auxiliaries{
    public static int findOddSum(int x, int y){  //b
    int sum=0
    int i=x;
    while(i%2==1&&i<=y){sum+=i; i++;}
    return sum;
    }
    //then another method for c
    
}

to use the Auxiliaries class in main, use:

int oddSum=Auxiliaries.findOddSum(number1,number2);

This way you don't lose the values of the numbers as you are doing the operations.

1. This is java and you are postin in C++ section, I just want to make sure that you are not turning in a java program for a C++ class.

2.

while(firstNum < secondNum) {
      if(firstNum%2 != 0) {
      System.out.println(firstNum);
      }
        firstNum++;
      }

should be in a methods that gets called; right now as soon as you hit this while loop your program forgets what the initial 2 numbers were.

3. So this is b again and this code goes before public class main(){...}

class Auxiliaries{
    public static int findOddSum(int x, int y){  //b
    int sum=0
    int i=x;
    while(i%2==1&&i<=y){sum+=i; i++;}
    return sum;
    }
    //then another method for c
    
}

to use the Auxiliaries class in main, use:

int oddSum=Auxiliaries.findOddSum(number1,number2);

This way you don't lose the values of the numbers as you are doing the operations.

thanx alot , next time I will post in Java section

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.