You are on page 1of 15

Lab 4

Introduction to Computer Programming Lab

Lab Journal - 4
Name: Muhammad Furqan KHan

Enrollment #: 01-235181-033

Section BS (IT) 1A

Objective:
1) Understanding Looping Statements
2) Practicing while repetition structures
3) Practicing do-while repetition structures

Tools Required:

a) PC with Windows 7 Professional or onwards


b) Visual Studio 2013 onwards

Attempt the following tasks:


Task 1 : Give output of the following pieces of codes :
1 #include<iostream> Output:
. #include<conio.h>
using namespace std;
void main()
{
int start=1,end
= 10;

while(start<=end
)
{
cout <<
start <<endl;
start++;
}
getch();
}
2 #include<iostream> Output:
. #include<conio.h>
using namespace std;
void main()
{
int start=10,end
= 1;

Introduction to Computer Programming Lab


Page 1
Lab 4

do
{
cout <<
start<<endl;
start--;
}
while(start>=end
);
getch();
}
3 //Is the condition of Output and Answer:
. do-while true? Should Answer: the condition of do while is not true. But statements
the loop run? Why? executes one time because in do while loop..it must one time
execute.
#include<iostream> Output:
#include<conio.h>
using namespace std;
void main()
{
int n=6,i=7;
do
{
cout << i;
i++;
}
while (i <=n);
getch();
}
4 Identify and correct the error Identify and correct the error in the following code:
. in the following code:
while(i<10)
int x=0; {
do cout<<i;
{ i++;
cout<<x<<endl; }
x++;
}
while(x<=10)

Introduction to Computer Programming Lab


Page 2
Lab 4

Corrected Code: Corrected Code:


#include<iostream> #include<iostream>
#include<conio.h> #include<conio.h>
using namespace std; using namespace std;
void main() void main()
{ {
int x = 0; int i = 0;
do while (i<10)
{ {
cout << x << cout << i;
endl; i++;
x++; }
} while (x <= 10); _getch();
_getch();
}
}
5 // Calculate the Output:
square of first 10
natural numbers.

int j=0;
while( j<10)
{
cout<< “ square
is: “<< j*j;
j++; //same as j
= j+1;
}
cout<<endl;

6 //Calculate the Output:


cube of all the
natural numbers
where the // cube
is less than a
four digit
number.

int cube = 1;
int numb = 1;
while( cube < 999)
{
cube = numb *
numb * numb;
cout<< “ Number
is: “ <<numb<<endl;
cout<< “ Cube is:
“<<cube;
numb++;
}

Introduction to Computer Programming Lab


Page 3
Lab 4

7 //A program to Output:


divide 2 numbers
taken from user
and display the
quotient and
remainder. Also
continue to the
next calculation
again and again
if the user says
yes, and exit the
program when the
user says no.

int dividend, divisor,


quotient, remainder;
char ch;
do{
cout<< “ Enter
dividend : “;
cin>> dividend;

cout<<”Enter
Divisor: “;
cin>>divisor;

quotient =
dividend /divisor;
remainder =
dividend % divisor;

cout<<” Quotient
is: “<< quotient;
cout<<” Remainder
is : ”<<remainder;
cout<<”\n Do
another Division
Calculation? “;
cout<<”\\n(Press
y/n for yes/no) : “;
cin>>ch;
}while ( ch != ‘n’);
8 A program that Output:
will never end
until the user
asks to exit it.

char ch;
While(1)
{
cout<<
“\n\n\n****************
*********** “;

Introduction to Computer Programming Lab


Page 4
Lab 4

cout<< “\nWelcome
to my program “;
cout<<” \nIt will
not exit “;
cout<<”\n Until
you ask for it “;
cout<<”\n Press e
if you want to exit”;
cout<<”\n And any
other character if you
want to continue“;
cin>>ch;
if(ch == ‘e’)
exit(0);
}

Task 2 : Using a do-while loop write a program that asks user to enter a temperature in Fahrenheit
and convert it into Celsius. The program should ask the user if he/she wants to perform another
conversion. In case the user enters an ‘N’ the program should terminate. This is similar to one of
the practice tasks you have done. [Hint : Formula for conversion is: C = 5/9 (F-32)]

Code :

#include<conio.h>

using namespace std;

void main()

float result;

int fahrenheit;

char ch;

do{

cout << " Enter the temperature in Fahrenheit : ";

cin >> fahrenheit;

Introduction to Computer Programming Lab


Page 5
Lab 4

result = (fahrenheit - 32) * 5 / 9;

cout << "\n\n\nYOur temperature in celciusis\t\t\t" << result << endl;

cout << "\n\n(Press Y for again entering the temperature and N fot exit) : ";

cin >> ch;

} while (ch != 'n');

Output :

