Question
Asked Mar 27, 2019
29 views

2. Write a program called ListOddNumbers that prints the below (there is a single space between each number). 

Printing odd numbers between 1 and 50
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

3. Write a program called NestedLoop that prints the below. Use a for nested loop. You need to use if nested loop. 
1
22
333
4444
55555

check_circle

Expert Answer

Step 1

Solution:

 2.

 The below program is written in Java language to display the odd number between 1 and 50.

The program has the following algorithm:

  • The java class is created in the name of “ListOddNumbers”.
  • In the main() method,
    • The for loop executes until the number “50”. If condition satisfied,
      • Then, check whether the number is odd. If yes,
        • Display the odd numbers.
      • End the for loop.

 

Step 2

Program:

 //Define the class

public class ListOddNumbers 

{

     //Define the main() method

     public static void main(String[] args)

     {

          //Display the message

          System.out.println("Printing odd numbers between 1 and 50");

          //Loop executes until the number "50"

          for(int i=1;i<=50;i++)

          {

              //Check whether the number is odd

              if(i % 2 != 0)

              {                 

                   //Display the odd number with single space

                   System.out.print(i+" ");

              }

          }

                   //Display the empty line

          System.out.println();

     }

}

Step 3

Output:

Printing odd numbers between 1 and 50

1 3 5 7 9 11 13 15 17 19 2...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Write a program that coverts a zip code to a bar code. The bar codes use large and small bars. We wi...

A: Since no programming language is mentioned we will use Java to implement solution to this problem. F...

question_answer

Q: Give regular expressions for the following languages. (a) L1 = {anbm, n ≥ 3, m ≤ 4} (b) L2 = {anbm :...

A: Answer:Regular expression:a. L = {an bm, n &gt;=3, m &lt;= 4}

question_answer

Q: Attached Files:  getRandomLib.h: // This library provides a few helpful functions to return rand...

A: Program Instructions:Include the necessary header files.Print hello message and as for a response.De...

question_answer

Q: Nine coins are placed in a 3x3 matrix with some face up and some face down. You can represent the st...

A: As there is no programming language mentioned, we are writing this program in JAVA. The program is g...

question_answer

Q: Hello, I have a question with the program javascript and in the program I need to create a timer in ...

A: To display the greeting message, we can declare a new variable, suppose wish, to store the greeting ...

question_answer

Q: function myCompose(f,g){// TODO: return (f o g);// that is, a function that returns f(g(x)) wh...

A: Click to see the answer

question_answer

Q: Show that the following grammar is ambiguous: S→AB|aaaB,A→a|Aa,B→b.

A: A grammar is known as ambiguous when there is more than one derivation tree (right most derivation t...

question_answer

Q: I have a question about blockchain technologies. The question states "Describe three components of b...

A: Solution:Blockchain technologies:Blockchain is a technology which permits the digital data to be dis...

question_answer

Q: the book is "First Course on Fuzzy Theory and Applications"

A: The formula for calculating the Hamming distance (d) of fuzzy set: