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

Introduction To Computer Programming Lab Lab Journal - 4: Objective

This document provides instructions for a lab on computer programming. It outlines 7 tasks to help students practice loops, including while and do-while loops. The tasks include writing programs to output loop results, calculate averages, perform math operations, and more. Students are asked to write code to convert temperatures, find averages, calculate factorials, and build a basic calculator program with a menu that remains on screen until the user chooses to exit.

Uploaded by

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

Introduction To Computer Programming Lab Lab Journal - 4: Objective

This document provides instructions for a lab on computer programming. It outlines 7 tasks to help students practice loops, including while and do-while loops. The tasks include writing programs to output loop results, calculate averages, perform math operations, and more. Students are asked to write code to convert temperatures, find averages, calculate factorials, and build a basic calculator program with a menu that remains on screen until the user chooses to exit.

Uploaded by

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

Lab 4

Introduction to Computer Programming Lab

Lab Journal - 4
Name: _________________________________

Enrollment #: _________________________________

Date: ________________________________

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;

do
{
cout << start<<endl;

Introduction to Computer Programming Lab


Page 1
Lab 4

start--;
}
while(start>=end);
getch();
}
3. //Is the condition of do-while true? Output and Answer:
Should the loop run? Why?

#include<iostream>
#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 in the following code: Identify and correct the error in the
following code:
int x=0;
do while(i<10)
{ {
cout<<x<<endl; cout<<i;
x++; i++;
} }
while(x<=10)

Corrected Code: Corrected Code:

Introduction to Computer Programming Lab


Page 2
Lab 4

5 // Calculate the square of first 10 Output:


natural numbers.

int j=0;
while( j<10)
{
cout<< “ square is: “<< j*j;
j++; //same as j = j+1;
}
cout<<endl;
6 //Calculate the cube of all the Output:
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;
cout<< “ Cube is: “<<cube;
numb++;
}

7 //A program to divide 2 numbers Output:


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’);

Introduction to Computer Programming Lab


Page 3
Lab 4

8 A program that will never end Output:


until the user asks to exit it.

char ch;
While(1)
{
cout<<
“\n\n\n*************************** “;
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)]

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]

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]

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

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

Introduction to Computer Programming Lab


Page 4
Lab 4

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);
}

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

Introduction to Computer Programming Lab


Page 5

You might also like