0% found this document useful (0 votes)
3 views25 pages

C++ Programming

This document is a lab report for Programming Fundamentals (CS-308) by Faizan Sharif, detailing various programming exercises using conditional statements in C++. It includes solutions for checking even/odd numbers, determining grades based on percentages, and implementing a basic calculator, among other tasks. Each exercise is accompanied by code snippets and expected outputs.

Uploaded by

faizansharif678
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

C++ Programming

This document is a lab report for Programming Fundamentals (CS-308) by Faizan Sharif, detailing various programming exercises using conditional statements in C++. It includes solutions for checking even/odd numbers, determining grades based on percentages, and implementing a basic calculator, among other tasks. Each exercise is accompanied by code snippets and expected outputs.

Uploaded by

faizansharif678
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROGRAMMING FUNDAMENTALS (CS-308)

NAME: Faizan Sharif

AG: 2025-ag-11396

LAB REPORT # 2

CLASS: B.S C. S

SECTION:2nd E

DEPARTMENT OF COMPUTER SCIENCE, PARS

UNIVERSITY OF AGRICULTURE, FAISALABAD


LAB REPORT # 2

Activity 1: Single Selection


(if statement)
Q#1: Write a program that takes an integer from the user and
checks whether the number is even or odd using an if statement.

• Practical Solution#1:

#include <iostream>
using namespace std;
int main() {
int num;
cin >> num
if(num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

• Output#1:
Q#2: Write a program that takes a number as input and determines
whether the number is positive, negative, or zero.

• Practical Solution#2:

#include <iostream>

using namespace std;

int main() {

int num;

cin >> num;


if(num > 0)

cout << "Positive";

else if(num < 0)

cout << "Negative";

else

cout << "Zero";

return 0;

• Output#2:
Q#3: Write a program that inputs three numbers from the user and
determines which number is the largest using conditional statements.

• Practical Solution#3:

#include <iostream>

using namespace std;

int main() {

int a, b, c;

cin >> a >> b >> c;

if(a >= b && a >= c)

cout << "Largest: " << a;

else if(b >= a && b >= c)

cout << "Largest: " << b;

else

cout << "Largest: " << c;

return 0;

• Output#3:
Q#4: Write a program that takes a character from the user and checks
whether it is a vowel or a consonant using logical operators.

• Practical Solution#4:

#include <iostream>
using namespace std;
int main() {
char ch;
cin >> ch;
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
cout << "Vowel";
else
cout << "Consonant";
return 0;
}
• Output#4:

Q#5: Write a program that inputs a number and checks whether it is


divisible by both 3 and 5 using logical operators.

• Practical Solution#5:

#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
if(num % 3 == 0 && num % 5 == 0)
cout << "Divisible by both 3 and 5";
else
cout << "Not divisible";
return 0;
}

• Output#5:
Q#6: Write a program that inputs marks of 5 subjects of a student and
calculate percentage of that student. Assigns a grade based on the following
conditions:

o percentage ≥ 80 → Grade A
o percentage ≥ 70 → Grade B
o percentage ≥ 60 → Grade C
o percentage ≥ 50 → Grade D
o percentage < 50 → Fail

• Practical Solution#6:

#include <iostream>
using namespace std;
int main() {
float m1, m2, m3, m4, m5, per;
cin >> m1 >> m2 >> m3 >> m4 >> m5;
per = (m1+m2+m3+m4+m5)/5;
if(per >= 80) cout << "Grade A";
else if(per >= 70) cout << "Grade B";
else if(per >= 60) cout << "Grade C";
else if(per >= 50) cout << "Grade D";
else cout << "Fail";
return 0;
}
• Output#6:

Q#7: Write a program that inputs two numbers and displays which
number is greater or displays "Both numbers are equal" if they are the
same.

• Practical Solution#7:

#include <iostream>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if(a > b)
cout << "A is greater";
else if(b > a)
cout << "B is greater";
else
cout << "Equal";
return 0;
}

• Output#7:

Activity 2: Double Selection


(if-else statement, ternary
operator)
Q#8: Write a program that inputs the age of a person and determines
whether the person is eligible to vote (age ≥ 18) or not eligible by using
ternary operator.

• Practical Solution#8:

#include <iostream>
using namespace std;
int main() {
int age;
cin >> age;
(age >= 18) ? cout << "Eligible" : cout << "Not Eligible";
return 0;
}

• Output#8:

Q#9: Write a program that takes a student’s marks as input and displays
"Pass" if marks are greater than or equal to 50, otherwise display "Fail"
(use ternary o.
• Practical Solution#9:

#include <iostream>
using namespace std;
int main() {
int marks;
cin >> marks;
(marks >= 50) ? cout << "Pass" : cout << "Fail";
return 0;
}

• Output#9:

Activity 3: Multiple Selection


(else ifs statement)

Q#10: Write a program that inputs a person’s age and displays whether
the person is a child (age < 13), teenager (13–19), or adult (20 and above).
• Practical Solution#10:

#include <iostream>
using namespace std;
int main() {
int age;
cin >> age;
if(age < 13) cout << "Child";
else if(age <= 19) cout << "Teenager";
else cout << "Adult";
return 0;
}

• Output#10:

Q#11: Write a program that inputs a number and checks whether the
number is divisible by both 3 and 5, only by 3, only by 5, or none of them.

• Practical Solution#11:
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
if(num%3==0 && num%5==0)
cout << "Both";
else if(num%3==0)
cout << "Only 3";
else if(num%5==0)
cout << "Only 5";
else
cout << "None";
return 0;
}

• Output#11:

Q#12: Write a program that asks the user to enter three numbers and
determines whether all numbers are equal, two numbers are equal, or all
numbers are different.
• Practical Solution#12:

#include <iostream>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
if(a==b && b==c)
cout << "All equal";
else if(a==b || b==c || a==c)
cout << "Two equal";
else
cout << "All different";
return 0;
}

• Output#12:

Q#13: Write a program that inputs the age of a person and determines
whether the person is not eligible for voting (<18), eligible (18–60), or a
senior citizen (>60).

• Practical Solution#13:
#include <iostream>
using namespace std;
int main() {
int age;
cin >> age;
if(age < 18) cout << "Not eligible";
else if(age <= 60) cout << "Eligible";
else cout << "Senior citizen";
return 0;
}

• Output#13:

Q#14: Write a program that asks the user to enter the marks of a student
and displays Grade A (≥85), Grade B (70–84), Grade C (50–69), or Fail
(<50). Use logical operators.

• Practical Solution#14:

#include <iostream>
using namespace std;
int main() {
int marks;
cin >> marks;
if(marks >= 85) cout << "Grade A";
else if(marks >= 70) cout << "Grade B";
else if(marks >= 50) cout << "Grade C";
else cout << "Fail";
return 0;
}

• Output#14:

Q#15: Write a program that asks the user to enter the lengths of three
sides of a triangle. The program should then determine the type of triangle
based on the equality of its sides.

o If all three sides are equal, the triangle is called an Equilateral


Triangle.
o If exactly two sides are equal, the triangle is called an Isosceles
Triangle.
o If all three sides are different, the triangle is called a Scalene
Triangle.
The program should display the type of triangle according to the entered
side lengths.

• Practical Solution#15:

#include <iostream>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
if(a==b && b==c)
cout << "Equilateral";
else if(a==b || b==c || a==c)
cout << "Isosceles";
else
cout << "Scalene";
return 0;
}

