You are on page 1of 25

CSL-113: Computer Programming Lab

Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#1
Write a C++ Program that read a float (amount) and an int (num) input from user. Add
both (amount and num) numbers and store them in two different int (a) and float (b)
variables. Display the outputs of a and b variables. Also if outputs are greater than 16
print “value of a & b > 16” else print “value of a & b < 16”.

Source code

#include<iostream>
using namespace std;

int main()
{ float
amount,add,a,b;
int num;
cout<<"please enter amount :";
cin>>amount;

cout<<"\nplease enter num :";


cin>>num;

add=amount + num;

a==add;
b==add;

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout<<"\nvalue of a is :"<<a<<endl;
cout<<"\nvalue of b is :"<<b<<endl;

if (a>16 && b>16)

{
cout<<"\nvalue of a & b is > 16";
}
else
{
cout<<"\nvalue of a & b is < 16";
}

return 0;

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#2
Write a C++ Program that read three input from user and returns the
smallest one.

Source code

#include <iostream>
using namespace std;

int main()
{
float n1, n2, n3;

cout << "Enter three numbers :\n";


cin >> n1 >> n2 >> n3;

if(n1 < n2 && n1 < n3)


{
cout << "smallest number: " << n1;
}

if(n2 < n1 && n2 < n3)


{
cout << "smallest number: " << n2;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

if(n3 < n1 && n3 < n2) {


cout << "smallest number: " << n3;
}

return 0;
}

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#3
Write a C++ Program that read an alphabet (e.g. a,b,c,d,…..z) and display
whetherthe input alphabet is a vowel (i.e. a, e, i, o, u) or consonant.

Source code
#include <iostream> using
namespace std;

int main()
{ char
c;
int LowercaseVowel, UppercaseVowel;
cout << "Enter an alphabet: "; cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
LowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
UppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel
is true

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

if (LowercaseVowel || UppercaseVowel)
cout << c << " is a vowel.";

else
cout << c << " is a consonant.";

return 0;
}

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#4:
Write a C++ Program that read a number from user .Test the user entered
number whether it is EVEN or ODD. Print the message that number is EVEN
or ODD.

Source code

#include <iostream>

using namespace std;


int main()
{ int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0) cout << n
<< " is even."; else cout <<
n << " is odd.";
return 0;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#5
Write a C++ Program that read an integer input in between (1 to 12) and store it
month_of_year. Print the corresponding month of year. Use else if statement.
Eample: Input is 4… Print “April”

Source code
#include <iostream> using
namespace std;

int main()
{

int month_of_year;
cout<<"Enter a number from 1-12 : ";
cin>>month_of_year; if
(month_of_year ==1)
cout<<"January"; else if
(month_of_year==2) cout<<
"February"; else if
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

(month_of_year==3)
cout<<"March"; else if
(month_of_year==4)
cout<<"April";

else if (month_of_year==5)
cout<<"May"; else if
(month_of_year==6)
cout<<"June"; else if
(month_of_year==7)
cout<<"July"; else if
(month_of_year==8)
cout<<"August"; else if
(month_of_year==9)
cout<<"September"; else if
(month_of_year==10)
cout<<"October"; else if
(month_of_year==11)
cout<<"November"; else if
(month_of_year==12)
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout<<"December"; else if
(month_of_year>12)
cout<<"Sorry I need a number from 1-12."<<endl; else
if(month_of_year<=12)
cout<< "month of year "<<month_of_year;

return 0;
}

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#6
Rewrite exercise 5 using switch statement.
Source code
#include <iostream>

using namespace std;

int main (){ int month_of_year;


cout<<"please Enter integer : ";
cin>>month_of_year;

switch(month_of_year)
{ case 1:
cout<<"\nthe corresponding month of year is Jan"<<endl;
break; case 2:
cout<<"\nthe corresponding month of year is Feb"<<endl;
break; case 3:
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout<<"\nthe corresponding month of year is Mar"<<endl;


break; case 4:
cout<<"\nthe corresponding month of year is Apr"<<endl;
break; case 5:
cout<<"\nthe corresponding month of year is May"<<endl;
break; case 6:
cout<<"\nthe corresponding month of year is Jun"<<endl;
break; case 7:
cout<<"\nthe corresponding month of year is Jul"<<endl;
break; case 8:
cout<<"\nthe corresponding month of year is Aug"<<endl;
break; case 9:
cout<<"\nthe corresponding month of year is Sep"<<endl;
break; case 10:
cout<<"\nthe corresponding month of year is Oct"<<endl;
break; case 11:
cout<<"\nthe corresponding month of year is Nov"<<endl;
break; case 12:

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout<<"\nthe corresponding month of year is Dec"<<endl;


break;

default: // default is for when you enter a number out of 1-12 range.
for instance, 13
cout<<"\ninvalid input!"<<endl;
}

return (0);
}
Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#7
Write a C++ Menu driven program that allows a user to enter 3 numbers
and then
choose between findings the smallest, largest, sum or average. Use else if
statement to determine what action to take.
Source code
#include<iostream>
using namespace std;

int main()
{
int num1=0,num2=0,num3=0,choice=0;
int sum=0,avg=0,largest,smallest;

cout << "\t\t\t''MENU''\n"<<endl;


cout <<"1. Find Smallest of 3"<<endl;
cout <<"2. Find Largest of 3"<<endl;
cout <<"3. Find Sum of 3"<<endl; cout
<<"4. Find Average of 3"<<endl;
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout <<"\nchose any of the given serial numbers :";


cin >> choice;

cout <<"\nEnter 3 numbers :\n\n";


cin>>num1>>num2>>num3;

if(choice==1)
{
smallest=num1;

if(num2 < smallest )


{ // compare num2 to smallest,
smallest = num2;
}
else if(num3 < smallest )
{
smallest = num3;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout <<"Smallest of all integers: " <<smallest<<endl;

return 0;
}
else if(choice==2)
{
largest=num1;

if(num2 > largest )


{
largest = num2;
}
else if(num3 > largest )
{
largest = num3;
}

cout <<"Largest of all integers: " <<largest<<endl;


Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

}
else if(choice==3)
{
sum = ( num1 + num2 + num3 );
cout <<"Sum of the given numbers: "<<sum<<endl;
}
else if(choice==4)
{
avg = ( ( num1 + num2 + num3 ) / 3 );
cout <<"Average of the given numbers: "<<avg<<endl;
}
else if(choice>4)
{
cout<<" you have entered wrong number ";
}
return 0;
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

Task#8
Rewrite exercise 7 using switch statement
Source code
#include<iostream> using
namespace std;

int main()
{
int num1=0,num2=0,num3=0,choice=0;
int sum=0,avg=0,largest,smallest;

cout << "\t\t\t''MENU''\n"<<endl; cout <<"1.


Find Smallest of 3"<<endl; cout <<"2. Find Largest
of 3"<<endl; cout <<"3. Find Sum of 3"<<endl;
cout <<"4. Find Average of 3"<<endl; cout
<<"\nchose any of the given serial numbers :"; cin
>> choice;
cout <<"\nEnter 3 numbers :\n\n";
cin>>num1 >> num2>> num3; switch(choice)
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

{
//case 1 start
case 1 :choice==1;

smallest=num1;

if(num2 < smallest )


{ // compare num2 to smallest,
smallest = num2;
}
else if(num3 < smallest )
{
smallest = num3;
}
cout <<"\nSmallest of all integers: " <<smallest<<endl;
break;

}
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

//case 2 start
case 2 :choice==2;

{
largest=num1;

if(num2 > largest )


largest = num2; else
(num3 > largest );
largest = num3;

cout <<"\nLargest of all integers: " <<largest<<endl;


break;
}
//case 3 start
case 3 :choice==3;

{
sum = ( num1 + num2 + num3 );
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

cout <<"\nSum of the given numbers: "<<sum<<endl;


break;
}
//case 4 start
case 4 :choice==4;

{
avg = ( ( num1 + num2 + num3 ) / 3 );
cout <<"\nAverage of the given numbers: "<<avg<<endl;
break;

case 5 :choice>4;

cout<<"\nyou have entered wrong number ";


break;
}

}
return 0;
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement
CSL-113: Computer Programming Lab
Semester BS CS – 1A
Name : Muhammad Zain
Enroll no :(02-134182-037)

}
Snapshot

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 04: Conditional Statements (if-else, Nested, switch statement

You might also like