🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Determining if a number is odd or even is a fundamental programming task that is often used in a variety of applications. TypeScript, being a superset of JavaScript, provides us with the tools necessary to accomplish this task effectively.
2. Program Overview
This program will contain a function named checkOddEven which will take a number as its parameter. It will return a string stating whether the number is 'Odd' or 'Even'.
3. Code Program
// Define the function with TypeScript type annotations
function checkOddEven(num: number): string {
if (num % 2 === 0) {
return 'Even';
} else {
return 'Odd';
}
}
// Test the function
const numberToCheck = 8;
const result = checkOddEven(numberToCheck);
console.log(`${numberToCheck} is an ${result} number.`);
Output:
8 is an Even number.
4. Step By Step Explanation
We use the modulus operator (%) to find the remainder of the division of the number by 2. If a number is divisible by 2 with no remainder (i.e., the remainder is 0), then it's an even number.
For our example:
8 divided by 2 = 4 with a remainder of 0.
Hence, 8 is an even number.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment