Artikel Terkait Tutorial C
To understand this example, you should have knowledge of following C++ programming topics:
Integers which are perfectly divisible by 2 are called even numbers. And those integers which are not perfectly divisible by 2 are not known as odd number. To check whether an integer is even or odd, the remainder is calculated when it is divided by 2 using modulus operator %. If remainder is zero, that integer is even if not that integer is odd.
You can also use ternary operators ?: instead of if..else statement. Ternary operator is short hand notation of if...else statement.
OutputEnter an integer: 23In this program, if..else statement is used to check whether
23 is odd.
n%2 == 0
is true or not. If this expression is true, n is even if not n is odd.You can also use ternary operators ?: instead of if..else statement. Ternary operator is short hand notation of if...else statement.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer: ";
cin >> n;
(n%2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
return 0;
}
Check out these related examples :
- C++ program to Check Whether a Number can be Express as Sum of Two Prime Numbers
Tag : Examples C++ Objects and Class, C++ Constructors Tutorials, Coding C++ Objects & Function, Program C++ Operator Overloading
Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :
Rekomendasi Web Hosting
- 20rb perbulan. Diskon hingga 40% kode kupon: MCP Daftar disini (apache).
- 10rb perbulan. Diskon hingga 75% kode kupon: MCP Daftar disini (litespeed).
- 10rb perbulan. Diskon hingga 70% kode kupon: aff-MCP Daftar disini (apache).