Professional Documents
Culture Documents
Task#1: CSL-113: Computer Programming Lab Semester BS CS - 1A Name: Muhammad Zain Enroll No: (02-134182-037)
Task#1: CSL-113: Computer Programming Lab Semester BS CS - 1A Name: Muhammad Zain Enroll No: (02-134182-037)
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;
add=amount + num;
a==add;
b==add;
cout<<"\nvalue of a is :"<<a<<endl;
cout<<"\nvalue of b is :"<<b<<endl;
{
cout<<"\nvalue of a & b is > 16";
}
else
{
cout<<"\nvalue of a & b is < 16";
}
return 0;
Snapshot
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;
return 0;
}
Snapshot
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
if (LowercaseVowel || UppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
Snapshot
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>
Snapshot
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
Task#6
Rewrite exercise 5 using switch statement.
Source code
#include <iostream>
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)
default: // default is for when you enter a number out of 1-12 range.
for instance, 13
cout<<"\ninvalid input!"<<endl;
}
return (0);
}
Snapshot
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;
if(choice==1)
{
smallest=num1;
return 0;
}
else if(choice==2)
{
largest=num1;
}
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;
}
Snapshot
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;
{
//case 1 start
case 1 :choice==1;
smallest=num1;
}
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;
{
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)
{
avg = ( ( num1 + num2 + num3 ) / 3 );
cout <<"\nAverage of the given numbers: "<<avg<<endl;
break;
case 5 :choice>4;
}
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