Computer Fundamentals and Programming: Lab Report
Computer Fundamentals and Programming: Lab Report
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";
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";
}
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 )
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)
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
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 << " 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;
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)
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>
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;
break;
case 2: cout << "Tuesday";
break;
case 3: cout << "Wednesday";
break;
case 4: cout << "Thursday";
break;
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;
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");
case 8:
cout << "\t A-";
break;
case 7:
cout << "\t B";
break;
case 6:
cout << "\t C";
break;
case 5:
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