Example 3. #include <iostream> using namespace std; int main()

Similar documents
Why Is Repetition Needed?

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

BITG 1233: Introduction to C++

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Chapter 3 Problem Solving and the Computer

Chapter 4 - Notes Control Structures I (Selection)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Increment and the While. Class 15

I/O Streams and Standard I/O Devices (cont d.)

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Lab Instructor : Jean Lai

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Programming Language. Functions. Eng. Anis Nazer First Semester

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

CS 103 Lecture 3 Slides

PROGRAMMING EXAMPLE: Checking Account Balance

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Engineering Problem Solving with C++, Etter/Ingber

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Introduction to C++ (Extensions to C)

REPETITION CONTROL STRUCTURE LOGO

A First Program - Greeting.cpp

C++ basics Getting started with, and Data Types.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Ch 6. Functions. Example: function calls function

Introduction to Programming EC-105. Lecture 2

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Chapter Four: Loops II

Chapter 5: Control Structures II (Repetition)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

CHAPTER 3 Expressions, Functions, Output

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Getting started with C++ (Part 2)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

c++ Solutions eedsohag.epizy.com Ahmed Ali

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Chapter 15 - C++ As A "Better C"

C++ Final Exam 2017/2018

Problem Solving: Storyboards for User Interaction

BITG 1113: Array (Part 1) LECTURE 8

Week 3: File I/O and Formatting 3.7 Formatting Output

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

Chapter 3 - Functions

Fundamentals of Programming CS-110. Lecture 2

Chapter 3. Numeric Types, Expressions, and Output

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Name SECTION: 12:45 2:20. True or False (12 Points)

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Solving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++


Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Operations. Making Things Happen

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

What we will learn about this week:

CS242 COMPUTER PROGRAMMING

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Lab # 02. Basic Elements of C++ _ Part1

CS2141 Software Development using C/C++ C++ Basics

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Looping. Arizona State University 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

3.1. Chapter 3: The cin Object. Expressions and Interactivity

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Chapter 1 INTRODUCTION

C++ Programming: From Problem Analysis to Program Design, Third Edition

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Sample Code: OUTPUT Daily Highs & Lows

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor

Definition Matching (10 Points)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Control Structures. Repetition (Loop) Structure. Repetition (Loop) Structure. Repetition (Loop) Structure. CS225: Slide Set 8: C++ Loop Structure

C++ Quick Reference. switch Statements

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

LECTURE 02 INTRODUCTION TO C++

Unit 7. 'while' Loops

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

Transcription:

1 Repetition Structure Examples Example 1 #include <iomanip> float number, sum=0; // number to be read cin >> number; // read first number cout << fixed << setprecision(3); while ( number > 0 ) // loop while number read > 0 sum = sum + number; cout << sum<<endl; // sum the positive number cin >> number; // read next number Example 3 cout << "Enter a number : "; int ival; for (int i=1;i<=5;i=i+1) cout << i << "x" << ival << "=" << i*ival << endl; return(0); Example 2 char letter; int counter=-1; // loop until 'q' is read ++counter; // increment letter counter cout <<" Enter a letter, q to end"<< endl; cin >> letter; // read next letter while ( letter!= 'q'); cout<<"the number of letters read is "<<counter<<endl; Example 4 // Outputs a table of x and sin(x) // Uses manipulators for output #include <cmath> #include <iomanip> float x, i; cout << fixed << showpoint; for (i = 0.0; i <= 1.0; i=i+0.1 ) x = 0.1 * i; cout << setprecision(1) << setw(4) << x; cout << setprecision(6) << setw(10) << sin(x) << endl;

2 Example 5 int counter, sum=0; for (counter = 1;counter < 10;++counter) sum = sum + counter; cout << "The sum is " <<sum<< endl;//display the sum cout << "Enter an integer greater"<< " than or equal to 0 and " << "less than 100: "; cin >> guess; cout << endl; if (guess == num) cout<<"you guessed the correct "<< "number."<< endl; isguessed = true; if (guess < num) cout << "Your guess is lower than the " << "number.\n Guess again!" << endl; cout << "Your guess is higher than "<< "the number.\n Guess again!"<< endl; i++; //end while cout << "The number guessed " << num; Example 6 //Flag-controlled while loop. //Number guessing game. #include <iostream > #include <ctime > //declare the variables int num; //variable to store the ranm number int guess,i=0; //variable to store the number //guessed by the user bool isguessed; //boolean variable to control the loop num = (rand() + time(0)) % 100; isguessed = false; while ((isguessed==false) && (i<10)) Example 7 // Program: Telephone Digits;Example of a sentinel- // controlled loop. This program converts uppercase // letters to their corresponding telephone digits. char letter; cout << "Program to convert uppercase "<<"letters to their corresponding telephone digits." << endl;

3 cout << "To stop the program enter #." << endl; cout << "Enter a letter: "; cin >> letter; cout << endl; while (letter!= '#') cout <<"The letter you entered is: "<<letter<< endl; cout << "The corresponding telephone "<<"digit is: "; if (letter >= 'A' && letter <= 'Z') switch (letter) case 'A': case 'B': case 'C': cout << "2" <<endl; case 'D': case 'E': case 'F': cout << "3" << endl; case 'G': case 'H': case 'I': cout << "4" << endl; case 'J': case 'K': case 'L': cout << "5" << endl; case 'M': case 'N': case 'O': cout << "6" << endl; case 'P': case 'Q': case 'R': case 'S': cout << "7" << endl; case 'T': case 'U': case 'V': cout << "8" << endl; case 'W': case 'X': case 'Y': case 'Z': cout << "9" << endl; cout << "Invalid input." << endl; cout << "\nenter another uppercase " << "letter to find its corresponding telephone digit."<< endl; cout << "To stop the program enter #."<< endl; cout << "Enter a letter: "; cin >> letter; cout << endl; //end while Example 8 int sum, i=0; // the sum of the numbers read bool ne; // used to control the while loop int number; // the number read sum = 0; // initialize the sum ne = false; // initialize the boolean variable ne cout << "Enter numbers, negative number to end "; cin >> number; // read next number if ( number > 0 ) // sum number if

