Zahra Lab Report 2023
Zahra Lab Report 2023
Page | 2
the user and find the maximum number.
TASK 2: Write a program which takes an array as an input from
the user and also calculate the sum and average of all the
array elements.
Page | 3
LAB NO 1
Introduction to IDE
Installation of IDE:
Step 1- First you must download the DEV C++ on your windows machine . Visit to Download DEV C++:
http://www.bloodshed.net/
Step 2- There are packages for different Operating Systems.
Step 3- Under package DEV C++ 5.0(4.9.9.2) with Mingw/GCC 3.4.2 compiler and GDB 5.2.1 debugger (9.0 MB) Click on the link
“Download from SourceForge”.
Step 4- This package will download C++ .exe file for Windows that can be used to install on Windows 7/8/XP/Vista/10
Page | 4
Step 5- You will direct to SourceForge website , your C++ download will start automatically.
● The installer will ask you a language to select . Select “English” and click on “OK” .
● The screen for liense agreement will appear. Click on “I agree” to proceed further.
● You can see different components of Dev C++ will be installed with this package.
● By default, the destination folder is in C drive. You are free to change this destination folder but make sure you
have enough memory.
● Now ,Dev C++ is installed successfully on your windows .select “Run Dev C++ “ to run it .Click on “finish” button .
Page | 6
Understanding C++ program structure
1- Libraries
2- Main function
{
body;
}
SIMPLE PROGRAM
#include<iostream>
Int main()
LAB NO 2
Understanding of Datatypes, Variables and Constants
Data type Bit size
Character 1 byte
Page | 7
Integer 4 bytes
Float 4 bytes
Double 8 bytes
Boolean 1 byte
String 8 bytes
Long 4 bytes
long double 16 bytes
LAB NO 3
TYPES OF OPERATORS
Introduction to arithmetic operators
Page | 8
<= Less than equals to x<=y
== Equal to x==y
#include<iostream>
Using namespace std;
in main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int a=7;
int b=2;
cout<< ”a+b=” << a+b << endl;
cout<< ”a-b=” << a-b << endl;
cout<< ”a*b=” << a*b <<endl;
cout<< ”a/b=” << a/b <<endl;
cout<< “a%b ”<< a%b <<endl;
}
Compiling program:
Page | 9
OUTPUT:
(2) Inside the ‘main ()’ function,you declare two integer variables,’a’ and ‘b’,and initialize them with values 7 and
2,respectively.
(3) Then,you use the ‘cout’ stream to display the results of various arithmetic operations:
• ‘cout<<“sum of a+b=”<<a+b <<endl;’ calculates the sum of ‘a’ and ‘b’ (7+2) and prints “sum of a+b=9” followed
by a newline.
• ‘cout<<”subtraction of a-b=”<<a-b<<endl;’ calculates the difference of ‘a’ and ‘b’ (7-2) and prints “subtraction
of a-b=5” followed by a newline.
• ‘cout<<”multiplication of a*b=”<<a*b<<endl;’ calculates the product of ‘a’ and ’b’ (7*2) and prints
“multiplication of a*b=14” followed by a newline.
• ‘cout<<”division of a/b=”<<a/b<<endl;’ calculates the division of ’a’ and ‘b’ (7/2) and prints “division of a/b=3"
followed by a newline.Note that because both ‘a’ and ‘b’ are integers ,the result is also an integer ,and any decimal part is
truncated.
• ‘cout<<”modulo of a%b=”<<a%b<<endl;’ calculates the modulus of ‘a’ and ‘b’ (7%2) and prints “division of a
%b=1” followed by a newline.
(4) Finally the program terminates.So, this C++program calculates and displays the results of basic arithmetic operations on the
values of ‘a’ and ‘b’.
OUTPUT:
Page | 11
HOW ABOVE PROGRAM WORKS:
(1) ‘#include<iostream>’: This line includes the standard input and output stream library,allowing you to use input and
output operations.
(2) ‘int main()’: This is the strarting point of C++ program. ’main’ is the function that gets executed when you run the
program.
(3) ‘int a=3;’ and ‘int b=4;’ : These lines declare two integer variables ‘a’ and ’b’ and initialize them wit the values 3 and
4.
(4) ‘bool result;’ : This line declares a boolean variable named ‘result’ without initializing it.
(5) ‘result =(a==b); ‘ :This line compares the value of ‘a’ and ‘b’ using the ‘==’ operator. The result of this comparison is
stored in the result variable.
(6) ‘cout<<”3==4 is”<<boolalpha<<result<<endl; ‘ : This line prints the result of comparison between ‘a’ and’b’.
• ‘boolalpha’ is used to format the boolean output as “true” or “false” rather than numeric ‘1’ or ‘0’.
(7) Similar comparisons are made for ‘!=’ , ‘>’ and ‘<’ between ‘a’ and ‘b’, and their results are printed in the same line.
(8) The outputs represent the results of the comparisons between the values of ‘a’ and ‘b’ using the different comparison
operators.
Page | 12
cout<<”size of short:” << sizeof(short) <<”byte”<< endl;
cout<<”size of float:” << sizeof(float) <<”byte”<< endl;
cout<<”size of double:” << sizeof(double) <<”byte”<< endl;
cout<<”size of bool:” << sizeof(bool) <<”byte”<< endl;
cout<<”size of string:” << sizeof(string) <<”byte”<< endl;
}
Compiling program:
OUTPUT:
(2) ‘int main’ : This is the main function where program execution starts.
(3) Inside the ‘main’ function ,there are several lines that use the ‘cout’ object to displays information:
(4) ‘cout<<”size of char”<<sizeof(char)<<”byte”<<endl’ : This line prints the size of a ‘char’ variable in bytes. It
uses the ‘size of’ operator to determine the size of ‘char’ and then displays it along with the text.
(5) Similar lines follow for ‘int’ , ‘short’ , ‘float’ , ‘double’ , ‘bool’ and ‘string’ data types, displaying their respective
sizes in bytes.
(6) ‘endl’ : This is used to insert a newline character , effectively moving the outtput to the next line.
(7) The program ends with a closing brace ‘}’ for the ‘main’ function.
Page | 13
LAB NO 4
Pseudocode and flowchart
Task no 1: Write a pseudoode and flowchart to find average of students and print
whether it is passing or failing based on 50% marks.
1- Start
2- Input marks of subject 1
3- Input marks of subject 2
4- Input marks of subject 3
5- Input marks of subject 4
6- Calculate average
Average=M1 + M2 + M3 / 3
7- If average < 50 print fail
8- If average >=50 print pass
9- End
Start
Input marks of
Subject 1,2,3and 4
M 1+ M 2+ M 3+ M 4
Average=
4
True False
If average>=50
End
Page | 14
Task no 2: Write a pseudocode and flowchart that takes input of three random numbers
write them in ascending order.
1- Start
2- Input num 1
Input num 2
Input num 3
3- If (num 1 < num 2) and (num 2 < num 3)
4- Print num 1, num 2 and num 3
5- If (num 1 < num 3) and (num 3 < num 2)
6- Print num 1, num 3 and num 2
7- If (num 2 < num 3) and (num 3 < num 1)
8- Print num 2, num 3 and num 1
9- If (num 2 < num 1) and (num 1< num 3)
10-Print num 2, num 1 and num 3
11-If (num 3 < num 1) and (num 1 < num 2)
12- Print num 3, num 1 and num 2
13- If (num 3 < num 2) and (num 2 < num 1)
14- Print num 3, num 2 and num 1
15- End
Task no 3: Write a flowchart to calculate birth year and print senior and young citizen on
based of 60 years.
1-Start
2-Input birth year
3-Input current year
4-age=current year – birth year
5-if age > 60
6-Print senior citizen
7-if age < 60
Page | 15
8-Print young citizen
9-End
Start
End
LAB NO 5
if else statements
Task no 1: write a program based on average print pass and fail students.
#include<iostream>
Page | 16
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int avg;
cout<<”enter average” ;
cin>>avg;
if(avg >=50)
cout<<”Pass”;
else
cout<<”fail” ;
}
Compiled program :
OUTPUT:
#include<iostream>
Page | 17
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int x;
cout<<”Enter number”;
cin>>x;
if(x%2 == 0)
cout<<”Even”;
else
cout<<”Odd”;
}
Compiled program:
OUTPUT:
#include<iostream>
using namespace std;
int main()
Page | 18
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
char c;
cout<<”Enter a character” ;
cin>>c;
if( c==’a’ || c==’e’ || c==’i’ || c==’o’ || c==’u’ || c==’A’ || c==’E’ || c==’I’ || c==’O’ || c==’U’ )
cout<<” It is a vowel” ;
else
cout<<”it is a consonant ”;
}
Compiled program:
OUTPUT:
Task no 4: Write a program that takes students numerical grade and prints letter grade
using if else statement.
#include<iostream>
using namespace std;
int main()
{
Page | 19
cout<<”This program is run and compiled by Zahra”<<endl;
Page | 20
OUTPUT:
Task no 5: Write a program to count total number of currency notes of 5000, 1000, 500,
100, 50, 20 and 10 in a given amount.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int amount;
int note10, note20, note50, note100, note500, note1000, note5000;
note10=note20=note50=note100=note500=note1000=note5000=0;
cout<<"Enter amount ";
cin>>amount;
Page | 21
if(amount >= 5000)
{
note5000=amount/5000;
amount-=note5000*5000;
}
if(amount >= 1000)
{
note1000=amount/1000;
amount-=note1000*1000;
}
if(amount >= 500)
{
note500=amount/500;
amount-=note500*500;
}
if(amount >= 100)
{
note100=amount/100;
amount-=note100*100;
}
if(amount >= 50)
{
note50=amount/50;
amount-=note50*50;
}
if(amount >=20)
{
note20=amount/20;
amount-=note20*20;
}
if(amount >= 10)
Page | 22
{
note10=amount;
}
cout<<"total number of notes "<<endl;
cout<<"note5000 = "<<note5000<<endl;
cout<<"note1000 = "<<note1000<<endl;
cout<<"note500 = "<<note500<<endl;
cout<<"note100 = "<<note100<<endl;
cout<<"note50 = "<<note50<<endl;
cout<<"note20 = "<<note20<<endl;
cout<<"note10 = "<<note10<<endl;
}
Compiled program:
OUTPUT:
Page | 23
Task no 6: Write a program to input electricity unit consumed and calculate total electricity
bill.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int unit;
int bill;
cout<<"Enter units ";
cin>>unit;
if(unit <= 100)
{
bill=unit*5;
cout<<"electricity bill is "<<bill;
}
else if(unit <= 200)
{
bill=unit*10;
cout<<"electricity bill is "<<bill;
}
else
Page | 24
{
bill=unit*20;
cout<<"electricity bill is "<<bill;
}}
Compiled program:
OUTPUT:
Task no 7 : Write a program that takes three numbers from user and find the maximum
number among them.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int a,b,c;
cout<<”Enter first number : ”;
cin>>a;
cout<<”Enter second number : ”;
Page | 25
cin>>b;
cout<<”Enter third number : ”;
cin>>c;
if(a>b && a>c)
cout<<”Maximum number is ”<<a;
else if(b>a && b>c)
cout<<”Maximum number is ”<<b;
else
cout<<”Maximum number is ”<<c;
}
Compiled program:
OUTPUT:
LAB NO 6
Switch statement
Task no 1: write a program in switch statement to make calculator.
#include<iostream>
Page | 26
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int n1, n2;
char op;
cout<<"Enter first value ";
cin>>n1;
cout<<"Enter second value ";
cin>>n2;
cout<<"Enter operator ";
cin>>op;
switch(op)
{
case'+' :
cout<<"sum : "<<n1+n2;
break;
case'-' :
cout<<"subtraction : "<<n1-n2;
break;
case'*' :
cout<<"multiplication : "<<n1*n2;
break;
case'/' :
cout<<"division : "<<n1/n2;
break;
case'%' :
cout<<"modulus : "<<n1%n2;
break;
default:
Page | 27
cout<<"invalid operator";
}
}
Compiled program:
OUTPUT:
Task no 2: Write a program to check the GPA of students based on grade using switch
statement.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
Page | 28
cout<<”Roll no: bsf23005097”<<endl;
char grade;
cout<<”Enter the grade : “;
cin>>grade;
switch(grade)
{
case ‘A’ :
cout<<”Your GPA is 4.0”;
break;
case ‘B’ :
cout<<”Your GPA is 3.0”;
break;
case ‘C’ :
cout<<”Your GPA is 2.0” ;
break;
case ‘D’ :
cout<<”Your GPA is 1.0” ;
break;
default:
cout<<”Invalid grade”;
}}
Compiled program:
Page | 29
OUTPUT:
LAB NO 7
Nested if else
Task no 1: Write a program to check username and password.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
string username,password;
cout<<”Enter username : “<<endl;
cin>>username;
Page | 30
cout<<”Enter password : “<<endl;
cin>>password;
if(username == “doll123”)
{
if(password == “kjl67”)
cout<<”Login successful”<<endl;
else
cout<<”Incorrect password”<<endl;
}
else
cout<<”Incorrect username”<<endl;
}
Compiled program:
Output:
Page | 31
Task no 2: Write a program to design ATM interference using if else.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
char option;
int choice;
float balance=70000;
do
{
cout<<”Press B to check balance account”<<endl;
cout<<”Press D to cash deposit”<<endl;
cout<<”Press W to cash withdrawal”<<endl;
cin>>option;
if(option == ‘B’ || option == ‘b’){
cout<<”Your account balance is : “<<balance<<endl; }
else if(option == ‘D’ || option == ‘d’){
float deposit;
cout<<”Enter the deposit amount : ”;
cin>>deposit;
balance+=deposit;
cout<<”Deposit of ”<<deposit<<”successful.your updated balance is: ”<<balance<<endl;}
else if(option == ‘W’ || option == ‘w’){
float withdrawal;
cout<<”Enter the withdrawal amount: ”;
cin>>withdrawal;
if(withdrawal>balance){
cout<<”Insufficient balance . withdrawal failed”;}
else {
Page | 32
balance-=withdrawal;
cout<<”Withdrawal of ”<<withdrawal<<”successful. Your updated balance is: ”<<balance<<endl;}
}
else{
cout<<”Invalid option. Please try again”<<endl;}
char choice;
cout<<”Do you want to perform another transaction? (y/n) ;
cin>>choice;
if(choice != ‘Y’ || choice != ‘y’)
cout<<”Thank you for using ATM. Have a great day !”<<endl;
break;
}while(choice == ‘y’ || choice == ‘Y’);
}
Compiled program:
OUTPUT:
Page | 33
LAB NO 8
Loops(while, do-while, for)
Task no 1: Write a program that prints number 1 to 10 using while loop.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int i;
i=1;
while(i<=10)
{
cout<<i<<endl;
i++;
}}
Compiled program:
Page | 34
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int num=24;
int guess;
do
{
cout<<”Enter the number between 1 and 100 : ”;
cin>>guess;
if(guess==num)
cout<<”Correct”;
else if(guess<num)
cout<<”Number is lesser”<<endl;
else
cout<<”Number is greator”<<endl;
}while(guess!=num);
}
Compiled program:
Page | 35
OUTPUT:
Task no 3: Write a program that enters a positive integer from user and calculate its
factorial.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int n;
int factorial=1.0;
cout<<”Enter a positive integer : “;
cin>>n;
if(n<0)
Page | 36
cout<<”Error! Please enter a positive integer”;
else
{
for(int i=1;i<=n;i++)
factorial*=i;
}
cout<<”Factorial of “<<n<<”is “<<factorial;}
Compiling program:
OUTPUT:
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
for(int i=1;i<=12;i++)
Page | 37
{
cout<<”Month : “<<i<<endl;
for(int j=1;j<=4;j++)
{
cout<<”Week : “<<j<<endl;
for(int k=1;k<=7;k++)
{
cout<<”day”<<k<<” “;
}
cout<<endl;}}
}
Compiled program:
Output:
Page | 38
Task no 5: Write a program which takes username and password if incorrect three times
using loop.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
string username,password;
for(int attempt=1;attempt<=3;attempt++)
{
cout<<”Enter username “<<endl;
cin>>username;
cout<<”Enter password “<<endl;
cin>>password;
if(username != “doll123” && password != “kjl67”)
cout<<”Incorrect username and password”<<endl;
else if(username != “doll123” && password == “kjl67”)
cout<<”Incorrect username and correct password”<<endl;
else if(username != “doll123” && password != “kjl67”)
cout<<”correct username and incorrect password”<<endl;
else
break;
}
cout<<”Access granted”<<endl;
}
Compiled program:
Page | 39
Output:
Task no 6: Write a program that takes input from user and print the table.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int j,n;
cout<<”Enter table of : “;
Page | 40
cin>>j;
cout<<”Enter table upto ”;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<j<<” x “<<i<<” = “<<j*i<<endl;
}}
Comiling program:
OUTPUT:
LAB NO 9
ARRAYS:
How to declare a simple array in c++ :
int arr[4]={2,4,6,7};
Task no 1: Write a program that takes an array as an input and find the maximum number.
#include<iostream>
using namespace std;
int main()
Page | 41
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int arr[4];
cout<<”Enter numbers”<<endl;
for(i=0;i<4;i++)
{
cin>>arr[i];
}
int max=arr[0];
for(int i=0;i<4;i++)
{
if(arr[i] >max)
max=arr[i];
}
cout<<”Maximum number is : “ <<max;
}
Compiling program:
Output:
Page | 42
Task no 2: Write a program which takes an array as an input and also calculate sum and
average of all array elements.
#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int arr[4],sum=0,i;
cout<<”Enter numbers”<<endl;
for(i=0;i<4;i++)
{
cin>>arr[i]
}
for(i=0;i<4;i++)
{
sum=sum+arr[i];
}
cout<<”sum : “<<sum<<endl;
cout<<”average : “<<sum/i<<endl;
}
Compiled program:
Page | 43
Output:
___________________________________________________________________________
Page | 44