Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Carbon Programming Language
3.
Features of Carbon
4.
Problem Statement
4.1.
Explanation
5.
Modulo Operator
5.1.
Points to Remember About Modulo Operator
6.
Solution Using Modulo Operator
6.1.
Algorithm
6.2.
Implementation
6.2.1.
Input
6.2.2.
Output
6.2.3.
Input
6.2.4.
Output
7.
Complexities
7.1.
Time Complexity
7.2.
Space Complexity
8.
Bitwise Operators
8.1.
Bitwise AND operator
8.2.
Using Bitwise AND
8.3.
Algorithm 
8.4.
Implementation
8.4.1.
Input
8.4.2.
Output
8.4.3.
Input
8.4.4.
Output
9.
Complexities
9.1.
Time Complexity
9.2.
Space Complexity
10.
Frequently Asked Questions
10.1.
Who is the creator of the Carbon Programming language?
10.2.
Is Carbon better than C++?
10.3.
What is the Carbon Programming language used for?
10.4.
Is Carbon capable of compiling C++ code?
10.5.
Is Carbon an interpreted language?
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

Program To Find if Number is Even or Odd in Carbon

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninja! So you’re learning to code in Carbon Language? That’s great! We've got a simple yet exciting problem for you to check out. Keep reading to find out. After reading this write-up, you will be able to write a program that can determine whether a given number is even or odd in Carbon. We will focus on two ways to solve this problem in detail.

Introduction

First of all, let’s learn about this new programming language in the market, i.e., the Carbon Programming language.

Carbon Programming Language

Carbon was introduced as an open-source, experimental heir to C++. Out of many programming languages available for developers to choose from, C++ is the first choice of developers. It is one of the most preferred languages worldwide. It is used by more than 4.4 million developers around the world. To resolve some of the flaws of C++, Carbon was launched in July 2022. Google initiated this project. It is currently accepting contributions by developers on GitHub. You must be wondering why Google decided to introduce a totally new language instead of improving C++.

The reason is that making changes to C++ is quite hard, even for a big firm like Google. This is due to the focus on backward compatibility, the tight governance behind the language, and a long approval process that might take years. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Features of Carbon

Some of the features of the Carbon Programming language are listed below:

  • Like its predecessor (C++), Carbon is intended to provide performance advantages through a Low-level virtual machine ( LLVM ).
     
  • Carbon can invoke C++ language code and vice versa.
     
  • It supports the migration of code from C++ to Carbon.
     
  • Carbon allows developers to create fast and scalable builds.
     
  • The entire language is publicly accessible via GitHub, where anyone may contribute to the Program focused on their aims and priorities.
     

Now let's come to our problem statement.

Problem Statement

You need to write a function to find whether a number is even or odd in Carbon Programming language.

Explanation

A number "n" is called even only when it is completely divisible by 2. If n divided by 2 gives any number other than zero as the remainder, then n will be called an odd number.

odd and even numbers 

Using the modulo operator is one solution to this problem. Let’s look at the function of the modulo operator. 

Modulo Operator

The symbol of the modulo operator is “ %. “ It is one of the arithmetic operators. We use this operator to get the remainder after dividing two numbers. The modulo operator cannot be used with floating-point numbers. It is limited to being used with integers. To understand how it works, let z and be two integers. To get the remainder of the division of z by x, we will use the modulo operator, like z%x.

For example

8 % 4 = 0
5 % 2 = 1
2 % 3 = 2

Points to Remember About Modulo Operator

Let p and q be two integer numbers. You have to keep these points in mind when using this operator:

  • p % q = p, if p < q.
     
  • p % q = 0 if p is completely divisible by q.
     
  • If p is not completely divisible by q, p % q will give the remainder of the division of p by q.
     
  • If q = 0, p % q will result in a compile-time error, as division by 0 is not possible.
     

Now that we know how to use the modulo operator let us come to the solution to our problem.

Solution Using Modulo Operator

Algorithm

To find if a number is even or odd in Carbon using the modulo operator, we will follow the following algorithm:

  1. Input a number.
     
  2. If number % 2 == 0, then return true, which means the number is even.
     
  3. Else return false, which means the number is odd.


Look at this flowchart to understand the algorithm better:

flowchart for finding out odd and even numbers using modulo

Now let's look at the implementation of this algorithm to find if a number is even or odd in the Carbon language

Implementation


// Program to check if a number is even or odd in Carbon using modulo  

package sample api;
 
// Function definition
fn IsEven(var x: i32) -> bool {
    if(x%2==0) {
    
       // If the number is even, the function returns true
        return true;
    }

    // If the number is odd
    return false;
}
 
