Q1 Print all the odd numbers from 1 to a user specifiable up

Q1. Print all the odd numbers from 1 to a user specifiable upper limit (inclusive) using both while and for loops

Example,
Please enter a number:

9

Odd numbers from 1 to 9 are:

1
3
5
7
9
HINT: The number 9 should be included!!!

2. Write a program called CheckerBoard that displays the following n×n (n=7) checkerboard pattern using two nested for-loops.

Solution

Q)1)

Using While Loop:

DisplayOddNumbers.java

import java.util.Scanner;

public class DisplayOddNumbers {

   public static void main(String[] args) {
       //Declaring variables
       int number;
       int i=1;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //getting the number entered by the user
       System.out.println(\"Please enter a number:\");
       number=sc.nextInt();
      
       //Displaying the odd numbers using while loop
       System.out.println(\"Odd numbers from 1 to 9 are:\");
       while(number>0)
       {
           //Checking the number is even or odd
           if(i%2!=0)
           {
               System.out.println(i);
           }
           i++;
           number--;
       }

   }

}

_____________________

Output:

Please enter a number:
9
Odd numbers from 1 to 9 are:
1
3
5
7
9

_______________

using for loop:

DisplayOddNosUsingForLoop.java

import java.util.Scanner;

public class DisplayOddNosUsingForLoop {

   public static void main(String[] args) {
       //Declaring variables
               int number;
              
               //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
              
               //getting the number entered by the user
               System.out.println(\"Please enter a number:\");
               number=sc.nextInt();
              
               //Displaying the odd numbers using while loop
               System.out.println(\"Odd numbers from 1 to 9 are:\");
               for(int i=1;i<=number;i++)
               {
                   //Checking the number is even or odd
                   if(i%2!=0)
                   {
                       System.out.println(i);
                   }
               }

   }

}

____________________

Output:

Please enter a number:
9
Odd numbers from 1 to 9 are:
1
3
5
7
9

_____________________

2)Q)

package org.students;

import java.util.Scanner;

public class CheckerBoard {

   public static void main(String[] args) {

       //Displaying the checker board using nested for loop
       for(int i=1;i<=7;i++)
       {
           for(int j=1;j<=7;j++)
           {
               if(i%2==0)
                   System.out.print(\" #\");  
               else
                   System.out.print(\"# \");              
           }
           System.out.println(\" \");
       }

   }

}

__________________

output:

# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #
# # # # # # #

____________Thank You

Q1. Print all the odd numbers from 1 to a user specifiable upper limit (inclusive) using both while and for loops Example, Please enter a number: 9 Odd numbers
Q1. Print all the odd numbers from 1 to a user specifiable upper limit (inclusive) using both while and for loops Example, Please enter a number: 9 Odd numbers
Q1. Print all the odd numbers from 1 to a user specifiable upper limit (inclusive) using both while and for loops Example, Please enter a number: 9 Odd numbers

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site