0% found this document useful (0 votes)
33 views44 pages

Zahra Lab Report 2023

Uploaded by

mahambilal020
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)
33 views44 pages

Zahra Lab Report 2023

Uploaded by

mahambilal020
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/ 44

Lab no Description Page no

Lab.1 Introduction to IDE Page no 4


TASK 1: Installation of Dev C++
TASK 1: Understanding the program structure of C++ and
writing the first program.
Lab.2 Understanding of datatypes variables and constants Page no 7
TASK 1: Determine size of different data types
TASK 2: Practice of variable and constants
Lab .3 Types of Operator Page no 8
TASK 1: Write a program in which all the arithmetic operators
are used.
TASK 2: Write a program in which all the comparison
operators are used.
Lab.4 Pseudocode and flowchart Page no 13
TASK 1: : Write a pseudocode and flowchart to determine a
student’s final grade and indicate whether it is passing or
failing based on the 50% marks. The final grade is calculated as
the average of four marks.
TASK 2: Write pseudocode that takes input of three random
numbers and writes them all in ascending order.
TASK 3: Write a pseudocode and flowchart that calculate the
age of citizens and separate young and old citizens
Lab.5 If-else statements Page no 17
Task 1: Write a program to print pass and fail students baed on
average.
TASK 2: Write a program to check Even and odd Numbers.
TASK 3: Write a program to check vowels and consonants
TASK 4: Write a program to calculate grades of students.
TASK 5: Write a program to count the total number of
currency notes of 5000,1000,100,50,20, and 10 in a given
amount.
TASK 6: Write a program to input electricity unit consumed
and calculate total electricity bill according to the given
condition:
Page | 1
If number of units are up to 100, the bill will be charged at the
rate of Rs. 5 per unit. If number of units are up to 200, the bill
will be charged at the rate of Rs. 10 per unit. If number of
units are above 200, the bill will be charged at the rate of Rs.
20 per unit.
TASK 7: Write a program that take three numbers as input
from the user and finds maximum umber among them.
Lab .6 Switch Statements Page no 26
TASK 1: Write a program to develop a simple calculator. The
program should take inputs of two integers, select the
operator (+, - ,*, /) using switch statement and display the
output after performing the desired calculation.
Task 2: Write a program to check gpa of students based on
grades.
Lab.7 Nested if-else statement Page no 29
TASK 1: Write a program to check username and password.
TASK 2: Write a program to design ATM interface using nested
ifelse statements to perform the following transactions.
Initialize the balance with zero and perform the cash deposit
and withdrawal
transactions accordingly. After each transaction, the program
should ask the user to perform any other transaction or not.
Based on the user input (y/n) the program either redirects to
the main interface or exit.
• Press B to check account Balance
• Press D to cash Deposit
• Press W to cash Withdraw

Lab .8 Loops (while, do-while ,for) Page no 32


TASK 1: Write a program that displays number from 1 to 10
using while loop. TASK 2: Write the program of Guess the
number game.
TASK 3: Write a program which asks user to enter a positive
integer and then calculate the factorial of that number.
TASK 4: Write a program which works like a year calendar
TASK 5: Write a program which takes username and password
three times if incorrect using loop.
Task 6: Write a program that takes input from user and prints
the table.
Lab.9 Arrays Page no 39
TASK 1: Write a program which takes an array as an input from

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.

• Click on save button to save . By default , it is saved in “Downloads “ folder .


● After the download completes , go the saved .exe file and click on it to run.

● 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.

Step 6- In this step ,

● You can see different components of Dev C++ will be installed with this package.

● Click on “next” button.


Page | 5
Step 7- In this step,

● By default, the destination folder is in C drive. You are free to change this destination folder but make sure you
have enough memory.

● Click on “install button” .

In the next screen , installation begins

● 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>

Using namespace std;

Int main()

cout<<”Lets start programming”;

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

Practice of variables and constants


const int a = 76; // becomes integer constant variable
const char r = ‘e’; //becomes character constant variable
const string w = “doll4082”; //becomes string constant variable
const float s = 44.4 //becomes float constant variable

LAB NO 3
TYPES OF OPERATORS
Introduction to arithmetic operators

Operator Name description Example

+ Addition Add two values x+y

- Subtraction Subtract two values from another x-y

* Multiplication Multiply two values x*y

/ Division Divide two values x/y

% Modulus Retrns the division remainder x%y

++ Increment Increases the value of variable by x++


1

__ Decrement Decreases the value of variable x--


by 1

Introduction to relational operators

Operator Name Example

>= Greater than equals to x>=y

Page | 8
<= Less than equals to x<=y

> Greater than x>y

< Less than x<y

== Equal to x==y

!= Not equal to x!=y

Introduction to logical operators


Operator Name Example

&& AND operator x>y && x<y

|| OR operator x<y || x>y

! NOT operator !(x>y)

TASK NO 1 : Write a program using arithmetic operators

#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:

HOW ABOVE PROGRAM WORKS:


(1) You start by including the necessary C++ library headers.

(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’.

TASK 2 : Program to perform relational operations


Page | 10
#include<iostream>
Using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
int a=3;
int b=4;
bool result;
result = (a==b);
cout<<”3==4 is “ << boolalpha << result << endl;
result = (a!=b);
cout<<”3!=4 is” <<boolalpha << result << endl;
result = (a>b);
cout<<”3>4 is” <<boolalpha << result <<endl;
result = (a<b);
cout<<”3<4 is” <<boolalpha << result << endl;
}
Compiling program:

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’.

• “3==4 is” is astring that is printed first.

• ‘boolalpha’ is used to format the boolean output as “true” or “false” rather than numeric ‘1’ or ‘0’.

• ‘result’ holds the boolean value.

• ‘false’ ,which is then printed.

• ‘endl’ inserts a newline character to start a new line.

(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.

TASK 3: Program to check the size of data type


#include<iostream>
using namespace std;
int main()
{
cout<<”This program is run and compiled by Zahra”<<endl;
cout<<”Roll no: bsf23005097”<<endl;
cout<< “size of char:” << sizeof(char) <<”byte”<< endl;
cout<<”size of int:” << sizeof(int) <<”byte”<< endl;

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:

HOW ABOVE PROGRAM WORKS:


(1) ‘#include<iostream>’ : This line includes the input/output stream library,allowing you to use input and output
functions like ‘cout’ and ‘endl’.

(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

Print pass Print fail

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

Input birth year

Input current year

Age = current year – birth


year

yes if age > 60 no

senior citizen young citizen

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:

Task no 2: write a program to print even numbers.

#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:

Task no 3: write a program to print vowel letters.

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

cout<<”Roll no: bsf23005097”<<endl;


char grade;
int marks;
cout<<”Enter marks “;
cin>>marks;
if(marks >= 91 && marks <=100)
cout<<”A”;
else if(marks >=81)
cout<<”B”;
else if(marks >=71)
cout<<”C”;
else if(marks >=61)
cout<<”D”;
else if(marks >=51)
cout<<”E”;
else
cout<<”F”;
}
Compiled program:

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:

Task no 2: Write a program of guess number game between 1 to 100.

#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:

Task no 4: Write a program which works like a calendar year.

#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

You might also like