CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 10
10.
Write a program to check whether the given number is even or odd (using ? : ternary operator)
Ternary operator is alive and well with this exercise. Unlike the previous assignment, I felt that this is a much better use case for a ternary operator. I say this because numbers can only be either one of two types, odd or even. Luckily, zero is classified as an even number so I no longer have any gripes with the assignment.
Nevertheless, for this assignment I had to use the modulo or modulus operator. Essentially, it is the process of division and presenting the remainder. For example, 5/2 would equate to 2 with a remainder of 1. The quickest way to accomplish this would be 5 mod 2 or in the case of C++ 5%2. With that said, below is the source code for this assignment.
Below is the output. The program is able to successfully determine if the number is either odd or even.
Admittedly, I am becoming more confident with coding. It is only day 2 and I have completed all 10 assignments. This concludes Variable, Operator & Expression Assignment Set 1. Onward to Assignment Set 2.
Ternary operator is alive and well with this exercise. Unlike the previous assignment, I felt that this is a much better use case for a ternary operator. I say this because numbers can only be either one of two types, odd or even. Luckily, zero is classified as an even number so I no longer have any gripes with the assignment.
Nevertheless, for this assignment I had to use the modulo or modulus operator. Essentially, it is the process of division and presenting the remainder. For example, 5/2 would equate to 2 with a remainder of 1. The quickest way to accomplish this would be 5 mod 2 or in the case of C++ 5%2. With that said, below is the source code for this assignment.
Below is the output. The program is able to successfully determine if the number is either odd or even.
Admittedly, I am becoming more confident with coding. It is only day 2 and I have completed all 10 assignments. This concludes Variable, Operator & Expression Assignment Set 1. Onward to Assignment Set 2.
Comments
Post a Comment