SUPERIOR UNIVERSITY
ASSIGNMENT NO. 1
PROGRAMING FUNDANETAL
Academic Year: 2022 – 2026
BS ROBOTICS
Abdul Muizz | Enrollment No.
PF / SIR AHMED BILAL
Date of Submission: March 15, 20XX
Question #1: Write a C++ program that ask user to enter his age then using following rules print their place of service.
1. if employee age is in between 20 to 40 then he may work in anywhere 2. if employee age is in between 40 t0 60
then he will work in urban areas only. 3. And any other input of age should print "ERROR".
Code:
#include <iostream>
using namespace std;
int main()
int age; // declare an integer variable age to store the user's age
cout << "Enter your age: ";
cin >> age; // prompt the user to enter their age and read it into the variable age
// check if the age is between 20 and 40, inclusive
if (age >= 20 && age <= 40)
cout << "You may work anywhere" << endl; // print message indicating that the employee may work anywhere
// check if the age is between 40 and 60, inclusive
else if (age > 40 && age <= 60)
cout << "You can only work in urban areas" << endl; // print message indicating that the employee can only work in
urban areas
// if the age is not within the specified range, print an error message
else
cout << "ERROR" << endl; // print message indicating that the input age is invalid
return 0;
}
Question #2: Write a C++ program that ask user to enter marks and print the corresponding grade. THE SUPERIOR
UNIVERSITY LAHORE (Faisalabad Campus) Assignment_01 (BSSE/BSIT) Faculty of Computer Science and Information
Technology Subject: Programming Fundamentals Instructor: Mr. Ch. Ahmad Bilal Total marks: 30 Instructor:
Muhammad Hassan Total Marks: 50 Using following rules for grading system? a. Below 25 - F b. 25 to 45 - E c. 45 to 50
- D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.
Code:
#include <iostream>
using namespace std;
int main()
int marks; // declare an integer variable marks to store the user's marks
cout << "Enter your marks: ";
cin >> marks;
// check if the marks fall within a certain range
if (marks < 25)
cout << "Grade: F" << endl; // print message indicating the corresponding grade is F
else if (marks >= 25 && marks < 45)
cout << "Grade: E" << endl; // print message indicating the corresponding grade is E
}
else if (marks >= 45 && marks <= 50)
cout << "Grade: D" << endl; // print message indicating the corresponding grade is D
else if (marks > 50 && marks <= 60)
cout << "Grade: C" << endl; // print message indicating the corresponding grade is C
else if (marks > 60 && marks <= 80)
cout << "Grade: B" << endl; // print message indicating the corresponding grade is B
else if (marks > 80)
cout << "Grade: A" << endl; // print message indicating the corresponding grade is A
else
cout << "Invalid marks entered." << endl; // print message indicating that the input marks is invalid
return 0;
}
Question #3: Write a C++ program that input the age of three peoples and determine oldest and youngest among
them?
Code:
#include <iostream>
using namespace std;
int main()
// Declare three integer variables to store the ages of three people.
int age1, age2, age3;
// Prompt the user to enter the age of each person.
cout << "Enter the age of first person: ";
cin >> age1;
cout << "Enter the age of second person: ";
cin >> age2;
cout << "Enter the age of third person: ";
cin >> age3;
// Determine the oldest person.
if (age1 >= age2 && age1 >= age3)
cout << "The oldest person is " << age1 << " years old." << endl;
}
else if (age2 >= age1 && age2 >= age3)
cout << "The oldest person is " << age2 << " years old." << endl;
else
cout << "The oldest person is " << age3 << " years old." << endl;
// Determine the youngest person.
if (age1 <= age2 && age1 <= age3)
cout << "The youngest person is " << age1 << " years old." << endl;
else if (age2 <= age1 && age2 <= age3)
cout << "The youngest person is " << age2 << " years old." << endl;
else
cout << "The youngest person is " << age3 << " years old." << endl;
return 0;
}
Question #4: Write a C++ program to calculate area of the circle if the radius is greater than 0 using goto statement.?
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
double radius, area;
input:
cout << "Enter the radius of the circle: "; // Prompt the user to enter the radius
cin >> radius; // Read the radius entered by the user
if(radius <= 0) // Check if the radius is valid
cout << "Radius should be greater than 0." << endl; // Output an error message if the radius is invalid
goto input; // Use goto statement to jump back to the input label and prompt the user again for a valid radius
area = M_PI * pow(radius, 2); // Calculate the area of the circle using the formula
cout << "The area of the circle is " << area << endl; // Output the calculated area of the circle
return 0;
Question #5: Write a C++ program in which a user enters a number, and the program will find the Even and odd
number. Your program should only accept integers of length 2 or higher and should ask the user to re-enter a value if
the value containing total numbers of integers less than 2. The program with goto statement?
Code:
#include <iostream>
using namespace std;
int main()
int num;
input:
cout << "Enter a number (must be at least 2 digits long): "; // Prompt the user to enter a number
cin >> num; // Read the number entered by the user
if(num < 10) // Check if the number entered by the user has less than 2 digits
cout << "Number must be at least 2 digits long." << endl; // Output an error message if the number has less than 2
digits
goto input; // Use goto statement to jump back to the input label and prompt the user again for a valid number
}
if(num % 2 == 0) // Check if the number is even
cout << "The number " << num << " is even." << endl; // Output a message indicating that the number is even
else // If the number is not even, it must be odd
cout << "The number " << num << " is odd." << endl; // Output a message indicating that the number is odd
return 0;
Question #6: Write a C++ program to find the roots of a quadratic equation ax2 + bx + c = 0?
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, determinant, root1, root2, real, imag;
cout << "Enter coefficients a, b and c of quadratic equation \n";
cin >> a >> b >> c;
/* Calculate determinant */
determinant = b*b - 4*a*c;
if(determinant >= 0)
root1= (-b + sqrt(determinant))/(2 * a);
root2= (-b - sqrt(determinant))/(2 * a);
cout << "Square roots are " << root1 <<
" " << root2;
else {
real= -b/(2*a);
imag = sqrt(-determinant)/(2 * a);
cout << "Square roots are " << real << "+" << imag << "i , "
<< real << "-" << imag << "i";
return 0;