Practical 3] Write a C++ program that prompts the user to input three integer values and find
the greatest value of the three values.
Solution:
* In this program, the user is asked to enter three numbers.
* Then this program finds out the largest number among three numbers entered
by user and displays it with a proper message.
Code:
#include <iostream>
using namespace std;
int main() {
double n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
// check if n1 is the largest number
if(n1 >= n2 && n1 >= n3)
cout << "Largest number: " << n1;
// check if n2 is the largest number
else if(n2 >= n1 && n2 >= n3)
cout << "Largest number: " << n2;
// if neither n1 nor n2 are the largest, n3 is the largest
else
cout << "Largest number: " << n3;
return 0;
}
Practical 4] Write a program that determines a student’s grade. And determine the grade
based on the following rules:
- If the average score >= 90 → grade A+
- If the average score >= 80 → grade A
- If the average score >= 70 → grade B+
- If the average score >= 60 → grade B
- If the average score >= 50 → grade C
- If the average score >= 40 → grade D
MARKS GRADES
100 - 90 A+
90 – 80 A
80 – 70 B+
70 – 60 B
60 – 50 C
50 – 40 D
Solution:
#include <iostream>
Using namespace std;
int main(){
int marks;
cout<<”Enter your Marks: ”;
cin>>marks;
if (marks >= 90){
cout<<”Your grade is A+”;
}
else if (marks >= 80){
cout<<”Your grade is A”;
}
else if (marks >= 70){
cout<<”Your grade is B+”;
}
else if (marks >= 60){
cout<<”Your grade is B”;
}
else if (marks >= 50){
cout<<”Your grade is C”;
}
else if (marks >= 40){
cout<<”Your grade is B”;
}
else{
cout<<”Enter valid marks”;
}
Return 0;
}
Practical 5] Define a class called as circle which has radius as its data member. The class
should have following member functions
a. Function to set the value of radius.
b. Function to get the value of radius.
c. Function to calculate and return the area of circle.
d. Function to calculate and return circumference.
Solution:
#include <iostream>
#include <cmath>
const double PI = 3.14159; // Define the value of PI as a constant
class Circle {
private:
double radius;
public:
Circle(double rad): radius(rad) {}
double calculateArea() {
return PI * pow(radius, 2);
}
double calculateCircumference() {
return 2 * PI * radius;
}
};
int main() {
double radius;
std::cout << "Input the radius of the circle: ";
std::cin >> radius;
Circle circle(radius);
double area = circle.calculateArea();
std::cout << "Area: " << area << std::endl;
double circumference = circle.calculateCircumference();
std::cout << "Circumference: " << circumference << std::endl;
return 0;
}
Practical 6] Develop a one-digit counter to determine the following function. The class
should have a following function
a. Function to set the value of the counter.
b. Function to display value of the counter.
c. Function to increment the counter.
d. Function to decrement the counter.
Solution:
#include <iostream>
using namespace std;
main(){
int number = 21;
int counter ;
counter = number++;
cout <<”1. Value of Counter is : “ <<counter << endl;
cout <<”2. Value of Counter in Increment Form: ” << number << endl;
number = ++number;
cout <<”3. Value of Counter in Decrement Form: ” << counter << endl;
return 0;
}