• Output#15:
Q#16: Write a program that takes a single character as input from the
user. The program should determine the type of the character using
conditional statements.

o If the character is between '0' and '9', it should be classified as a


Digit.
o If the character is between 'A'–'Z' or 'a'–'z', it should be classified as
an Alphabet.
o Any other character should be classified as a Special Character.

The program should display the appropriate category of the character.

• Practical Solution#16:

#include <iostream>
using namespace std;
int main() {
char ch;
cin >> ch;
if(ch >= '0' && ch <= '9')
cout << "Digit";
else if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
cout << "Alphabet";
else
cout << "Special Character";
return 0;
}

• Output#16:
Q#17: Write a program that asks the user to enter the basic salary of an
employee. The program should calculate the net salary by adding an
allowance according to the following conditions:

o If the basic salary is less than 50,000, the employee receives an


allowance of 20% of the salary.
o If the basic salary is 50,000 or greater, the employee receives an
allowance of 10% of the salary.

The program should then calculate the net salary using the formula:
Net Salary = Basic Salary + Allowance

Finally, the program should display the allowance amount and the net
salary.

• Practical Solution#17:

#include <iostream>
using namespace std;
int main() {
float salary, allowance, net;
cin >> salary;
if(salary < 50000)
allowance = salary * 0.20;
else
allowance = salary * 0.10;
net = salary + allowance;
cout << "Allowance: " << allowance << endl;
cout << "Net Salary: " << net;
return 0;
}

• Output#17:

Q#18: Develop a calculator which will calculate Sum, minus,


multiplication and division of two user given numbers. It will calculate
subtraction only when first number is greater than second number
otherwise it should display *cannot subtract.*. Same rule is applied for
division. If the second number is 0, do not divide both numbers, instead
display *cannot divide a number by 0*.
• Practical Solution#18:

#include <iostream>
using namespace std;
int main() {
float a,b;
cin >> a >> b;
cout << "Sum = " << a+b << endl;
if(a > b)
cout << "Subtraction = " << a-b << endl;
else
cout << "Cannot subtract\n";
cout << "Multiplication = " << a*b << endl;
if(b == 0)
cout << "Cannot divide\n";
else if(a > b)
cout << "Division = " << a/b;
else
cout << "Cannot divide";
return 0;
}

• Output#18:
Q#19: Ask the user to enter two numbers and then ask to enter the
operator (+, -, /, *). Calculate and display the result (use multiple selection
statements).

• Practical Solution#19:

#include <iostream>
using namespace std;
int main() {
float a,b;
char op;
cin >> a >> b >> op;
if(op == '+') cout << a+b;
else if(op == '-') cout << a-b;
else if(op == '*') cout << a*b;
else if(op == '/') cout << a/b;
else cout << "Invalid";
return 0;
}

• Output#19:
Q#20: A bank gives loan in two situations
o If customer is male and his age is more than 25 and his
salary is more than 25000
o If customer is a female and her age is more than 30 and
her income is 20000

Program: Ask user to enter age, gender(M/F) and income. Check whether
he\she is eligible to get loan or not.

• Practical Solution#20:

#include <iostream>
using namespace std;
int main() {
int age;
char gender;
float income;
cin >> age >> gender >> income;
if((gender=='M' && age>25 && income>25000) ||
(gender=='F' && age>30 && income>20000))
cout << "Eligible for loan";
else
cout << "Not eligible";
return 0;
}

• Output#20:

{End Of Lab}

You might also like