Rewrite the following statement using if-else statement: (3)cout << ( ( (num % 2) = = 0) ? "Even \ n" : "Odd \ n")
Question
Rewrite the following statement using if-else statement: (3)cout << ( ( (num % 2) = = 0) ? "Even \ n" : "Odd \ n")
Solution
To rewrite the given statement using an if-else statement, follow these steps:
- Declare a variable
numand assign it a value. - Use an if-else statement to check if
numis even or odd. - Inside the if block, print "Even" using the
coutstatement ifnumis even. - Inside the else block, print "Odd" using the
coutstatement ifnumis odd.
Here is the code:
#include <iostream>
using namespace std;
int main() {
int num = 5; // Assign a value to num
if (num % 2 == 0) {
cout << "Even" << endl;
} else {
cout << "Odd" << endl;
}
return 0;
}
In this code, we check if num is divisible by 2 using the modulo operator %. If the remainder is 0, it means num is even, so we print "Even". Otherwise, we print "Odd".
Similar Questions
Single File Programming QuestionProblem StatementAmil, a curious coder, has a distinctive approach to handling numerical input. Write a program to get an integer from the user. If the number is even, the system employs a goto statement to a label indicating an affirmative response. Otherwise, another goto statement conveys a negative response.Input format :The input consists of a single integer n.Output format :The output displays one of the following:If n is an even number, it displays "You entered an even number."If n is an odd number, it displays "You entered an odd number."Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1 ≤ n ≤ 100Sample test cases :Input 1 :1Output 1 :You entered an odd number.Input 2 :100Output 2 :You entered an even number.Input 3 :67Output 3 :You entered an odd number.
If num is an integer variable, what does the expression num % 2 == 0 ? "Even" : "Odd" represent?Marks : 1Negative Marks : 0Answer hereCheck if num is even and assign the result to a string.Check if num is odd and assign the result to a string.Concatenate "Even" or "Odd" to the value of num.None of the mentioned options
Single File Programming QuestionProblem StatementManoj wants to explore numbers. The number should be even and also a multiple of 10. Write a program to obtain a number x and check if it is even or not. If even, check whether it is a multiple of 10 or not.Function Specifications:void iseven(int x) - Prints whether the input number is even or not.void ismultiple(int x) - Prints whether the input number is a multiple of 10 or not.Input format :The input consists of an integer x.Output format :The first line displays "Even" if x is even or "Not even" otherwise.If x is even, the second line displays "Multiple of 10" if x is a multiple of 10 or "Not a multiple of 10" otherwise.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ x ≤ 106Sample test cases :Input 1 :50Output 1 :EvenMultiple of 10Input 2 :13Output 2 :Not evenInput 3 :1Output 3 :Not evenInput 4 :1456Output 4 :EvenNot a multiple of 10Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
#include <iostream>using namespace std;int main(){ int n; cin >> n; if(n % 2 == 0) cout << n << endl; else cout << n * 2 << endl; return 0;}
Given an integer, , perform the following conditional actions:If is odd, print WeirdIf is even and in the inclusive range of to , print Not WeirdIf is even and in the inclusive range of to , print WeirdIf is even and greater than , print Not WeirdInput Format
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.