Task 3 : Write a program to find the average of natural numbers from 1 to N using do while loop.
Value of N should be entered by user.
[Hint : Average = sum of all numbers / total number of natural numbers]

Code :
using namespace std;
void main()
{

Introduction to Computer Programming Lab


Page 6
Lab 4

float a=1;
float b;
float avg=0;
float sum = 0;
cout << "enter the number=";
cin >> b;
while (a <= b)
{
sum += a;
a++;
}
avg = sum / b;
cout << "average of natural number is=" << avg;
_getch();
}

Output :

Task 4: Write a program that takes total number of students in a class. And then asks the user to
enter marks for each student in the class and compute the class average. Use a while loop to
compute the average. [Hint : Average = sum of marks of all students / total number of students]

Code :

#include<iostream>

#include<conio.h>

using namespace std;

void main()

Introduction to Computer Programming Lab


Page 7
Lab 4

float totalStudent=0;

float a=1;

float b = 0;

float sum = 0;

float average;

cout << "enter the total students in a class=";

cin >> totalStudent;

while (a <= totalStudent)

cout << "enter the" << a << " student marks" << endl;

cin >> b;

sum = sum + b;

a++;

average = sum / totalStudent;

cout << "average of six students is="<<average;

_getch();

Output:

Introduction to Computer Programming Lab


Page 8
Lab 4

Task 5: Write a program to calculate the factorial of a number using while loop

Code:

using namespace std;

void main()

int num1=0;

int a=1;

int f = 1;

cout << "enter any number=";

cin >> num1;

while (a<=num1)

f=f*a;

a++;

cout << "factorial is=" << f;

Introduction to Computer Programming Lab


Page 9
Lab 4

_getch();

Output :

Task 6: Write a program for a basic calculator (Using Switch Only) Your calculator should take
two integers. Then it should display options for different operations and then ask the user for
choice. Based on user’s choice it will perform the operation. The options will be displayed as
following

Press 1 for addition


Press 2 for subtraction
Press 3 for multiplication
Press 4 for division
Press 5 for finding the remainder
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{

int a;
int b;
int num1;
int result;

cout << "Press 1 for addition" << endl;


cout << "Press 2 for subtraction" << endl;
cout << "Press 3 for multiplication" << endl;
cout << "Press 4 for division" << endl;
cout << "Press 5 for modulus" << endl;

Introduction to Computer Programming Lab


Page 10
Lab 4

cout << "select any option ,,,you want to perform=";


cin >> num1;
cout << "enter the value of a=";
cin >> a;
cout << "enter the value of b=";
cin >> b;
switch (num1)
{
case 1:
result = a + b;
cout << "addition=" << result;
break;
case 2:
result = a - b;
cout << "subtraction=" << result;
break;
case 3:
result = a * b;
cout << "multiplication" << result;
break;
case 4:
result = a/b;
cout << "division=" << result;
break;
case 5:
result = a%b;
cout << "modulus=" << result;

Introduction to Computer Programming Lab


Page 11
Lab 4

default:
cout << "invalid input" << endl;
}
_getch();
}
Output:

Task 7: Now make the menu of the previous program stay on the screen until the user asks to exit
using an infinite loop. For that simply enclose the whole code in while(1) loop, as shown in one of
the practice tasks above and add a case 6: for exiting.
Hint: Enclose its code in the following while block, Except the declaration and initialization
statements.
while (1)
{
system(“cls”);
//adjust that code here, and add a 6th case as shown below
case 6:
exit(0);
}
Code:
#include<iostream>
#include<conio.h>
using namespace std;

Introduction to Computer Programming Lab


Page 12
Lab 4

int main()
{

int a;
int b;
int num1;
int result;
bool exit = false;
while (1)
{

cout << "Press 1 for addition" << endl;


cout << "Press 2 for subtraction" << endl;
cout << "Press 3 for multiplication" << endl;
cout << "Press 4 for division" << endl;
cout << "Press 5 for modulus" << endl;
cout << "press 6 for exit" << endl;

cout << "enter the value of a=";


cin >> a;
cout << "enter the value of b=";
cin >> b;
cout << "select any option ,,,you want to perform=";
cin >> num1;
switch (num1)
{
case 1:

Introduction to Computer Programming Lab


Page 13
Lab 4

result = a + b;
cout << "addition=" << result;
break;
case 2:
result = a - b;
cout << "subtraction=" << result;
break;
case 3:
result = a * b;
cout << "multiplication" << result;
break;
case 4:
result = a / b;
cout << "division=" << result;
break;
case 5:
result = a%b;
cout << "modulus=" << result;
case 6:
exit = true;
break;
default:
cout << "invalid input" << endl;
break;
}
if (exit)
break;
}
_getch();

Introduction to Computer Programming Lab


Page 14
Lab 4

return 0;
}
Output:

****************************************************

Introduction to Computer Programming Lab


Page 15

You might also like