4 i++; sum = sum + number; //sum the positive numbers ne = true; // terminate the loop while (!ne ) ;// loop while not ne cout << "Sum = " << sum<<endl; cout << "i = " << i; Example 9 /* Program to reverse the digits of a number */ #include<iostream> int number, rev_number=0, right_digit; cout << "Enter your number: "; cin >> number; right_digit = number % 10; rev_number=rev_number*10+right_digit; number = number / 10; while ( number!= 0 ); cout <<"After reversing digits : "<<rev_number <<endl; system("pause"); Another version /* Program to reverse the digits of a number */ #include<iostream> int number, rev_number, rev_number1, right_digit; cout << "Enter your number: "; cin >> number; rev_number1=number % 10; number = number / 10; right_digit = number % 10; rev_number=rev_number*10+right_digit; number = number / 10; while ( number!= 0 ); cout <<rev_number*10 + rev_number1 <<"\n"; Example 10 int q,w; char z; cout<<"please enter the first number :" ; cin>>q; cout<<"please enter the second number :" ; cin>>w; cout<<"answer is = " ; cout<<q+w<<endl; cout<<"do you want to add more numbers:" ; cin>>z;

5 while(z=='y' z=='y'); Example 11 // average.cpp - average a sequence of numbers int num=0, sum=0; cout << "Enter number (-1 to stop) : "; int ival; while (ival!= -1) sum = sum + ival; num = num + 1; cout << "Enter number (-1 to stop) : "; if (num==0) cout << "No numbers entered" << endl; cout << "Average=" << (uble)sum/num << endl; return(0); Example 12 float r; int ival, p=1,i=0; cout << "Enter a number (99 to end) : "; while (ival!= 99) i++; p = p * ival; cout << "Enter a number (99 to end) : "; r=p/i; cout << "Result=" << r << endl; return(0); Example 13 int i, j; for(i=1;i<=5;i++) for(j=1;j<=10-2*i;j++) cout<<" "; for(j=1;j<=4*(i-1)+1;j++)cout<<"*"; for(i=5;i>=1;i--) for(j=1;j<=10-2*i;j++) cout<<" "; for(j=1;j<=4*(i-1)+1;j++)cout<<"*";

6 Example 14 int i, j, n, b=1; cout<<"please enter a width "; cin >> n; cout << endl; int p = n/2; if (n%2==0 && p%2==1) b=0; while (b==1); for(i=1;i<=n/2;i++) for(j=1;j<=n-2*i;j++) cout<<" "; for(j=1;j<=4*(i-1)+1;j++)cout<<"*"; for(i=n/2;i>=1;i--) for(j=1;j<=n-2*i;j++) cout<<" "; for(j=1;j<=4*(i-1)+1;j++)cout<<"*"; Example 15 using namespace std ; int main () uble p=1;// p is a great number so use uble int a=3; while( a<=15)// after while not put ";" p=p*a; a++; cout << " p = " << p<< " a = " << a<<endl; cout << "Result = "<< p; OR #include <cstdlib> int i; uble p=1; for(i=3;i<=15;i++) p*=i; cout<<"result = : "<<p; Example 16

7 char Typedletters; int nump=0; cout << "Start typing a character: "; cin >> Typedletters; while (Typedletters!= '$' ) nump++; cout << "Start typing a character: "; cin >> Typedletters; cout << "number of small character " << nump << endl; Example 18 int i, j; for(i=0;i<7;i++) for(j=7-i;j>=1;--j)cout<<"*"; Example 17 int i, j, k; for(i=1;i<=7;i++) for(j=1;j<=i;++j) cout<<j; for(k=7-i;k>=1;k--) cout<<"*"; Example 19 int m1,m2,m3,i=0; cout <<"Please enter three marks : "; cin>> m1>>m2>>m3; if (m1<40) i++; if (m2<40) i++; if (m3<40) i++; // mark is absent if we type 101 if (m1!= 101 && m2!=101 && m3!=101) if (i<=1) if (((m1+m2+m3)/3.0) >=50) cout << "You Pass!" << endl;

8 cout << "You Fail!" << endl; cout << "You Fail!" << endl; cout << "You Fail!" << endl; // ending brace aligned with the start of the statement Example 20 char choice,tchoice,bchoice; int Mchoice; cout << "T : Toyota "<<endl; cout << "M : Mercedes "<<endl; cout << "B : BMW "<<endl; cout << "E : Exit "<<endl; cout << " Choice : "; cin>>choice; if (choice == 'T') cout << "C : Camry "<<endl; cout << "O : Orion "<<endl; cout << "E : Exit "<<endl; cout << " Choice : "; cin>>tchoice; while(tchoice!='e'); if (choice =='M') cout << "1 : E300 "<<endl; cout << "2 : S500 "<<endl; cout << "3 : Exit "<<endl; cout << " Choice : "; cin>>mchoice; while(mchoice!=3); if (choice =='B') cout << "M : M5 "<<endl; cout << "N : N6 "<<endl; cout << "E : Exit "<<endl; cout << " Choice : "; cin>>bchoice; while(bchoice!='e'); while ( choice!='e') ;// loop while not ne