0% found this document useful (0 votes)
171 views

While and Do While Loops in Programming

The document is a lab report from a computer fundamentals lab course. The objectives of Lab 6 were to learn about while and do-while loops in C++. The lab report includes examples of while and do-while loops and their output. It also includes two tasks - the first involves a survey tally program using a do-while loop, and the second calculates the nth term of the Fibonacci sequence using a for loop.

Uploaded by

Zeshan Naseer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

While and Do While Loops in Programming

The document is a lab report from a computer fundamentals lab course. The objectives of Lab 6 were to learn about while and do-while loops in C++. The lab report includes examples of while and do-while loops and their output. It also includes two tasks - the first involves a survey tally program using a do-while loop, and the second calculates the nth term of the Fibonacci sequence using a for loop.

Uploaded by

Zeshan Naseer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Electrical Engineering

Riphah College of Science and Technology


Riphah International University, Lahore

Program: B.Sc. Electrical engineering Semester: I

Subject: CS-101 Computer Fundamentals Lab Date: 7-12-2020

Lab 6:
While and do While loops in programming

OBJECTIVES:
i. Learn about while and do-while loops in C++

Name: Waleed Shahzad

SAP ID: 26564

No. Lab Evaluation Scheme Obtained


Marks
1 Understanding and Ability to Conduct Experiment. (0-5)
2 Implementation and Results (0-5)
Total
No. Lab Report Scheme Obtained
Marks
1 Report Content(0-5)
2 Data Presentation and conclusion(0-5)
Total

Remarks (if any): ………………………………


Signature of faculty: …………………………………
Lab 6

WHILE/DO-WHILE LOOPS

PURPOSE

1. To understand the iterative statements and their use in C


2. To understand and practice
 While loop
 Do-while loop

PROCEDURE

1. In the lab students should complete both the “Examples” and “Lab Tasks”.

PRE-LAB READING ASSIGNMENTS

Before starting this lab, students should read/revise the lectures presented on given topics in the class.

Examples

1. Determine the result of the following code segments. Explain their results.
a)
char letter = 'a';

while (letter != 'x')


{
cout<< "Please enter a letter";
cin>>letter;
cout<<"The letter you entered is ";
cout<< letter;
}
b)

float total_grade=0.0;
float grade_avg = 0.0;
float grade;
int grade_ctr = 0;

do
{
cout<< “What is your grade? (-1 to end) ``;
cin>> grade;
if (grade >= 0.0)
{
total_grade += grade; // Add to total.
grade_ctr ++;
} // Add to count.

}
while (grade >= 0.0); // Quit when -1 entered.

grade_avg = (total_grade / grade_ctr);


cout<<“You made a total of points ”;
cout<<total_grade;
cout<<“Your average was”<< grade_avg;
if (total_grade >= 450.0)
cout<<“** You made an A!!”;
Lab Task

Task 1

1. Write a program that performs a survey tally on beverages. The program should prompt for the next
person until a sentinel value of –1 is entered to terminate the program. Each person participating in the
survey should choose their favorite beverage from the following list:

1. Coffee
2. Tea
3. Coke
4. Orange Juice

Sample Run:

Please input the favorite beverage of person #1: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
4
Please input the favorite beverage of person #2: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
1
Please input the favorite beverage of person #3: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
3
Please input the favorite beverage of person #4: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
1
Please input the favorite beverage of person #5: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
1
Please input the favorite beverage of person #6: Choose 1, 2, 3, or 4 from the above menu or -1 to
exit the program
-1

The total number of people surveyed is 5. The results are as


follows: Beverage Number of Votes
*******************************
* Coffee 3
Tea 0
Coke 1
Orange Juice 1
SOLUTION :

#include<iostream>

using namespace std;

int main()

int count=0;

int personCount=1;

int choice;

int i=-1;

do

cout<<"Please input the favourite beverage of person :"<<personCount<<endl;

cout<<"enter 1 for coffee"<<endl;

cout<<"enter 2 for coke"<<endl;

cout<<"enter 3 for orangrJuice"<<endl;

cout<<"enter 4 for tea"<<endl;

cin>>choice;

cout<<"to exit press -1 or any other number to continue : "<<endl;

cin>>i;

} while(i!=-1);

cout<<"outside while loop";

}
OUTPUT :

Task 2

The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13,… , where the first two terms are 0 and 1, and each term
thereafter is the sum of the two preceding terms. Using this information, write a program that calculates
the nth number in a Fibonacci sequence, where n is entered into the program by the user.
SOLUTION :

#include<iostream>
using namespace std;
int main()
{
int n, t1=0,t2=1,nextterm=0;
cout<<"Enter the number of terms :";
cin>>n;
cout<<"Fibonacci series";
for(int i=1 ; i<=n; ++i)
{
//prints the first two terms
if(i==1)
{
cout<<" "<<t1;
continue;
}
if(i==2)
{
cout<<t2<<" ";
continue;
}
nextterm=t1+t2;
t1=t2;
t2=nextterm;
cout<<nextterm<<" ";

}
return 0;
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy