SlideShare a Scribd company logo
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

More Related Content

PPTX
Lab101.pptx
PPTX
Lab01.pptx
DOC
Java final lab
PDF
C# Lab Programs.pdf
PDF
C# Lab Programs.pdf
PPTX
Lab-11-C-Problems.pptx
DOCX
java program assigment -1
PPTX
05A_Java_in yemen adenProgramming_IT.pptx
Lab101.pptx
Lab01.pptx
Java final lab
C# Lab Programs.pdf
C# Lab Programs.pdf
Lab-11-C-Problems.pptx
java program assigment -1
05A_Java_in yemen adenProgramming_IT.pptx

Similar to Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf (8)

DOCX
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
PDF
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
PDF
Simple Java Program for beginner with easy method.pdf
PPS
C programming session 03
PPTX
Loops in Python
DOCX
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
PPT
09-ch04-1-scanner class in java with explainiation
PPTX
Course work Questions and answers .pptx
Bottom of FormCreate your own FunctionFunctionsFor eac.docx
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
Simple Java Program for beginner with easy method.pdf
C programming session 03
Loops in Python
CMIS 102 Hands-On LabWeek 6OverviewThis hands-on lab .docx
09-ch04-1-scanner class in java with explainiation
Course work Questions and answers .pptx
Ad

More from fathimalinks (20)

PDF
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
PDF
Write a program in C++ that declares a structure to store the code n.pdf
PDF
Write a generic VBA Sub procedure to compute the value of the follow.pdf
PDF
Why are standards needed in data communication and networking What .pdf
PDF
Which of the following are mismatched A. Giordia - transmitted by f.pdf
PDF
Which of the following statements about human evolution is correct.pdf
PDF
What are the six major pollutions in the National Ambient Air Qualit.pdf
PDF
UPS Worldpart DiscussionWhat do you think are the operational str.pdf
PDF
True or False Justify your answer.Using multilevel signaling, it .pdf
PDF
This is question about excel cuers wish to answer shortly how to use.pdf
PDF
There is a requirement to design a system to sense the presence of gl.pdf
PDF
The investments of Charger Inc. include a single investment 11,010 .pdf
PDF
RNA polymerasebinds to DNA after the double strands have been unwoun.pdf
PDF
Research and explain the deviant actions of the Los Angeles Police D.pdf
PDF
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
PDF
Please revise the answer bellow.Q1. What historically have been Ap.pdf
PDF
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
PDF
List and explain at least three popular sixteenth century dance type.pdf
PDF
Introduction to Database Management SystemConsider the following .pdf
PDF
In female mammals, one of the X chromosomes in each cell is condensed.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write a program in C++ that declares a structure to store the code n.pdf
Write a generic VBA Sub procedure to compute the value of the follow.pdf
Why are standards needed in data communication and networking What .pdf
Which of the following are mismatched A. Giordia - transmitted by f.pdf
Which of the following statements about human evolution is correct.pdf
What are the six major pollutions in the National Ambient Air Qualit.pdf
UPS Worldpart DiscussionWhat do you think are the operational str.pdf
True or False Justify your answer.Using multilevel signaling, it .pdf
This is question about excel cuers wish to answer shortly how to use.pdf
There is a requirement to design a system to sense the presence of gl.pdf
The investments of Charger Inc. include a single investment 11,010 .pdf
RNA polymerasebinds to DNA after the double strands have been unwoun.pdf
Research and explain the deviant actions of the Los Angeles Police D.pdf
Question 1 10 points Save A TALIA Contribute NINA Co Contribute TALIA.pdf
Please revise the answer bellow.Q1. What historically have been Ap.pdf
Negligence Curtis R. Wilhelm owned beehives and kept the hives on pr.pdf
List and explain at least three popular sixteenth century dance type.pdf
Introduction to Database Management SystemConsider the following .pdf
In female mammals, one of the X chromosomes in each cell is condensed.pdf
Ad

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Lesson notes of climatology university.
PDF
Classroom Observation Tools for Teachers
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Basic Mud Logging Guide for educational purpose
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Insiders guide to clinical Medicine.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Supply Chain Operations Speaking Notes -ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharma ospi slides which help in ospi learning
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Renaissance Architecture: A Journey from Faith to Humanism
Microbial disease of the cardiovascular and lymphatic systems
Basic Mud Logging Guide for educational purpose
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Anesthesia in Laparoscopic Surgery in India
Insiders guide to clinical Medicine.pdf
Complications of Minimal Access Surgery at WLH
PPH.pptx obstetrics and gynecology in nursing
Supply Chain Operations Speaking Notes -ICLT Program

Q1. Print all the odd numbers from 1 to a user specifiable upper lim.pdf

  • 1. 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
  • 2. 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
  • 3. 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++) {
  • 4. if(i%2==0) System.out.print(" #"); else System.out.print("# "); } System.out.println(" "); } } } __________________ output: # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ____________Thank You