Do you think IIT Guwahati certified course can help you in your career?
No
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.
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)
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
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.
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.
Lastly, we will compare temp and sum. The number will be declared as Armstrong if they are equal.
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
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: