[go: up one dir, main page]

0% found this document useful (0 votes)
19 views12 pages

Lab Manual 06 - Increment Decrement Operator & While Loop

The lab manual covers programming fundamentals focusing on increment/decrement operators and while loops, with examples and coding exercises. It includes coding examples for pre and post increment/decrement operations, as well as various applications of while loops such as calculating sums, factorials, and displaying even/odd numbers. Additionally, it provides practice questions for students to enhance their understanding of these concepts.

Uploaded by

rajabfan555
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)
19 views12 pages

Lab Manual 06 - Increment Decrement Operator & While Loop

The lab manual covers programming fundamentals focusing on increment/decrement operators and while loops, with examples and coding exercises. It includes coding examples for pre and post increment/decrement operations, as well as various applications of while loops such as calculating sums, factorials, and displaying even/odd numbers. Additionally, it provides practice questions for students to enhance their understanding of these concepts.

Uploaded by

rajabfan555
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/ 12

Lab Manual: Programming Fundamentals

University of Management and Technology,


Lahore Campus

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

Identify key programming


Areas Problem Understanding structures such as Design/ Development of Solutions
Assessed variables, loops, and
functions.
(CLO 1) (CLO 2) (CLO 3)
No able to identify key
Not able to understand Not able to Model a solution for a
Poor programming structures such
Problem given problem
as variables, loops, and
functions.

Partially able identify Modeled an average solution


Able to partially understand
Average key programming for a given problem using
the problem
structures such as programming principles
variables, loops, and
functions.

Thoroughly identify key


Modeled a proper solution for a
Very Good Fully Understand the problem programming structures
given problem using programming
such as variables, loops,
principles
and functions.

Department of Computer Science, UMT, Lahore. 1 Awais Amin


Lab Manual: Programming Fundamentals

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

Department of Computer Science, UMT, Lahore. 2 Awais Amin


Lab Manual: Programming Fundamentals

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

Department of Computer Science, UMT, Lahore. 3 Awais Amin


Lab Manual: Programming Fundamentals

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;

Department of Computer Science, UMT, Lahore. 4 Awais Amin


Lab Manual: Programming Fundamentals
return 0;
}
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;
int maximum=10;
int num=minimum;
cout<<"Number number squared \n ";
while(num<=maximum)

Department of Computer Science, UMT, Lahore. 5 Awais Amin


Lab Manual: Programming Fundamentals
{
cout<<num<<"\t\t"<<num*num<<endl;
num++;
}
return 0;
}
Output:

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

Department of Computer Science, UMT, Lahore. 6 Awais Amin


Lab Manual: Programming Fundamentals
#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;
}

Eample 6 : Countdown from 10 to 1


#include <iostream>
using namespace std;

int main() {
int i = 10;
while (i > 0) {
cout << i << endl;
i--;
}
cout << "Liftoff!" << endl;
return 0;
}

Department of Computer Science, UMT, Lahore. 7 Awais Amin


Lab Manual: Programming Fundamentals

Example 7 : Guessing Game


#include <iostream>
using namespace std;

int main() {
int secret_number = 7; // Fixed secret number
int guess;

cout << "Guess the number between 1 and 10: ";


cin >> guess;

while (guess != secret_number) {


cout << "Wrong! Try again: ";
cin >> guess;
}

cout << "Correct! You guessed the number." << endl;


return 0;
}

Exampke 8: WAP for Multiplication Table of a Given Number


#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number to print its multiplication table: ";
cin >> num;

int i = 1;

Department of Computer Science, UMT, Lahore. 8 Awais Amin


Lab Manual: Programming Fundamentals
while (i <= 10) {
cout << num << " x " << i << " = " << num * i << endl;
i++;
}
return 0;
}
Example 8: 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
{
cout<<"\nOdd numbers:";
cout<<"\n"<<i;
odd++;
}
i++;
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTOtal odd numbers:"<<odd;
return 0;
}

OR

Department of Computer Science, UMT, Lahore. 9 Awais Amin


Lab Manual: Programming Fundamentals

#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()
{

Department of Computer Science, UMT, Lahore. 10 Awais Amin


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

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.

Department of Computer Science, UMT, Lahore. 11 Awais Amin


Lab Manual: Programming Fundamentals
3. Character Display:
Write a program that takes a string input from the user and displays each character separately
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.

Department of Computer Science, UMT, Lahore. 12 Awais Amin

You might also like