3

Alright please go easy. Just learning C++ and first also question here. I've written a program to list all Armstrong numbers below 1000. While I have read the Wikipedia article on narcissistic numbers, I'm only looking for 3-digit ones. Which means I only care for the sum of the cubes of the digits.

It works by executing a for loop for 1 to 1000, checking whether the indexing variable is armstrong or not using a user defined function and printing it if it is. The user defined function works simply by using a while loop to isolate digits and matching the sum of the cubes to the original number. If it is true, then returns 1 otherwise return 0.

The problem is, I'm getting abolutely no numbers in the output. Only the cout statement in void main() appears and the rest is blank. Tried to debug as much as I could. Complier is Turbo C++. Code-

#include<iostream.h>
#include<conio.h>

int chk_as(int);//check_armstrong

void main()
{
    clrscr();
    cout<<"All Armstrong numbers below 1000 are:\n";
    for(int i=1;i<=1000;i++)
    {
        if (chk_as(i)==1)
            cout<<i<<endl;
    }
    getch();
}

int chk_as (int n)
{
    int dgt;
    int sum=0,det=0;//determinant
    while (n!=0)
    {
        dgt=n%10;
        n=n/10;
        sum+=(dgt*dgt*dgt);
    }
    if (sum==n)
    {det=1;}
    else
    {det=0;}
    return det; 
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • 3
    `main` should return `int`. – Shafik Yaghmour Oct 31 '13 at 14:30
  • @ShafikYaghmour It is not mandatory. – Kakalokia Oct 31 '13 at 14:32
  • 2
    @AliAlamiri It certainly is! From N3337, 3.6.1: "An implementation shall not predefine the `main` function. This function shall not be overloaded. **It shall have a return type of type `int`**, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of `main`: `int main() { /* ... */ }` and `int main(int argc, char* argv[]) { /* ... */ }`" – BoBTFish Oct 31 '13 at 14:32
  • Also, `#include` should be `#include `. There is no standard header called `iostream.h`. – anjruu Oct 31 '13 at 14:33
  • main should also take an `int` and a `char**` as params. ;-) – Grimm The Opiner Oct 31 '13 at 14:34
  • 3
    @user1158692 *that* **is** optional by the standard. – WhozCraig Oct 31 '13 at 14:34
  • @BoBTFish if main is declared like "void main", then there's no need of return 0. – Kakalokia Oct 31 '13 at 14:35
  • @AliAlamiri I don't think `void main()` is standards-compliant, actually (http://stackoverflow.com/questions/1621574/mains-signature-in-c) – anjruu Oct 31 '13 at 14:37
  • I think chk_as shold return bool – LeeNeverGup Oct 31 '13 at 14:40
  • Mutating a formal is a bad programming practice because it makes programs difficult to debug. If you had not engaged in this bad practice then you would not have written the bug in the first place. My advice: stop doing that right now. – Eric Lippert Nov 18 '13 at 22:22

4 Answers4

6

The problem is that you are dynamically changing the value of n in your method, but you need its original value to check the result.

Add in a temporary variable, say, t.

int t = n;
while (t!=0)
{
    dgt=t%10;
    t=t/10;
    sum+=(dgt*dgt*dgt);
}
if (sum==n)
// ... etc.
Alex Walker
  • 2,337
  • 1
  • 17
  • 32
  • +1 Note: You don't need the temp. You can do this by setting `sum` equal to `n` on inception, and *subtracting* each term. If after the loop `sum == 0` its valid. – WhozCraig Oct 31 '13 at 14:41
  • Yep, that method works too, I just prefer temporary variables for clarity. Adding this one in meant minimal adjustment to the existing code. – Alex Walker Oct 31 '13 at 14:44
0

EDIT: Nevermind... this was wrong

while (n!=0)
    {
    dgt=n%10;
    n=n/10;
    sum+=(dgt*dgt*dgt);
    }

This runs forever as n never reaches 0.

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46
0

The problem is, that in the end of the loop

while (n!=0)
{
    dgt=n%10;
    n=n/10;
    sum+=(dgt*dgt*dgt);
}

n is 0, so the condition if (sum==n) is never true.

Try something like :

int chk_as (int n)
{
int copy = n;
int dgt;
int sum=0,det=0;//determinant
while (copy!=0)
    {
    dgt=copy%10;
    copy=copy/10;
    sum+=(dgt*dgt*dgt);
    }
if (sum==n)
{det=1;}
else
{det=0;}
return det; 
}
LeeNeverGup
  • 1,096
  • 8
  • 23
0

I have given here the program for finding armstrong number of a three digits number.

The condition for armstrong number is, Sum of the cubes of its digits must equal to the number itself.

For example, 407 is given as input. 4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an armstrong number.

#include <stdio.h>

int main()
{
      int i, a, b, c, d;
      printf("List of Armstrong Numbers between (100 - 999):\n");

      for(i = 100; i <= 999; i++)
      {
            a = i / 100;
            b = (i - a * 100) / 10;
            c = (i - a * 100 - b * 10);

            d = a*a*a + b*b*b + c*c*c;

            if(i == d)
            {
                  printf("%d\n", i);
            }
      }  
      return 0;
}

List of Armstrong Numbers between (100 - 999): 153 370 371 407

Reference: http://www.softwareandfinance.com/Turbo_C/Find_Armstrong_Number.html