Assignment-1: 24F-6005 EE-1A
Assignment-1: 24F-6005 EE-1A
Assignment-1: 24F-6005 EE-1A
24F-6005
Abubakar Adeel
EE-1A
Question 1: Evaluate the following arithmetic expressions and write down the result for each.
Question 2: Evaluate the following arithmetic expressions and write down the result for each.
Arithmetic Expression Result
5.0 + 3.5 8.5
3.0 + 9.4 12.4
16.3 - 5.2 11.1
4.2 * 2.5 10.5
5.0 / 2.0 2.5
34.5 / 6.0 5.75
34.5 / 6.5 5.30769
Question 3: Write a C++ program that declares a variable miles of type double. The program
should take input from the user for miles and display the value on the screen.
#include <iostream>
using namespace std;
int main() { Hello there.My name is James.
cout << "Hello there.";
cout << "My name is James.";
Hello there.
cout << endl;
cout << "Hello there.\n";
cout << "My name is James."; My name is James.
return 0;
}
Question 7: Which of the following are valid C++ assignment statements? Assume that i, x, and
percent are double variables.
a) i = i + 5; (Valid)
b) x + 2 = x; (Invalid)
Reason: Left side must be a variable so, x = x + 2 will be correct
c) x = 2.5 *x; (Valid)
d) percent = 10%; (Invalid)
Reason: % operator can not be used as percentage sign where as this is modulo operator.
Question 6: Which of the following variable declarations are correct? If a variable declaration
is not correct, give the reason(s) and provide the correct variable declaration.
a) n = 12; (False)
Reason: No datatype is assigned to the variable
b) char letter = ; (False)
Reason: The variable requires a value to be declared.
c) int one = 5, two; (Correct) the syntax of line is valid and two can be declared after.
d) double x, y, z; (Correct)
Question 8: Consider the following C++ program in which the statements are in the incorrect
order. Rearrange the statements so that it prompts the user to input the length and width of a
rectangle and output the area and perimeter of the rectangle.
Corrected Code:
#include <iostream>
using namespace std;
int main() {
int length;
int width;
int area;
int perimeter;
cout << "Enter the length: ";
cin >> length;
cout << "Enter the width: ";
cin >> width;
area = length * width;
perimeter = 2 * (length + width);
cout << endl;
cout << "Area = " << area << endl;
cout << "Perimeter = " << perimeter << endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string name;
double rate, hoursWorked, wages;
cout << "Enter name: ";
cin >> name;
cout << "Enter pay rate: $";
cin >> rate;
cout << "Enter hours: $";
cin >> hoursWorked;
wages = rate * hoursWorked;
cout << endl;
cout << "Name: " << name << endl;
cout << fixed << setprecision(2); // Ensure two decimal places
cout << "Pay Rate: $" << rate << endl;
cout << "Hours Worked: " << hoursWorked << endl;
cout << "Salary: $" << wages << endl;
return 0;
}
Question 10: Write a C++ program to swap the values of two positive numbers using third
variable.
#include <iostream>
Using namespace std;
int main() {
int a, b, temp;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
temp = a;
a = b;
b = temp;
cout << "After swapping:" << endl;
cout << "First number: " << a << endl;
cout << "Second number: " << b << endl;
return 0;
}
Question 11: Write a C++ program that takes dividend and divisor from the user and prints
quotient and remainder.
#include <iostream>
using namespace std;
int main() {
int dividend, divisor, quotient, remainder;
cout << "Enter the dividend: ";
cin >> dividend;
cout << "Enter the divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Quotient: " << quotient << endl;
cout << "Remainder: " << remainder << endl;
return 0;
Question 12: Write a C++ program that prints the grade you wish to pass this course with.
Print using the char data type.
Code:
#include <iostream>
using namespace std;
int main() {
char grade;
cout << "Enter the grade you wish to pass the course with: ";
cin >> grade;
cout << "You wish to pass the course with grade: " << grade << end;
return 0;
}
Question 13: Create a program that calculates the profit of a company. Ask the user to input
revenue and expenses, then display the profit.
#include <iostream>
using namespace std;
int main() {
double revenue, expenses, profit;
cout << "Enter the revenue: ";
cin >> revenue;
cout << "Enter the expenses: ";
cin >> expenses;
profit = revenue - expenses;
cout << "Profit: $" << profit << endl;
return 0;
}