finding out an armstrong number

hi..
i was doing the exercise from ..a website..
I managed to write the code..to find the armstrong number...but the question is actually under the nested loop section

but my program does not have any nested loop..can someone show an alternative..or at least advice..on how I can convert my program to something that uses nested loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int number,total,n,divide,num1=0,modulus,result,result1=0,number_duplicate=0;
	int answer;

	cout<<"Enter the total number of set"<<"\n";
	cin>>total;
	cout<<"Enter the number"<<"\n";
	cin>>number;

	number_duplicate=number;

	for(int i=total;i>0;i--)
	{
		n=i-1;
		answer=static_cast<int>(pow(10.0,n));
		//cout<<answer<<"\n";
		
		divide=number/answer;
		modulus=number%answer;
		number=modulus;
		cout<<divide<<" ";
		
		
			result = divide * divide * divide;
			cout<<result<<"\n";

			result1= result1 + result;
			cout<<result1<<"\n";
			
	}

	cout<<result1<<"\n";
	cout<<number_duplicate<<"\n";

	if(result1==number_duplicate)
	{
		cout<<"You have entered an armstrong number"<<"\n";
	}
	else if (result1!=number_duplicate)
	{
		cout<<"You have NOT ENTERED an armstrong number"<<"\n";
	}


return 0;

Care to explain what "total" is supposed to be?
Perhaps you misunderstood the task and you actually are supposed to find all armstrong numbers?
Hi Athar...

This is the question, An Angstrom number is one whose digits, when cubed, add up to the number itself. For instance, 153 is an Angstrom number, since 13 +53 +33 =153 .
Write a program to input a number, and determine whether it is an Angstrom number or not.
Hint: The modulus operator (%) will be useful here.

And..I needed to input the digits.I used "total" to input the number of digits..and then the digits as the "number".

So. if I know the number of digits..I can tell the program to divide as many times..as the number of digits..
Is it Armstrong or Angstrom?

Anyway, one way you could figure out the number of digits would be dividing it by its base (10, in this case) until the integer part is 0. It'll have as many digits as the number of divisions performed.
Other ways to determine the number of digits is to use log10 or the following method (which is the fastest of these three):

1
2
3
4
5
6
7
8
9
10
11
uint countDigits(uint x)
{
  uint count=1;
  uint value=10;
  while (x>=value)
  {
    value*=10;
    count++;
  }
  return count;
}


By the way, it makes no sense to use nested loops for this problem.
You can of course do this if you really want to...

1
2
3
4
5
6
7
for (int i=0;i<1;i++)
{
  for (int j=0;j<1;j++)
  { 
    for (int k=0;k<1;k++)
    {
       [...]
Last edited on
so..it makes sense..what I did rite...thanks..and also for the tips on how to use log10..
Topic archived. No new replies allowed.