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

Increment Decrement operator While Loop

The lab manual covers programming fundamentals focusing on increment/decrement operators and while loops in C++. It includes coding examples demonstrating pre and post increment/decrement operations, as well as various applications of while loops for tasks such as summing numbers, calculating factorials, and analyzing student grades. The manual serves as a practical guide for students to understand and implement these concepts in C++.

Uploaded by

iqraghouri440
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)
12 views

Increment Decrement operator While Loop

The lab manual covers programming fundamentals focusing on increment/decrement operators and while loops in C++. It includes coding examples demonstrating pre and post increment/decrement operations, as well as various applications of while loops for tasks such as summing numbers, calculating factorials, and analyzing student grades. The manual serves as a practical guide for students to understand and implement these concepts in C++.

Uploaded by

iqraghouri440
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/ 15

Lab Manual: Programming Fundamentals

University of Management and Technology,


Lahore Campus

Lab Manual
Instructor: Waqar Ashiq
Lecturer, Software Engineering Department
Email: [email protected]

Increment /Decrement operator & While Loop


Coding Examples:
Decrement operator:
include <iostream>

using namespace std;

int main()

int x=10,a;

a=--x;

cout<<"pre decrement operator";

cout<<"\na = "<<a;

cout<<"\nx ="<<x;

return 0;

Output:
Pre decrement operator

a=9

x=9
Lab Manual: Programming Fundamentals

include <iostream>

using namespace std;

int main()

int x=10,a;

a=x--;

cout<<"Post decrement operator";

cout<<"\na = "<<a;

cout<<"\nx ="<<x;

return 0;

Output:
a=10

x=9

Increment Operator:
include <iostream>

using namespace std;

int main()

int x=10,a;

a=++x;

cout<<"pre increment operator";

cout<<"\na = "<<a;

cout<<"\nx ="<<x;

return 0;
Lab Manual: Programming Fundamentals

Output:
Pre increment operator

a=11

x=11

include <iostream>

using namespace std;

int main()

int x=10,a;

a=x++;

cout<<"Post increment operator";

cout<<"\na = "<<a;

cout<<"\nx ="<<x;

return 0;

Output:
Post increment operator

a=10

x=11

While Loop:
• Loop: a control structure that causes a statement or statements to repeat
• General format of the while loop:
while (expression)
Lab Manual: Programming Fundamentals

statement;
• statement; can also be a block of statements enclosed in { }

Example 1: Write a program using while loop that show


the sum of first five numbers.
Code:
#include<iostream>
using namespace std;
int main()
{
int number=1,sum=0;
while(number<=5)
{
sum=sum+number;
number++;
}
cout<<"Sum is "<<sum;
return 0;
}
Lab Manual: Programming Fundamentals

Execution Process:

Flow chart:

Example 2: Write a program for take input the list of


numbers and print their squares.
Code:
#include <iostream>
using namespace std;
int main()
{
int minimum=1;
Lab Manual: Programming Fundamentals

int maximum=10;
int num=minimum;
cout<<"Number number squared \n ";
while(num<=maximum)
{
cout<<num<<"\t\t"<<num*num<<endl;
num++;
}
return 0;
}

Output:
Lab Manual: Programming Fundamentals

Example 3: Write a program in C++ to find the factorial


of a number.

#include <iostream>
using namespace std;

int main()
{
int n;
cout<< "Input a number to find the factorial:";
cin>>n;
int factorial = 1;
int i = 1;
while (i <= n) {
factorial = factorial*i;
i++;
}
cout << factorial;
}
Lab Manual: Programming Fundamentals

Example 4: A class of ten students took a quiz. The


grades (integers in the range 0 to 100) for this quiz are
available to you. Calculate and display the total of all
student grades and the class average on the quiz.
#include <iostream>
using namespace std;

