0% found this document useful (0 votes)
5 views5 pages

C++ Conditional Statement Examples

The document contains C++ programs demonstrating various conditional statements, including finding the maximum of three numbers, checking if a number is positive, negative, or zero, and determining if a year is a leap year. It also includes programs for checking divisibility, even or odd status, character types, calculating CGPA, and computing electricity bills. Each program is structured with input prompts and conditional logic to produce the desired outputs.

Uploaded by

faleca6418
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

C++ Conditional Statement Examples

The document contains C++ programs demonstrating various conditional statements, including finding the maximum of three numbers, checking if a number is positive, negative, or zero, and determining if a year is a leap year. It also includes programs for checking divisibility, even or odd status, character types, calculating CGPA, and computing electricity bills. Each program is structured with input prompts and conditional logic to produce the desired outputs.

Uploaded by

faleca6418
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Conditional Statement Programs

1.2.1 Maximum between three numbers

#include <iostream>
using namespace std;

int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Maximum: " << a;
else if (b >= a && b >= c)
cout << "Maximum: " << b;
else
cout << "Maximum: " << c;
return 0;
}

1.2.2 Check positive, negative, or zero

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << "Positive";
else if (num < 0)
cout << "Negative";
else
cout << "Zero";
return 0;
}

1.2.3 Check if divisible by 5 and 11

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 5 == 0 && num % 11 == 0)
cout << "Divisible by both 5 and 11";
else
cout << "Not divisible by both 5 and 11";
return 0;
}
1.2.4 Check even or odd

#include <iostream>
using namespace std;

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

1.2.5 Check leap year

#include <iostream>
using namespace std;

int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
cout << "Leap Year";
else
cout << "Not a Leap Year";
return 0;
}

1.2.6 Check if character is alphabet

#include <iostream>
using namespace std;

int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
cout << "Alphabet";
else
cout << "Not an Alphabet";
return 0;
}

1.2.7 Check if vowel or consonant

#include <iostream>
using namespace std;

int main() {
char ch;
cout << "Enter an alphabet: ";
cin >> ch;
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
cout << "Vowel";
else if ((ch >= 'a' && ch <= 'z'))
cout << "Consonant";
else
cout << "Not an Alphabet";
return 0;
}

1.2.8 Alphabet, digit, or special character

#include <iostream>
using namespace std;

int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
cout << "Alphabet";
else if (ch >= '0' && ch <= '9')
cout << "Digit";
else
cout << "Special Character";
return 0;
}

1.2.9 Calculate CGPA

#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter number of courses: ";
cin >> n;
float total_points = 0, total_credits = 0;
for (int i = 0; i < n; i++) {
float marks, credits, grade_point;
cout << "Enter marks and credit for course " << i + 1 << ": ";
cin >> marks >> credits;

if (marks >= 80) grade_point = 4.0;


else if (marks >= 75) grade_point = 3.75;
else if (marks >= 70) grade_point = 3.5;
else if (marks >= 65) grade_point = 3.25;
else if (marks >= 60) grade_point = 3.0;
else if (marks >= 55) grade_point = 2.75;
else if (marks >= 50) grade_point = 2.5;
else if (marks >= 45) grade_point = 2.25;
else if (marks >= 40) grade_point = 2.0;
else grade_point = 0.0;
total_points += grade_point * credits;
total_credits += credits;
}

cout << "CGPA = " << total_points / total_credits << endl;


return 0;
}

1.2.10 Days in a given month

#include <iostream>
using namespace std;

int main() {
int month;
cout << "Enter month number (1-12): ";
cin >> month;

switch(month) {
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
cout << "31 days";
break;
case 4: case 6: case 9: case 11:
cout << "30 days";
break;
case 2:
cout << "28 or 29 days";
break;
default:
cout << "Invalid month";
}
return 0;
}

1.2.11 Electricity bill calculation

#include <iostream>
using namespace std;

int main() {
float unit, bill;
cout << "Enter electricity units consumed: ";
cin >> unit;

if (unit <= 50)


bill = unit * 0.50;
else if (unit <= 150)
bill = 50 * 0.50 + (unit - 50) * 0.75;
else if (unit <= 250)
bill = 50 * 0.50 + 100 * 0.75 + (unit - 150) * 1.20;
else
bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (unit - 250) * 1.50;

bill = bill + (bill * 0.20); // 20% surcharge


cout << "Total Electricity Bill = " << bill << " Taka" << endl;
return 0;
}

You might also like