// Main code
fn Main() -> i32 {
  var x:i32 = 21424;

  // Function call 
  if(IsEven(x)) {
 
      // Print “even” if the function returns true
      Print("Even");
  }
  else {

      // Print “odd” if the function returns false
      Print("Odd");
  }
  return 0;
}

Input

24736

Output

output: even, using modulo

Input

25321

Output

Output: odd, using modulo

Complexities

Time Complexity

O(1) is the time complexity.

Reason: It is because this code takes constant time to check the conditions and determine if the number is even or odd in Carbon.

Space Complexity

O( 1) is the space complexity.

Reason: We are not using any extra space. As a result, the space complexity remains O(1).

Another approach to solving this problem is using the bitwise AND operator. Before looking at the solution, let us understand the bitwise operators.

Read More - Time Complexity of Sorting Algorithms

Bitwise Operators

In contrast to standard logical operators (such as +, -, *), which function on bytes or groups of bytes, the bitwise operators may verify or set each bit within a byte. Overflow is never caused by bitwise operators. This is because the result always lies within the acceptable limits for the numeric type concerned.

The bitwise operators used in Carbon are as follows:

  • AND
     
  • OR
     
  • XOR
     
  • NOT
     
  • Left shift
     
  • Right shift


Our solution involves the use of the bitwise AND operator. So, let us find out what it does.

Bitwise AND operator

The bitwise AND (& ) operator in Carbon accepts two operands. It performs an AND operation on each bit of the two numbers. The result of the & operation is 1 only if both bits of the two operands are 1. 

Now let us find out how we can use this operator to find out if the numbers are even or odd in Carbon. 

Using Bitwise AND

In the binary representation, an odd number's least significant bit (LSB) is always 1. Whereas the LSB of an even number is always 0. To find out if a number is even or odd in Carbon, we need to determine whether the LSB of the number is set (1) or reset (0). We will use the bitwise AND operator for the same. Suppose we want to check if an input number (num) is odd or even in Carbon. We will perform the AND operation on num with 1 (num & 1). If the AND operation results in 1, the number is odd. Otherwise, the number is even.

explanation of finding odd and even numbers using bitwise AND

Let us look at the algorithm that we will use to implement this approach.

Algorithm 

The algorithm to find if a number is odd or even in Carbon using bitwise AND is as follows:

  1.  Input a number
     
  2. If number & 1 = 1, then return true, which means that the number is odd.
     
  3. Else, return false, which means the number is even.
     

Look at the following flowchart to understand the algorithm better.

flow chart to find odd and even numbers using bitwise AND

Now let us look at the Program.

Implementation


// Program to find if a number is odd or even in Carbon using &

package sample api;

// Main function
fn Main() -> i32 {
  var x:i32 = 21424;

  // Checking the result after bitwise AND
  if(x&1 == 1) {

      // Print “odd” if bitwise & results 1
      Print("Odd");
  }
  else {
 
      // Print “even” if bitwise & results 0
      Print("Even");
  }
  return 0;
}

Input

12431

Output

output: odd, bitwise AND

Input

12234

Output

Output

Complexities

Time Complexity

O(1) is the time complexity.

Reason: It is because this code takes constant time to check the conditions and determine if the number is even or odd in Carbon.

Space Complexity

O(1) is the space complexity.

Reason: We are not using any extra space. As a result, the space complexity remains O(1).

Now let us answer some FAQs.

Frequently Asked Questions

Who is the creator of the Carbon Programming language?

Carbon was initially announced by Google developer “Chandler Carruth” in July 2022 at the CppNorth conference in Toronto.

Is Carbon better than C++?

Carbon is the successor to C++, which was made to overcome its flaws. Carbon is currently in its experimental phase, so we cannot say that it is better than C++.

What is the Carbon Programming language used for?

Carbon is intended to promote software and language evolution while creating performance-critical software. Another primary purpose is to create code that is simple to read, comprehend, and write.

Is Carbon capable of compiling C++ code?

Carbon is intended to be compatible with C++ Programs and to facilitate migration. The Carbon toolchain will be able to compile C++ code.

Is Carbon an interpreted language?

Carbon is a bytecode-interpreted scripting language with smart pointers for safe memory deallocation.

Conclusion

We understood the problem of finding out even and odd numbers in the Carbon Programming language. We also implemented the code using two different approaches.

We hope this article helps you on your journey. You can try solving another problem of Finding Fibonacci numbers in Carbon or Finding if a number is Armstrong in Carbon. You can refer to these articles related to Carbon by Coding Ninjas.

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head to our practice platform, Coding Ninjas Studio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Happy coding, Ninja!

Live masterclass