You are on page 1of 18

Computer fundamentals and

Programming
LAB REPORT
Electrical Engineering (section-B)
Registration No: BSEE-12637-2020
Submitted by:
Muhammad Huzaifa Zahid
Submitted to:
Dr.Muhammad Tufail
TASK 1:
Write a C++ program to calculate a bike’s average consumption from the given total distance (integer
value) traveled (in km) and spent fuel (in liters, float number – 2 decimal points).

Coding:
#include<iostream>
using namespace std;
int main()
{
system("color 70");

int distance;
float liter;
float avgconsumption;
cout << "enter the distance";

cin >> distance;


cout << "enter the total fuel spent in liters ";
cin >> liter;
avgconsumption = distance / liter;

cout << "average consumption= " << avgconsumption;


getchar();
return 0;
}

Output:
TASK 2:
2. Write a C++ program that reads an integer and check the specified range where it belongs
using if-else-if. Print an error message if the number is negative and greater than 80.

Coding:
#include<iostream>
using namespace std;

int main()
{
system("color 70");
int num;
cout << "enter the number";

cin >> num;


if (num >= 0 && num < 30)
{
cout << "range is from 0 to 29";

}
else if (num >=30 && num < 50)
cout << "range is from 30 to 50";
else if (num >= 50 && num <= 80)

{
cout << "range is from 50 to 80";
}
else

{
cout << "error";
}
getchar();
return 0;
}

Output:

TASK 3 :
(Printing the Decimal Equivalent of a Binary Number) Input an integer (5 digits or fewer)
containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent. [Hint: Use
the remainder and division operators to pick off the “binary” number’s digits one at a time from
right to left. Just as in the decimal number system, in which the rightmost digit has a positional
value of 1, and the next digit left has a positional value of 10, then 100, then 1000, and so on, in
the binary number system the rightmost digit has a positional value of 1, the next digit left has
a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be
interpreted as 4 * 1 + 3 * 10 + 2 * 100. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1
* 4 + 1 * 8 or 1 + 0 + 4 + 8 or 13.]

Coding:
Output:

TASK 4:
Print the value 123.4567 with two digits of precision. What value is printed?

Coding:
Output:

TASK 6:
Identify and correct the errors in each of the following:
(i)
if ( gender == 1 )
cout<< "Woman" ;
else;
cout<< "Man" ;

Error:
There must be no Semi colon after else.

(ii)
if ( a= 1 )

cout<< "a is equal to 1" ;


else
cout<< "a is not equal to 1" ;

Error:
There is an equal to missing in if condition which will be if (a==1).

(iii)
if ( ch == 1 );
cout<< "Lab" ;

else
cout<< "Tutorial" ;

Error:
In the first line of if condition, there must be no semi colon at the end of first line.

(iv)

Write four different C statements that each adds 1 to integer variable x.


First statement:
Add=1+x;
Second statement:
Add=++x;
Third statement:

Add=x++;
Fourth sentence:
X+=1;

TASK 7:
7. (Credit Limit Calculator) Develop a C++ program that will determine if a department store
customer has exceeded the credit limit on a charge account. For a customer, the following facts
are available:

a. Account number
b. Balance at the beginning of the month
c. Total of all items charged by this customer this month
d. Total of all credits applied to this customer's account this month

e. Allowed credit limit


The program should input each fact, calculate the new balance (= beginning balance + charges –
credits), and determine whether the new balance exceeds the customer's credit limit. For those
customers whose credit limit is exceeded, the program should display the customer's account
number, credit limit, new balance and the message “Credit limit exceeded.”

Coding:
using namespace std;
int main()

{
system("color 70");
int Account_Number;
double starting_balance;

double total_charges;
double total_credit;
double credit_limit;
double new_balance;

cout << " Please Enter The AccountNumber\n";

cin >> Account_Number;


cout << " Please Enter Beginning Balance:\n";
cin >> starting_balance;

cout << " Please Enter the Total Charges:\n";


cin >> total_charges;
cout << " Please Enter Total Credits:\n";
cin >> total_credit;
cout << " Please Enter Credit Limit:\n";

cin >> credit_limit;


new_balance = starting_balance + total_charges - total_credit;
cout << " NewBalance: " << new_balance << endl;
if (new_balance > credit_limit)

{
cout << " Credit LimitExceeded. " << endl;
}
getchar();

getchar();
return 0;

Output:
}

Task 8:
Write a program that demonstrates the difference between predecrementing and
postdecrementing using the decrement operator --.

Coding:
#include<iostream>
using namespace std;
int main()

