Pls someone should help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pls someone should help

Write a C++ program to tell a user to input a non-negative integer and check if the number is even. If it is even, print "the number is even", if it is odd, print "the number is odd". If the number is even, print the even number from the "user input" down to 1.

9th Jan 2023, 12:20 PM
Al-hassan Rabi'u
Al-hassan Rabi'u - avatar
1 Answer
0
Try this #include <iostream> using namespace std; int main() { // prompt the user to input a non-negative integer int num; cout << "Enter a non-negative integer: "; cin >> num; // check if the number is even or odd if (num % 2 == 0) { cout << "The number is even." << endl; // print the even numbers from the user input down to 1 for (int i = num; i >= 1; i -= 2) { cout << i << " "; } } else { cout << "The number is odd." << endl; } return 0; }
9th Jan 2023, 7:43 PM
Aditya Dixit