Pratham Pareek BSC Cs Oops Prac 3
Pratham Pareek BSC Cs Oops Prac 3
1. Write a C++ program that accepts a year and finds whether year entered is a leap year or
not.
int main() {
int a;
cout << "Enter a year: ";
cin >> a;
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) {
cout << a << " is a leap year." << endl;
}
else
{
cout << a << " is not a leap year." << endl;
}
OUTPUT:
2. Write a C++ program that accepts an age of a person and finds whether he/she is eligible
to vote or not and he/she is eligible to drive or not.
int main()
{
int a;
cout << "Enter your age: ";
cin >> a;
if (a<18) {
cout << "Your not eligible to drive and vote" << endl;
} else {
cout << "Your eligible to drive and vote" << endl;
}
OUTPUT:
int main()
{
int a;
cout << "Enter the Number: ";
cin>>a;
if (a % 2 == 0)
{
cout<<"The given Number is Even"<<endl;
}
else
{
cout<<"The given Number is Odd"<<endl;
}
OUTPUT:
int main()
{
int a;
cout << "Enter the Number: ";
cin>>a;
if (a % 3 == 0)
{
cout<<"The given Number is Divisible by 3"<<endl;
return 0;
}
else
{
cout<<"The given Number is not Divisible by 3"<<endl;
}
}
OUTPUT:
int main()
{
int a;
cout << "Enter the Number: ";
cin>>a;
if (a % 7 == 0)
{
cout<<"The given Number is Divisible by 7"<<endl;
return 0;
}
else
{
cout<<"The given Number is not Divisible by 7"<<endl;
}
}
OUTPUT:
6. Write a C++ program to check whether number entered is divisible by 3 or 5 or both.
int main()
{
int a;
cout << "Enter the Number: ";
cin>>a;
if (a % 3 == 0 && a % 5 == 0)
{
cout<<"The given Number is Divisible by 3 and 5 both"<<endl;
return 0;
}
else
{
cout<<"The given Number is not Divisible by 3 and 5 both"<<endl;
}
}
OUTPUT:
7. Write a C++ program to check whether number entered is positive, negative or zero.
int main()
{
int a;
cout << "Enter the Number: ";
cin>>a;
if (a > 0)
{
cout<<"The given Number Positive"<<endl;
return 0;
}
else if (a < 0)
{
cout<<"The given Number is Negative"<<endl;
}
else
{
cout<<"The given Number is Zero"<<endl;
}
}
OUTPUT:
int main() {
// Declare two variables
int num1, num2;
int main() {
int num1, num2, num3;
cout << "Enter the first number: ";
cin >> num1;
return 0;
}
Output:-
10. Write a C++ program to read marks for a student from user and grade the student
according to the following
Marks Grades
Above 80 O
70-79 A+
60-69 A
55-59 B+
50-54 B
45-49 C
40-45 D
Below 40 Fail
Code:-
#include <iostream>
using namespace std;
int main() {
int marks;
return 0;
}
Output:-
11. Write a C++ program to compute the roots of a quadratic equation ax2+bx+c=0
The roots are given by equation. (-b√b2-4ac)/2a
Code:-
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c, discriminant, root1, root2;
cout << "Enter the coefficients a, b, and c of the quadratic equation (ax^2 + bx + c = 0):"
<< endl;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "The roots are real and distinct." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
}
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
cout << "The roots are real and equal." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "The roots are complex." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}
return 0;
}
Output:-
12. BEST power distribution company charges its domestic consumers as follows:
Consumption Units Rate
0-100 Rs. 100
101-200 Rs.100+Rs. 5 per unit above 100 units
201-400 Rs.200+Rs.10 unit above 200 units
401-600 Rs.250+Rs.15 per unit above 400 units
601& above Rs.300+Rs.20 per unit above 600 units
Write a C++ program to read customer number and power consumed and prints the
amount to be paid by the customer.
Code:-
#include <iostream>
using namespace std;
int main() {
int customerNumber, unitsConsumed;
double totalAmount = 0.0;
return 0;
}
Output:-
13. The admission to a professional course if marks in subjects are as follows:
If marks in Maths >= 60 and marks in Physics >= 60 marks in Chemistry >= 60 and total
in all 3 subjects >=200 or total in Maths and Physics>=150.
Write a C++ program to accept marks out of 100 for 3 subjects and finds the candidate is
eligible or not.
Code:-
#include <iostream>
using namespace std;
int main() {
int maths, physics, chemistry, total;
if ((maths >= 60 && physics >= 60 && chemistry >= 60 && total >= 200) || (maths +
physics >= 150)) {
cout << "The candidate is eligible for admission." << endl;
} else {
cout << "The candidate is not eligible for admission." << endl;
}
return 0;
}
Output:-
14. Write a C++ program to enter month number between 1 and 12 and print the correct
month name otherwise print the appropriate error message. (switch – case)
Code:-
#include <iostream>
using namespace std;
int main() {
int month;
cout << "Enter month number (1-12): ";
cin >> month;
switch (month) {
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
case 3:
cout << "March" << endl;
break;
case 4:
cout << "April" << endl;
break;
case 5:
cout << "May" << endl;
break;
case 6:
cout << "June" << endl;
break;
case 7:
cout << "July" << endl;
break;
case 8:
cout << "August" << endl;
break;
case 9:
cout << "September" << endl;
break;
case 10:
cout << "October" << endl;
break;
case 11:
cout << "November" << endl;
break;
case 12:
cout << "December" << endl;
break;
default:
cout << "Error: Invalid month number. Please enter a number between 1 and 12." <<
endl;
break;
}
return 0;
}
Output:-
15. Write a C++ program to enter week day number between 1 and 7 and print the correct
week day name otherwise print the appropriate error message. (switch – case)
Code:-
#include <iostream>
using namespace std;
int main() {
int day;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Error: Invalid week day number. Please enter a number between 1 and 7."
<< endl;
break;
}
return 0;
}
Output:-
16. Write a C++ program to perform any one of operations mentioned below by taking choice
as character: (switch – case)
A - Addition of two numbers
B – Subtraction of two numbers
C - Multiplication of two numbers
D - Division of two numbers
E –Mod between two numbers
Otherwise display the error message.
Code:-
#include <iostream>
using namespace std;
int main() {
char choice;
double num1, num2, result;
switch (choice) {
case 'A':
case 'a':
result = num1 + num2;
cout << "Result of Addition: " << result << endl;
break;
case 'B':
case 'b':
result = num1 - num2;
cout << "Result of Subtraction: " << result << endl;
break;
case 'C':
case 'c':
result = num1 * num2;
cout << "Result of Multiplication: " << result << endl;
break;
case 'D':
case 'd':
if (num2 != 0) {
result = num1 / num2;
cout << "Result of Division: " << result << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl;
}
break;
case 'E':
case 'e':
if (static_cast<int>(num2) != 0) {
int mod_result = static_cast<int>(num1) % static_cast<int>(num2);
cout << "Result of Modulus: " << mod_result << endl;
} else {
cout << "Error: Modulus by zero is not allowed." << endl;
}
break;
default:
cout << "Error: Invalid choice. Please enter a valid operation (A/B/C/D/E)." << endl;
break;
}
return 0;
}
Output:-
17. Write a C++ program for the menu driven which perform any one operation specified
below. (switch – case)
1. Check whether no. is odd or even.
2. Check whether no. is positive, negative or 0.
3. Check whether no. is divisible by 5 or not.
Otherwise display the appropriate error message.
Code:-
#include<iostream>
using namespace std;
int main() {
int choice, num;
switch(choice) {
case 1:
if(num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
break;
case 2:
if(num > 0) {
cout << num << " is positive." << endl;
} else if(num < 0) {
cout << num << " is negative." << endl;
} else {
cout << num << " is zero." << endl;
}
break;
case 3:
if(num % 5 == 0) {
cout << num << " is divisible by 5." << endl;
} else {
cout << num << " is not divisible by 5." << endl;
}
break;
default:
cout << "Invalid choice! Please enter a number between 1 and 3." << endl;
break;
}
return 0;
}
Output:-
18. Write a C++ program to find area or perimeter of a rectangle by asking choice from user.
(switch – case)
Code:-
#include <iostream>
using namespace std;
int main() {
int choice;
double length, width, result;
switch (choice) {
case 1:
result = length * width;
cout << "The area of the rectangle is: " << result << endl;
break;
case 2:
result = 2 * (length + width);
cout << "The perimeter of the rectangle is: " << result << endl;
break;
default:
cout << "Error: Invalid choice. Please enter 1 or 2." << endl;
break;
}
return 0;
}
Output:-
19. Write a C++ program to find square of a number or cube of a number by asking choice
from user. (switch – case)
Code:-
#include <iostream>
using namespace std;
int main() {
int choice;
double number, result;
switch (choice) {
case 1:
result = number * number;
cout << "The square of " << number << " is: " << result << endl;
break;
case 2:
result = number * number * number;
cout << "The cube of " << number << " is: " << result << endl;
break;
default:
cout << "Error: Invalid choice. Please enter 1 or 2." << endl;
break;
}
return 0;
}
Output:-
20. Write a C++ program to read two numbers from the user and read the choice between 1 &
3 and do the following: (switch – case)
1. Greater among of two numbers
2. Smallest among of two numbers
Code:-
#include <iostream>
using namespace std;
int main() {
int num1, num2, choice;
switch (choice) {
case 1:
if (num1 > num2) {
cout << "The greater number is: " << num1 << endl;
} else if (num2 > num1) {
cout << "The greater number is: " << num2 << endl;
} else {
cout << "Both numbers are equal." << endl;
}
break;
case 2:
if (num1 < num2) {
cout << "The smaller number is: " << num1 << endl;
} else if (num2 < num1) {
cout << "The smaller number is: " << num2 << endl;
} else {
cout << "Both numbers are equal." << endl;
}
break;
default:
cout << "Invalid choice! Please choose 1 or 2." << endl;
}
return 0;
}
Output:-