Lab Manual 06 - Increment Decrement Operator & While Loop
Lab Manual 06 - Increment Decrement Operator & While Loop
Lab- 06 Manual
Lab Instructor: Awais Amin
Department of Computer Science
Email: awais.amin@umt.edu.pk
Lab 06: Increment /Decrement operator & While Loop
Lab Rubrics
Coding Examples:
Decrement operator:
Example 1:
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
Example 2:
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:
Example 1:
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;
}
Output:
Pre increment operator
a=11
x=11
Example 2:
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)
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;
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;
int maximum=10;
int num=minimum;
cout<<"Number number squared \n ";
while(num<=maximum)
int main() {
int i = 10;
while (i > 0) {
cout << i << endl;
i--;
}
cout << "Liftoff!" << endl;
return 0;
}
int main() {
int secret_number = 7; // Fixed secret number
int guess;
int main() {
int num;
cout << "Enter a number to print its multiplication table: ";
cin >> num;
int i = 1;
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++;
}
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()
{
int choice = 1;
while( choice == 1 )
{
int a;
cout << "Enter a number to check even or odd" << endl;
cin >> a; //input number
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;
cout << "I hope you checked all your numbers" << endl;
return 0;
}
Lab Tasks
Practice Question:
1. Reverse Counting:
Develop a program to print numbers from 10 to 1 in descending order using a while loop.
2. Calculate Powers:
Create a program that calculates the powers of 2 from 1 to 10 (2^1 to 2^10) using a while loop.
4. Sum of Squares:
Develop a program to calculate the sum of squares of numbers from 1 to 5 using a while loop.
• Sum of Even Numbers:
Calculate the sum of even numbers from 1 to 100 using a while loop.
5. Display Characters:
Given a string, display each character individually using a while loop.