0% found this document useful (0 votes)
112 views20 pages

C++ Conditional Programming Examples

c++ programming
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)
112 views20 pages

C++ Conditional Programming Examples

c++ programming
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

C++ Assignment-1

1. A program to check if a person is eligible to vote (age ≥ 18).

SOL: #include <iostream>

using namespace std;

int main() {

int age;

cout << "Enter your age: ";

cin >> age;

if (age >= 18) {

cout << "You are eligible to vote." << endl;

} else {

cout << "You are not eligible to vote." << endl;

return 0;

OUTPUT: Enter your age: 14

You are not eligible to vote.

...Program finished with exit code 0

Press ENTER to exit console.


2. A program to check whether a given number is even or odd.

SOL: #include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number % 2 == 0) {

cout << "The number is even." << endl;

} else {

cout << "The number is odd." << endl;

return 0;

OUTPUT: Enter a number: 5

The number is odd.

...Program finished with exit code 0

Press ENTER to exit console.


3. A program to check whether a given number is positive, negative, or zero.

SOL: #include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter a number: ";

cin >> number;

if (number > 0) {

cout << "The number is positive." << endl;

} else if (number < 0) {

cout << "The number is negative." << endl;

} else {

cout << "The number is zero." << endl;

return 0;

OUTPUT: Enter a number: +5

The number is positive.

...Program finished with exit code 0

Press ENTER to exit console.


4. A student passes an exam if marks ≥ 40, else fails. Write a program to display
result.

SOL: #include <iostream>

using namespace std;

int main() {

int marks;

cout << "Enter your marks: ";

cin >> marks;

if (marks >= 40) {

cout << "Pass" << endl;

} else {

cout << "Fail" << endl;

return 0;

OUTPUT: Enter your marks: 88

Pass

...Program finished with exit code 0

Press ENTER to exit console.


5. Write a program to find the largest of two numbers using if-else.

SOL: #include <iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter two numbers: ";

cin >> num1 >> num2;

if (num1 > num2) {

cout << num1 << " is the largest." << endl;

} else {

cout << num2 << " is the largest." << endl;

return 0;

OUTPUT: Enter two numbers: 25

79

79 is the largest.

...Program finished with exit code 0

Press ENTER to exit console.


6. A shopkeeper gives 10% discount if the purchase amount > 500, otherwise
no discount.

SOL: #include <iostream>

using namespace std;

int main() {

float purchaseAmount, discount = 0, finalAmount;

cin >> purchaseAmount;

if (purchaseAmount > 500) {

discount = purchaseAmount * 0.10;

finalAmount = purchaseAmount - discount;

cout << "Amount: " << purchaseAmount << endl;

cout << "Discount Amount: " << discount << endl;

cout << "Final Amount: " << finalAmount << endl;

return 0;

OUTPUT: 599

Amount: 599

Discount Amount: 59.9

Final Amount: 539.1

...Program finished with exit code 0

Press ENTER to exit console.


7. Write a program to check whether a number is divisible by 5 and 11 or not.

SOL: #include <iostream>

using namespace std;

int main() {

int num;

cin >> num;

if (num % 5 == 0 && num % 11 == 0) {

cout << "Divisible by 5 and 11";

} else {

cout << "Not divisible by 5 and 11";

return 0;

OUTPUT: 110

Divisible by 5 and 11

...Program finished with exit code 0

Press ENTER to exit console.


8. A program to input three numbers and find the largest among them using
nested if-else.

SOL: #include <iostream>

using namespace std;

int main() {

int a, b, c;

cin >> a >> b >> c;

if (a > b) {

if (a > c) {

cout << "Largest is " << a;

} else {

cout << "Largest is " << c;

} else {

if (b > c) {

cout << "Largest is " << b;

} else {

cout << "Largest is " << c;

return 0;

OUTPUT: 2

Largest is 9

...Program finished with exit code 0

Press ENTER to exit console.


9. Write a program to check if a year is leap year or not.

SOL: #include <iostream>

using namespace std;

int main() {

int year;

cin >> year;

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

cout << "Leap year";

} else {

cout << "Not a leap year";

return 0;

OUTPUT: 2016

Leap year

...Program finished with exit code 0

Press ENTER to exit console.


10. Write a program to calculate grade of a student based on marks:

90–100 → A

75–89 → B

50–74 → C

SOL: #include <iostream>

using namespace std;

int main() {

int marks;

cin >> marks;

if (marks >= 90 && marks <= 100) {

cout << "Grade A";

} else if (marks >= 75 && marks <= 89) {

cout << "Grade B";

} else if (marks >= 50 && marks <= 74) {

cout << "Grade C";

} else {

cout << " Fail";

return 0;

OUTPUT: 95

Grade A

...Program finished with exit code 0

Press ENTER to exit console.


11. A traffic police officer checks vehicle speed:

≤ 60 → Safe

61–100 → Warning

100 → Fine

SOL: #include <iostream>

using namespace std;

int main() {

int speed;

cin >> speed;

if (speed <= 60) {

cout << "Safe";

} else if (speed > 60 && speed < 100) {

cout << "Warning";

} else if (speed == 100) {

cout << "Fine";

} else {

cout << "Over Limit";

return 0;

OUTPUT: 77

Warning

...Program finished with exit code 0

Press ENTER to exit console.


12. ATM withdrawal: If amount ≤ balance → “Transaction Successful”, else →
“Insufficient Balance”.
SOL: #include <iostream>
using namespace std;

int main() {
int balance, amount;
cin >> balance >> amount;
if (amount <= balance) {
cout << "Transaction Successful";
} else {
cout << "Insufficient Balance";
}
return 0;
}
OUTPUT: 5500
1100
Transaction Successful
...Program finished with exit code 0
Press ENTER to exit console.
13. An electricity board charges:

 First 100 units → ₹5/unit

 Next 100 units → ₹7/unit

Above 200 units → ₹10/unit


Write a program to calculate bill.
SOL: #include <iostream>
using namespace std;
int main() {
int units;
cin >> units;
int bill = 0;
if (units <= 100) {
bill = units * 5;
} else if (units <= 200) {
bill = 100 * 5 + (units - 100) * 7;
} else {
bill = 100 * 5 + 100 * 7 + (units - 200) * 10;
}
cout << "Bill = " << bill;
return 0;
}
OUTPUT: 45
Bill = 225
...Program finished with exit code 0
Press ENTER to exit console.
14. A cinema hall gives student discount (20%) if age < 18, otherwise charges full
ticket price.
SOL: #include <iostream>

using namespace std;

int main() {

int age;

float price;

cin >> age >> price;

if (age < 18) {

price = price - (price * 0.2);

cout << "Ticket Price = " << price;

return 0;

OUTPUT: 12

4500

Ticket Price = 3600

...Program finished with exit code 0

Press ENTER to exit console.


15. Write a program to check whether the entered character is a vowel or
consonant.
SOL: #include <iostream>
using namespace std;

int main() {
char ch;
cin >> ch;
if (ch == 'a') cout << "Vowel";
else if (ch == 'e') cout << "Vowel";
else if (ch == 'i') cout << "Vowel";
else if (ch == 'o') cout << "Vowel";
else if (ch == 'u') cout << "Vowel";
else if (ch == 'A') cout << "Vowel";
else if (ch == 'E') cout << "Vowel";
else if (ch == 'I') cout << "Vowel";
else if (ch == 'O') cout << "Vowel";
else if (ch == 'U') cout << "Vowel";
else cout << "Consonant";
return 0;
}
OUTPUT: r
Consonant
A
Vowel

...Program finished with exit code 0


Press ENTER to exit console.
16. Write a program to input 3 sides of a triangle and check whether it is valid (sum
of two sides > third).

SOL: #include <iostream>

using namespace std;

int main() {

int a, b, c;

cin >> a >> b >> c;

if (a + b > c && a + c > b && b + c > a) {

cout << "Valid Triangle";

} else {

cout << "Invalid Triangle";

return 0;

OUTPUT: 10

15

20

Valid Triangle

...Program finished with exit code 0

Press ENTER to exit console.


17. Write a program to check if a number is a palindrome (e.g., 121, 1331).

SOL: #include <iostream>

using namespace std;

int main() {

int n, r = 0, x, d;

cin >> n;

x = n;

while (x > 0) {

d = x % 10;

r = r * 10 + d;

x = x / 10;

if (r == n) {

cout << "Palindrome";

} else {

cout << "Not Palindrome";

return 0;

OUTPUT: 7979

Not Palindrome

...Program finished with exit code 0

Press ENTER to exit console.


18. A banking app checks:

If balance < 1000 → Low balance warning

If balance ≥ 1000 and < 5000 → Normal balance

If balance ≥ 5000 → High balance

SOL: #include <iostream>

using namespace std;

int main() {

int balance;

cin >> balance;

if (balance < 1000) {

cout << "Low balance warning";

} else if (balance < 5000) {

cout << "Normal balance";

} else {

cout << "High balance";

return 0;

OUTPUT: 4500

Normal balance

...Program finished with exit code 0

Press ENTER to exit console.


19. A temperature app:

<0°C → Freezing

0–15°C → Cold

16–30°C → Moderate

30°C → Hot

SOL: #include <iostream>


using namespace std;

int main() {
int temp;
cin >> temp;
if (temp < 0) {
cout << "Freezing";
} else if (temp <= 15) {
cout << "Cold";
} else if (temp < 30) {
cout << "Moderate";
} else {
cout << "Hot";
}
return 0;
}

OUTPUT: 14
Cold

...Program finished with exit code 0


Press ENTER to exit console.
20. A marksheet system that checks eligibility for scholarship:

If marks ≥ 90 and attendance ≥ 75% → Eligible

Else → Not eligible

SOL: #include <iostream>


using namespace std;

int main() {
int marks, attendance;
cin >> marks >> attendance;
if (marks >= 90 && attendance >= 75) {
cout << "Eligible";
} else {
cout << "Not eligible";
}
return 0;
}

OUTPUT: 90
89
Eligible

...Program finished with exit code 0


Press ENTER to exit console.

You might also like