int main()
{
int total; // sum of grades entered by user
int gradeCounter; // number of the grade to be entered next
int grade; // grade value entered by user
double average; // average of grades
// initialization phase
total = 0; // initialize total
gradeCounter = 1; // initialize loop counter

// processing phase
while ( gradeCounter <= 10 ) // loop 10 times
{
cout << "Enter grade: "<<gradeCounter; // prompt for input
cin >> grade; // input next grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter by 1
} // end while
Lab Manual: Programming Fundamentals

// termination phase
average = total / 10; // integer division yields integer result
// display total and average of grades
cout << "\nTotal of all 10 grades is " << total << endl;
cout << "Class average is " << average << endl;
}

Formulating Algorithms (Sentinel-Controlled Repetition)


Example 5: Develop a class averaging program that will
process an arbitrary number of grades each time the
program is run.
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade; // one grade
float average; // average of grades
// initialization phase
total = 0; // Clear total
gradeCounter = 0; // prepare to loop
// processing phase
cout << "Enter grade, type -1 to indicate the end outer: ";
cin >> grade; // input grade
while ( grade != -1)
Lab Manual: Programming Fundamentals

{ // loop until -1 is entered


total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
cout << "Enter grade, type -1 to indicate the end inner: ";
cin >> grade;
}
// termination phase
if ( gradeCounter != 0 )
{
average = static_cast< float >( total ) / gradeCounter;
cout << "Class average is " << setprecision( 2 ) <<fixed<< average << endl;
}
else
{
cout << "No grades were entered"<<endl;
}

return 0; //indicate program ended successfully


}

Example 6: A college has a list of test results (1 = pass, 2


= fail) for 10 students. Write a program that analyzes
the results. If more than 8 students pass, print "Raise
Tuition".
//Analysis of examination results

#include <iostream>
Lab Manual: Programming Fundamentals

using namespace std;

int main()
{
// initialize variables in declarations
int passes = 0, // number of passes
failures = 0, // number of failures
studentCounter = 1, // student counter
result; // one exam result
// process 10 students; counter-controlled loop
while ( studentCounter <=10 )
{
cout << "Enter result (1=pass, 2=fail): ";
cin >> result;
if ( result == 1) // if/else nested in while
{
passes = passes + 1;
}
else
{
failures = failures + 1;
}
studentCounter = studentCounter + 1;
}
// termination phase
cout << "Passed " << passes << endl;
cout << "Failed " << failures << endl;
Lab Manual: Programming Fundamentals

if ( passes > 8 )
{
cout<< "Raise tuition " << endl;
}

return 0;
// successful termination
}

Example 7: Write a program to Printing and count Even


and Odd Numbers using While Loop in C++.
#include<iostream>
using namespace std;
int main()
{
int i=1,n,even=0,odd=0;
cout<<"\nEnter the Ending value:";
cin>>n;
while(i<=n)
{
if(i%2==0)
{
cout<<"\nEven numbers:";
cout<<"\n"<<i;
even++;
}
else
Lab Manual: Programming Fundamentals

{
cout<<"\nOdd numbers:";
cout<<"\n"<<i;
odd++;
}
i++;
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTOtal odd numbers:"<<odd;
return 0;
}
OR

#include<iostream>
using namespace std;
int main()
{
int i=1,n,even=0,odd=0;
cout<<"\nEnter the Ending value:";
cin>>n;
cout<<"\nEven numbers:";
while(i<=n)
{
if(i%2==0)
{
cout<<"\n"<<i;
even++;
Lab Manual: Programming Fundamentals

}
i++;

}
cout<<"\nOdd numbers:";
i=1;
while(i<=n)
{
if(i%2==1)
{
cout<<"\n"<<i;
odd++;
}
i++;
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTOtal odd numbers:"<<odd;
return 0;
}

Example 8: Write a program to check Even and Odd


Numbers using While Loop on the basis of user choice in
C++.
#include <iostream>
using namespace std;
int main()
Lab Manual: Programming Fundamentals

{
int choice = 1;
while( choice == 1 ){

int a;

cout << "Enter a number to check even or odd" << endl;


cin >> a; //input number

//check whether number is even or odd

if( a%2 == 0 ){
cout << "Your number is even" << endl;
}
else{
cout << "Your number is odd" << endl;
}

cout << "Want to check more : 1 for yes and 0 for no" << endl;

cin >> choice;


}

cout << "I hope you checked all your numbers" << endl;

return 0;
}

You might also like