LEC03: Decisions and Iterations

Similar documents
[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

IT 1033: Fundamentals of Programming Loops

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Building Java Programs

Building Java Programs

Control Statements: Part 1

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Topic 5 for loops and nested loops

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

CSC 1051 Data Structures and Algorithms I

Introduction to Java Applications

Course Outline. Introduction to java

Recap: Assignment as an Operator CS 112 Introduction to Programming

Chapter 4: Control structures. Repetition

Quiz Determine the output of the following program:

CS 112 Introduction to Programming

Chapter 4: Control structures

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Repetition, Looping CS101

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Building Java Programs Chapter 2

Condi(onals and Loops

Introduction to Computer Science Unit 3. Programs

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Control Structures II. Repetition (Loops)

Introduction to Java Applications

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS111: PROGRAMMING LANGUAGE II

ECE 122. Engineering Problem Solving with Java

Repetition, Looping. While Loop

Datatypes, Variables, and Operations

Object-Oriented Programming

Introduction to Java Programs for Packet #4: Classes and Objects

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

Algorithms and Conditionals

Instructor s Manual. for. Harvey M. Deitel Paul J. Deitel Jonathan Liperi Ben Wiedermann

1.3 Conditionals and Loops. 1.3 Conditionals and Loops. Conditionals and Loops

1.3 Conditionals and Loops

Reviewing all Topics this term

1.3 Conditionals and Loops

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CT 229 Java Syntax Continued

Unit 1 Lesson 4. Introduction to Control Statements

! Sequence of statements that are actually executed in a program. ! Conditionals and loops: enable us to choreograph control flow.

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Lab 9 Loops, Debugging

1.3 Conditionals and Loops

Task 1: Print a series of random integers between 0 and 99 until the value 77 is printed. Use the Random class to generate random numbers.

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Loops / Repetition Statements

Java Coding 3. Over & over again!

(c) ((!(a && b)) == (!a!b)) TRUE / FALSE. (f) ((!(a b)) == (!a &&!b)) TRUE / FALSE. (g) (!(!a) && (c-d > 0) && (b!b))

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java


Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Chapter 2. The Algorithmic Foundations of. Computer Science INVITATION TO. Computer Science. Tuesday, September 10, 13

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Instructor s Manual. Perl How to Program

Building Java Programs

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Lecture 3: Loops. While Loops. While Loops: Newton-Raphson Method. While Loops: Powers of Two

Loops. CSE 114, Computer Science 1 Stony Brook University

Repetition Structures

Summer Math Assignments for Students Entering Algebra II

CS112 Lecture: Repetition Statements

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

3 The L oop Control Structure

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Chapter 3. Selections

Building Java Programs

Comp 170 Test 1 SAMPLE June 8, 2000

Chapter 7. Iteration. 7.1 Multiple assignment

Building Java Programs

CS112 Lecture: Loops

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Computer Programming I - Unit 5 Lecture page 1 of 14

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CS 106 Introduction to Computer Science I

Repetition with for loops

APCS Semester #1 Final Exam Practice Problems

COMP-202 Unit 4: Programming with Iterations

Structured Programming. Dr. Mohamed Khedr Lecture 9

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CompSci 125 Lecture 11

AP Programming - Chapter 6 Lecture

Homework Set 1- Fundamentals

Building Java Programs

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Instructor: Eng.Omar Al-Nahal

Algorithm Discovery and Design

Introduction. C provides two styles of flow control:

Some Sample AP Computer Science A Questions - Solutions

Transcription:

LEC03: Decisions and Iterations Q1. Identify and correct the errors in each of the following statements: a) if ( c < 7 ); System.out.println( "c is less than 7" ); b) if ( c => 7 ) System.out.println( "c is equal to or greater than 7" ); Q2. What is the output of following program? System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); System.out.println("+----+"); Q3. What is the output of following program? int hightemp = 5; for (int i = -3; i <= hightemp / 2; i++) { System.out.println(i * 1.8 + 32); Q4. What is the output of following program? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print("*"); System.out.println(); // to end the line Q5. What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); System.out.println(); Q6. What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); System.out.println(line);

Q7. What is the output of the following nested for loops? for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print("."); for (int k = 1; k <= line; k++) { System.out.print(line); System.out.println(); Q8. Modify the previous code to produce this output:...1...2...3...4... 5... Q9. Write a method digitsum that accepts an integer parameter and returns the sum of its digits. a. Assume that the number is non-negative. b. Example: digitsum(29107) returns 2+9+1+0+7 or 19 c. Hint: Use the % operator to extract a digit from a number. Q10. Larger.java Write an application that asks the user to enter two integers, obtains the numbers from the user and displays the larger number followed by the words is larger. If the numbers are equal, print the message These numbers are equal. Q11. OddEven.java Write an application that reads an integer and determines and prints whether it is odd or even. [Hint: Use the remainder operator. An even number is a multiple of 2. Any multiple of 2 leaves a remainder of 0 when divided by 2.] Q12. Calculate.java Write a Java application that calculates and prints the sum of the integers from 1 to 10. Use a while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11 Q13. Gas.java Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a Java application that will get user input for the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results.

Q14. Wages.java Develop a Java application that will determine the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and pays time and a half for all hours worked in excess of 40 hours. You are given a list of the employees of the company, the number of hours each employee worked last week and the hourly rate of each employee. Your program should input this information for each employee (assume 3)and should determine and display the employee s gross pay. Q15. Largest.java The process of finding the largest value (i.e., the maximum of a group of values) is used frequently in computer applications. For example, a program that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write a pseudocode program and then a Java application that inputs a series of 10 single-digit numbers as characters and determines and prints the largest of the numbers. Your program should use at least the following three variables: a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been input and to determine when all 10 numbers have been processed); b) number: The current digit input to the program; c) largest: The largest number found so far.

Q16.Table.java Write a Java application that uses looping to print the following table of values: Q17.EtoX.java Write an application that computes the value of ex by using the formula Q18. Triangles.java Write an application that displays the following patterns separately, one below the othe r. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out.print( '*' ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to position to the next line. A statement of the form System.out.print( ' ' ); can be used to display a space for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.] Q19. Pi.java Calculate the value of pi from the infinite series Print a table that shows the value of pi approximated by computing one term of this series, by two terms, by three terms, etc. How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?

Q20. Power.java Write a method integerpower( base, exponent ) that returns the value of base exponent For example, integerpower( 3, 4 ) calculates 34 (or 3 * 3 * 3 * 3). Assume that exponent is a positive, nonzero integer and that base is an integer. Method integerpower should use a for or while loop to control the calculation. Do not use any math -library methods. Incorporate The main method should reads integer values for base and exponent from user and performs the calculation by calling integerpower method. [Note: your program should have two static methods: main and integerpower. The reason to make integerpower static is that main (which is static) can call only static methods.] Q21.Multiplicity.java Write a method multiple that determines, for a pair of integers, whether the second integer is a multiple of the first. The method should take two integer arguments and return true if the second is a multiple of the first and false otherwise. The main method should get two numbers from user, call this method and print the result.