Examples and Practice Exercises in Cpp
Examples and Practice Exercises
A. Implementing decision-making logic in C++ programs
Determine if a number entered by the user is positive, negative, or zero:
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number > 0) {
std::cout << "The number is positive.";
}else if (number < 0) {
std::cout << "The number is negative.";
} else {
std::cout << "The number is zero.";
}
return 0;
}
Check if a given year is a leap year or not:
#include <iostream>
int main() {
int year;
std::cout << "Enter a year: ";
std::cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
std::cout << "Leap year!";
} else {
std::cout << "Not a leap year.";
}
return 0;
}
B. Solving problems using conditional statements and operators
Find the maximum of three numbers using if-else statements:
#include <iostream>
int main() {
int a, b, c;
std::cout << "Enter three numbers: ";
std::cin>> a >> b >> c;
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
std::cout << "The maximum number is: " << max;
return 0;
}
Check if a given character is a vowel or a consonant using switch statements:
#include <iostream>
int main() {
char ch;
std::cout << "Enter a character: ";
std::cin>> ch;
switch (tolower(ch)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
std::cout << "Vowel";
break;
default:
std::cout << "Consonant";
}
return 0;
}
These code snippets provide examples and practice exercises for implementing decision-making logic in C++. Use them to practice and reinforce your understanding of conditional statements, comparison, and logical operators. Experiment with different inputs and scenarios to enhance your problem-solving skills using these concepts.
Conclusion
Throughout this guide, we have covered various aspects of decision-making in C++. We explored conditional statements, comparison, logical, and ternary operators. We also discussed best practices and provided examples and practice exercises to reinforce your understanding.
Decision-making is a fundamental aspect of programming. It allows you to control the flow of your code and make your programs dynamic and responsive. Mastering decision-making skills in C++ enables you to write efficient, robust, and flexible code that can handle different scenarios and make intelligent choices based on conditions.
Decision-making skills are honed through practice and exploration. As you continue your programming journey, we encourage you to challenge yourself with more complex decision-making scenarios. Explore advanced techniques like nested conditional statements, compound conditions, and decision trees. This will expand your problem-solving abilities and enhance your overall programming skills.
Remember, decision-making is not only about understanding syntax and constructs; it's about logical thinking, problem-solving, and making informed choices. Embrace the power of decision-making in C++ and continue to sharpen your skills through practice and continuous learning.
Happy coding!