{
system("color 70");
int a = 2;
//predecrement
a = --a;

cout << "predecrement\n" << a << '\n';


//postdecrement
a = a--;
cout << "postdecrement\n" << a << '\n';

getchar();
getchar();
return 0;
}
Output:

Task 9:
(Dangling Else Problem) Determine the output for each of the following when x is 9 and y is 11,
and when x is 11 and y is 9. The compiler ignores the indentation in a C++ program. Also, the
compiler always associates an else with the previous if unless told to do otherwise by the
placement of braces {}. Because, on first glance, you may not be sure which if an else matches,
this is referred to as the “dangling else” problem. We eliminated the indentation from the
following code to make the problem more challenging.

Coding:
#include <iostream>
using namespace std;
int main()
{
system("color 70");

int x, y;
cout << "enter value of x \n ";
cin >> x ;
cout << "enter value of x \n ";

cin >> y;

if (x < 10)

if (y > 10)

cout << "*****" << endl;


else

cout << "#####" << endl;

cout << "$$$$$" << endl;

getchar();
getchar();
return 0;
}

Output:
(a)

(b)

Task 10:
Write a C++ program to find maximum between two numbers using switch statement.
Coding:
#include <iostream>

using namespace std;


int main()
{
system("color 70");
int num1, num2;

cout << "Enter first number: ";


cin >> num1;
cout << "enter the second number: ";
cin >> num2;

switch (num1 > num2)


{

case 0:
cout << num2 << " is Maximum number";
break;

case 1:
cout << num1 << " is Maximum number";
break;

getchar();
getchar();
return 0;
}

Output:

Task 11:
Write a C++ program to find the day of the week using switch statement.

Coding:

#include <iostream>
using namespace std;

int main()
{
system("color 70");
int Day;

cout << "Enter the Day: ";


cin >> Day;
switch (Day)
{
case 1: cout << "Monday";

break;
case 2: cout << "Tuesday";
break;
case 3: cout << "Wednesday";
break;
case 4: cout << "Thursday";
break;

case 5: cout << "Friday";


break;
case 6: cout << "Saturday";
break;

case 7: cout << "Sunday";


break;
default: cout << "Invalid input";
}

getchar();
getchar();
return 0;
}

Output:

Task 12:
Write a C++ program to check whether an alphabet is a vowel or consonant using switch
statement.

Coding:

#include<iostream>
using namespace std;
Int main()
{
char ch;

cout<<"Enter any character from A to Z= ";


cin>>ch;
switch(ch)
{

case 'A':
cout<<"Vowel";
break;
case 'E':

cout<<"Vowel";
break;
case 'I':
cout<<"Vowel";
break;

case 'O':
cout<<"Vowel";
break;
case 'U':
cout<<"Vowel";
break;
case 'a':
cout<<"Vowel";

break;
case 'e':
cout<<"Vowel";
break;
case 'i':
cout<<"Vowel";

break;
case 'o':
cout<<"Vowel";
break;

case 'u':
cout<<"Vowel";
break;
default:

cout<<"consonant";
}
getchar();
getchar();
return 0;

Output:

Task 13:
Program to Calculate Grades according to your grading system using switch statement. Total
marks are supposed to be 100.

Coding:
#include <iostream>
using namespace std;
int main()
{
system("color 70");

int s1,s2,s3,s4,s5, Total, Average;


cout << " Enter Marks of first subject out of hundred: ";
cin >> s1;
cout << " Enter Marks of second subject out of hundred: ";

cin >> s2;


cout << " Enter Marks of third subject out of hundred: ";
cin >> s3;
cout << " Enter Marks of forth subject out of hundred: ";

cin >> s4;


cout << " Enter Marks of fifth subject out of hundred : ";
cin >> s5;
Total = s1 + s2 + s3 + s4 + s5;
Average = Total / 5;

switch (Average / 10)


{
case 10:
cout << "\t A+";
break;
case 9:
cout << "\t A";
break;

case 8:
cout << "\t A-";
break;
case 7:
cout << "\t B";
break;

case 6:
cout << "\t C";
break;
case 5:

cout << "\t D";


break;
default:
cout << "Student is Fail ";

break;
}
getchar();
getchar();

return 0;
}

Output:

Task 14:
Complete the following truth tables by filling in each blank with 0 or 1.

(i)
Condition 1 condition 2 condition 1 && condition 2
0 0 0
0 1 0
1 0 0
1 1 1

(ii)
Condition 1 condition 2 condition 1 || condition 2
0 0 0
0 1 1
1 0 1
1 1 1

You might also like