0% found this document useful (0 votes)
16 views18 pages

Pratham Pareek BSC Cs Oops Prac 3

The document contains a series of C++ programming exercises focused on conditional statements, including programs to determine leap years, voting eligibility, number properties (odd/even, divisibility), and more. Each exercise is accompanied by code snippets and expected outputs. The topics covered include basic arithmetic operations, grading systems, quadratic equations, and user input validation.

Uploaded by

sohaibdxb080
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)
16 views18 pages

Pratham Pareek BSC Cs Oops Prac 3

The document contains a series of C++ programming exercises focused on conditional statements, including programs to determine leap years, voting eligibility, number properties (odd/even, divisibility), and more. Each exercise is accompanied by code snippets and expected outputs. The topics covered include basic arithmetic operations, grading systems, quadratic equations, and user input validation.

Uploaded by

sohaibdxb080
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
You are on page 1/ 18

PRATHAM PAREEK BSC CS

C++ PRACTICAL 3 - CONDITIONAL STATEMENTS WITH RELATIONAL AND


LOGICAL OPERATORS

1. Write a C++ program that accepts a year and finds whether year entered is a leap year or
not.

CODE: #include <iostream>


using namespace std;

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.

CODE: #include <iostream>


using namespace std;

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:

3. Write a C++ program to check whether number entered is odd or even.

CODE: #include <iostream>


using namespace std;

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:

4. Write a C++ program to check whether number entered is divisible by 3 or not.

CODE: #include <iostream>


using namespace std;

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:

5. Write a C++ program to check whether number entered is divisible by 7 or not.

CODE: #include <iostream>


using namespace std;

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.

CODE: #include <iostream>


using namespace std;

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.

CODE: #include <iostream>


using namespace std;

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:

8. Write a C++ program to check greater among two numbers.


Code:-
#include <iostream>
using namespace std;

int main() {
// Declare two variables
int num1, num2;

// Input two numbers from the user


cout << "Enter the first number: ";
cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

// Compare the numbers and output the result


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;
}
return 0;
}
Output:-

9. Write a C++ program to check greater among three numbers.


Code:-
#include <iostream>
using namespace std;

int main() {
int num1, num2, num3;
cout << "Enter the first number: ";
cin >> num1;

cout << "Enter the second number: ";


cin >> num2;

cout << "Enter the third number: ";


cin >> num3;
if (num1 >= num2 && num1 >= num3) {
cout << "The greatest number is: " << num1 << endl;
} else if (num2 >= num1 && num2 >= num3) {
cout << "The greatest number is: " << num2 << endl;
} else {
cout << "The greatest number is: " << num3 << endl;
}

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;

cout << "Enter the marks of the student: ";


cin >> marks;

if (marks > 80) {


cout << "Grade: O" << endl;
} else if (marks >= 70 && marks <= 79) {
cout << "Grade: A+" << endl;
} else if (marks >= 60 && marks <= 69) {
cout << "Grade: A" << endl;
} else if (marks >= 55 && marks <= 59) {
cout << "Grade: B+" << endl;
} else if (marks >= 50 && marks <= 54) {
cout << "Grade: B" << endl;
} else if (marks >= 45 && marks <= 49) {
cout << "Grade: C" << endl;
} else if (marks >= 40 && marks <= 44) {
cout << "Grade: D" << endl;
} else {
cout << "Grade: Fail" << endl;
}

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;

cout << "Enter Customer Number: ";


cin >> customerNumber;
cout << "Enter Units Consumed: ";
cin >> unitsConsumed;

if (unitsConsumed <= 100) {


totalAmount = 100;
}
else if (unitsConsumed <= 200) {
totalAmount = 100 + (unitsConsumed - 100) * 5;
}
else if (unitsConsumed <= 400) {
totalAmount = 200 + (unitsConsumed - 200) * 10;
}
else if (unitsConsumed <= 600) {
totalAmount = 250 + (unitsConsumed - 400) * 15;
}
else {
totalAmount = 300 + (unitsConsumed - 600) * 20;
}

cout << "Customer Number: " << customerNumber << endl;


cout << "Total Amount to be paid: Rs. " << totalAmount << endl;

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;

cout << "Enter marks for Maths (out of 100): ";


cin >> maths;
cout << "Enter marks for Physics (out of 100): ";
cin >> physics;
cout << "Enter marks for Chemistry (out of 100): ";
cin >> chemistry;

total = maths + physics + chemistry;

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;

cout << "Enter week day number (1-7): ";


cin >> 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;

cout << "Enter your choice of operation: \n";


cout << "A - Addition\n";
cout << "B - Subtraction\n";
cout << "C - Multiplication\n";
cout << "D - Division\n";
cout << "E - Modulus\n";
cout << "Enter choice (A/B/C/D/E): ";
cin >> choice;

cout << "Enter first number: ";


cin >> num1;
cout << "Enter second number: ";
cin >> num2;

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;

cout << "Menu: \n";


cout << "1. Check whether number is odd or even\n";
cout << "2. Check whether number is positive, negative or 0\n";
cout << "3. Check whether number is divisible by 5\n";
cout << "Enter your choice (1-3): ";
cin >> choice;

cout << "Enter a number: ";


cin >> 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;

cout << "Choose an option to calculate:\n";


cout << "1 - Area of the rectangle\n";
cout << "2 - Perimeter of the rectangle\n";
cout << "Enter your choice (1 or 2): ";
cin >> choice;

cout << "Enter the length of the rectangle: ";


cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;

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;

cout << "Choose an option to calculate:\n";


cout << "1 - Square of a number\n";
cout << "2 - Cube of a number\n";
cout << "Enter your choice (1 or 2): ";
cin >> choice;

cout << "Enter the number: ";


cin >> number;

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;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

cout << "Choose an option:\n";


cout << "1. Greater among the two numbers\n";
cout << "2. Smaller among the two numbers\n";
cout << "Enter your choice (1 or 2): ";
cin >> 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:-

You might also like