0% found this document useful (0 votes)
2 views5 pages

Lab No. 2: Control Structures: Objective

The document outlines Lab No. 2 for the Faculty of Computing and Information Technology at Indus University, focusing on the implementation of control structures in programming. It includes objectives, examples of conditional statements and loops, as well as exercises for students to practice their skills in Java. The exercises involve categorizing numbers, performing arithmetic operations, and analyzing user input.

Uploaded by

jasonbrody3674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Lab No. 2: Control Structures: Objective

The document outlines Lab No. 2 for the Faculty of Computing and Information Technology at Indus University, focusing on the implementation of control structures in programming. It includes objectives, examples of conditional statements and loops, as well as exercises for students to practice their skills in Java. The exercises involve categorizing numbers, performing arithmetic operations, and analyzing user input.

Uploaded by

jasonbrody3674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Faculty of Computing and Information Technology (FCIT) Department of Computing

Indus University, Karachi

NAME OF STUDENT: ID No:

Lab No. 2: Control Structures


Objective:
Implementation of Control Structures (Selection, and Iterative statements)

Scope:
Sometimes we need to make a decision between two choices. The statements in by which we make this
decision are called conditional statements. We use loops to make one or more statements repeat for speci-
fied number of times. Clearly this has an importance when we wish to take more than one input from user
or do some calculations.

Example 1: This program is used to demonstrate the uses if-else condition.


Faculty of Computing and Information Technology (FCIT) Department of Computing
Indus University, Karachi

Example 2: This program is used to demonstrate the uses switch-case.

Object Oriented Programming using JAVA Lab Manual


Faculty of Computing and Information Technology (FCIT) Department of Computing
Indus University, Karachi

Example 3: This program demonstrates either an input number is prime or not

Object Oriented Programming using JAVA Lab Manual


Faculty of Computing and Information Technology (FCIT) Department of Computing
Indus University, Karachi

Example 4: Following program that keeps reading numbers (integers) from user until input is 0, then prints
average, sum, maximum and minimum.

Object Oriented Programming using JAVA Lab Manual


Faculty of Computing and Information Technology (FCIT) Department of Computing
Indus University, Karachi

Exercises

Exercise 1:
Write a program that specifies whether a given number (x) falls in one of the following categories (give x a
value from the code, don't read from user):
• 0 to 9
• 10 to 19
• 20 to 29
• None of the categories
For example, if x = 5, program should print "0 to 10", and if x = 44 it should print "None". Hint:
use if statements with Boolean expressions combined using &&.

Exercise 2:

Write a program that reads two integers and prints their sum like the code below (text shown in boldface is
supposed to be user input). Enter the first number: 3
Enter the second number: 4
The sum is 7

Exercise 3:

Write a program that asks the user to enter two numerical values (integers) and then select an operation
(addition, subtraction, multiplication and division) then prints the result based on operation selected. The
code below shows examples of the output (text shown in boldface is supposed to be user input). Enter first
number: 4 Enter second number: 2 1. Addition (+).
2. Subtraction (-).
3. Multiplication (*).
4. Division (/).
Enter operation number: 3
The result is 8

Exercise 4:

Write a program that reads 10 numbers from the user then prints out how many positive numbers andnegative
numbers user has entered (consider 0 a positive number).

Object Oriented Programming using JAVA Lab Manual

You might also like