Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Find if a number is Armstrong in Carbon

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

Introduction

There have been many follow-up programming languages for popular languages already widely used. C++ came as a successor to C, and now we have a successor to C++. Carbon is an advanced version of C++ that was launched in July 2022 in Toronto by Google.

Find if a number is Armstrong in Carbon

Introduction to Carbon

Carbon language is said to be a successor to C++. It is an open-source language that is general-purpose and experimental. Though it is not inherited and built from scratch, carbon provides efficient migration and bi-direction from C++. 

The file extension code for Carbon is .carbon, and you can save a new carbon file with this extension. (Example- Coding Ninjas Studio.carbon)

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

Armstrong Numbers

A number whose sum of all digits of a number raised to the power of the number of digits in that number is equal to the number itself is an Armstrong number. 

For example- if we have a three-digit number 153, then the sum of the cube of all the digits (1,5 and 3) must be equal to 153.

Example 1

Input- 153
Explanation- 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Output- 153 is an Armstrong number

Example 2

Input- 1340
Explanation- 1^4 + 3^4 + 4^4 + 0 = 1 + 81 + 256 + 0= 338
Output- 1340 is not an Armstrong number

Finding If a Number is Armstrong in Carbon

Currently, carbon language does not support for loop. So, we will print if a number is Armstrong or not by using the while loop. 

Steps

  1. First, we will take a variable temp and initialize it with n (The initial number); the temp variable will be compared to the sum of the numbers to identify if the number is Armstrong. 
     
  2. Then we will use the while loop to determine that number's last digit. We will divide the number by 10, take the remainder, raise the power to the number of digits in the number, and store the value in sum.
     
  3. Lastly, we will compare temp and sum. The number will be declared as Armstrong if they are equal.
Calculating armstrong number

Code

// Carbon program to check whether the given number is Armstrong or not
package sample api;

// Checking the value of ‘x’ raised to power ‘y’
fn Power(x: i32, y: i32) -> i32 {
    if(y==0) {
        return 1;
    }
    else if(y%2==0) {
        return Power(x,y/2)*Power(x,y/2);
    }
    return x*Power(x,y/2)*Power(x,y/2);
}

// Taking the variable temp 
fn Armstrong(var n:i32) -> bool {
    var digits:i32 = 0;
    var temp:i32 = n;
    while(temp > 0) {
        temp = temp/10;
        digits = digits+1;
    }

// Extracting the last digit of a number n
    temp = n;
    var sum:i32 = 0;
    while(n>0) {
        var lastdig:i32 = n%10;
        n = n/10;
        sum = sum + Power(lastdig, digits);
    }
    return (sum==temp);
}

// Program to check whether the given number is Armstrong or not
fn Main() -> i32 {
  var n:i32 = 1634;
  if(Armstrong(n)) {
      Print("{0} is Armstrong", n);
  }
  else {
      Print("{0} is not Armstrong", n);
  }
  return 0;
}

Output

Output

Frequently Asked Questions

How many Armstrong numbers are there between 1 and 1000?

There are 13 Armstrong numbers up to 1000.

Is Carbon language a good option?

Yes, it can serve as a successor to C++, which users or developers can consider a simple starting point or an entry-level language.

Is Garbage collection supported on Carbon?

Carbon language does not support garbage collection as it is made to be compatible with C++. But it does have destructors.

What will Carbon language be used for?

Carbon language will be used for the performance-sensitive software as it has code that is safe and easy to read.

How can we compile Carbon on windows?

You can use compiler explorer to compile carbon language on windows or install LLVM on windows and then set up the carbon environment.

Conclusion

In this article, we learned about Armstrong numbers and how we can identify them using Carbon language. We hope you understood the problem easily. Now, it is time you practice it and solve more similar problems. Check more related articles below:

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, AWS, and many more! If you wish to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

Topics covered
1.
Introduction
2.
Introduction to Carbon
3.
Armstrong Numbers
3.1.
Example 1
3.2.
Example 2
4.
Finding If a Number is Armstrong in Carbon
4.1.
Steps
4.2.
Code
4.3.
Output
5.
Frequently Asked Questions
5.1.
How many Armstrong numbers are there between 1 and 1000?
5.2.
Is Carbon language a good option?
5.3.
Is Garbage collection supported on Carbon?
5.4.
What will Carbon language be used for?
5.5.
How can we compile Carbon on windows?
6.
Conclusion