Docsity

Basic C++ Programming: Questions and Solutions, Exercises of Computer science

Toggle sidebar
bg1
1. Write a C++ program to get 5 subjects marks of a student and then calculate its
total,average and percentage and display them on the screen.
#include <iostream>
using namespace std;
int main() {
float mark1, mark2, mark3, mark4, mark5, total, average, percentage;
// Input marks of five subjects
cout << "Enter marks of five subjects (out of 100): " << endl;
cout << "Subject 1: ";
cin >> mark1;
cout << "Subject 2: ";
cin >> mark2;
cout << "Subject 3: ";
cin >> mark3;
cout << "Subject 4: ";
cin >> mark4;
cout << "Subject 5: ";
cin >> mark5;
// Calculate total, average, and percentage
total = mark1 + mark2 + mark3 + mark4 + mark5;
average = total / 5;
percentage = (total / 500) * 100;
// Display the results
cout << "\nTotal Marks = " << total << endl;
cout << "Average Marks = " << average << endl;
cout << "Percentage = " << percentage << "%" << endl;
return 0;
pf3
pf4
pf5

Partial preview of the text

Download Basic C++ Programming: Questions and Solutions and more Exercises Computer science in PDF only on Docsity!

1. Write a C++ program to get 5 subjects marks of a student and then calculate its total,average and percentage and display them on the screen. #include using namespace std; int main() { float mark1, mark2, mark3, mark4, mark5, total, average, percentage; // Input marks of five subjects cout << "Enter marks of five subjects (out of 100): " << endl; cout << "Subject 1: "; cin >> mark1; cout << "Subject 2: "; cin >> mark2; cout << "Subject 3: "; cin >> mark3; cout << "Subject 4: "; cin >> mark4; cout << "Subject 5: "; cin >> mark5; // Calculate total, average, and percentage total = mark1 + mark2 + mark3 + mark4 + mark5; average = total / 5; percentage = (total / 500) * 100; // Display the results cout << "\nTotal Marks = " << total << endl; cout << "Average Marks = " << average << endl; cout << "Percentage = " << percentage << "%" << endl; return 0;

2.Write a C++ program to find the maximum value out of four integers using

conditional operators.

#include using namespace std; int main() { int a, b, c, d, max; // Input four integers cout << "Enter four integers: "; cin >> a >> b >> c >> d; // Using conditional operator to find maximum max = (a > b)? ((a > c)? ((a > d)? a : d) : ((c > d)? c : d)) : ((b > c)? ((b > d)? b : d) : ((c > d)? c : d)); // Output the maximum value cout << "The maximum value is: " << max << endl; return 0; } 3.Write a C++ program to find out the roots of a quadratic equation. #include #include // For sqrt() function using namespace std; int main() { double a, b, c, discriminant, root1, root2, realPart, imagPart;

return 0; } 4.Write a C++ program to find out the area of a rectangle and display the result on the screen #include using namespace std; int main() { double length, width, area; // Input length and width of the rectangle cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the width of the rectangle: "; cin >> width; // Calculate the area of the rectangle area = length * width; // Display the area cout << "The area of the rectangle is: " << area << endl; return 0; } 5 .Write a C++ program to get the age of a student from the user at run time and display it on the screen. #include

using namespace std; int main() { int age; // Input age of the student cout << "Enter the age of the student: "; cin >> age; // Display the age of the student cout << "The age of the student is: " << age << " years." << endl; return 0; }