Thread: Need Help with Armstrong number Program

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    3

    Need Help with Armstrong number Program

    Hello everyone, im having some problems with a program regarding the Armstrong number. The requirements i have to fill out are:
    -Write a program that finds the Armstrong numbers from 0 to a certain number given by the user in the line of commands;
    -The program must write these numbers on the screen and in a file at my choice;
    -The program must execute in separate functions the following tasks:
    i)Receive the number of algarisms of a certain positive integer (which will be the Armstrong number);
    ii)Test if a given positive integer is or not an Armstrong number.

    I appreciate any help, and here's the program i wrote:
    (Don't mind the attached file, i don't if it is working or not so)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int nalgarisms(int p)
    {
        int n=0;
        while(p > 0)
        {
            p=p/10;
            ++n;
        }
        return n;
    }
    
    
    int armstrongtest(int p1)
    {
        int remainder,result,n,p2,test;
        n=0;
        result=0;
        p2=p1;
    
    
        while(p2>0)
        {
            p2=p2/10;
            ++n;
        }
    
    
     while(p1 > 0)
     {
         remainder=p1%10;
         resul+=pow(remainder,n);
         p1=p1/10;
     }
     if(p1 == result)
     {
         test=1;
     }
     else
     {
        test=0;
     }
        return test;
    }
    
    
    
    
    int main(int argc, char **argv)
    {
        int x,i,n;
        FILE *f1;
        f1=fopen("Armstrong.txt","wt");
        i=0;
       while(i<= argv[1])
       {
           n=armstrongtest(i);
           if(n==1)
           {
               x=nalgarisms(i);
               printf("The number %d is an Armstrong number, and it has %d algarisms.\n",i,x);
               fprintf(f1,"%d\n",i);
           }
           ++i;
       }
        fclose(f1);
        return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 11-12-2017 at 12:31 PM. Reason: Added code tags, learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > while(i<= argv[1])
    Well your first problem is that i is an integer, and argv[1] is a string.

    You should convert argv[1] into an integer for meaningful comparisons.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    3
    Pardon my question, but how can i convert argv[1] into an integer?
    Last edited by QuantumStrix; 11-12-2017 at 12:42 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    actually you should be using argc and not argv for your while loop.

    argc = 0,1,2,3,4,5,6,7 etc ........

    Code:
     // this will cause you trouble so use this instead
    while ( i  =< argc ) 
    // because zero ( 0 ) is your program name.
    while ( i < argc) 
    {
      // then use argv for your data comparison
     // or whatever you need to  do with it
    
    i++;
    }
    str to int is atoi function.

    so what do you think sending i into your function will be giving it?
    Code:
     n=armstrongtest(i); // <-- i = what?
    this is your first time using argc, and argv isn't it?

    run this program putting values or typing stilly stuff on your command line to see what argc and argv are used for.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    int main (int argc, const char **argv)
    {
    
      int i = 0;
      for ( i = 0; i < argc; i++)
            printf("argc = %d ; argv = %s\n", i, argv[i]);
    
    return 0 ;
    }
    when a function does a return it exits that function so your function ending can be written like this as well
    Code:
    if(p1 == result) {
         return 1;
    
     }
    
     else
    
     {
    
        return 0;
    
     }
    
      // your compiler might complain so maybe you'll 
       // have to still add this even though  it should not even reach here
    
        return -1 ;  to check if return == -1 it failed , or whatever your heart desires. 
    
    }
    but if I just happened to get your logic backwards, str to int is atoi(str);
    Code:
    while(i<= atoi(argv[1]))
    google say these are some of the Armstrong numbers whereas 10 is not one of them, as well as something else I pointed out about what you're sending in your function.

    my results I get are:
    Code:
    userx@slackwhere:~/bin
    $ ./temp 10  
    The number 0 is an Armstrong number, and it has 0 algarisms.
    The -digit numbers equal to the sum of th powers of their digits (a finite sequence) are called Armstrong numbers or plus perfect number and are given by 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, ... (OEIS A005188).
    just so you know what is ahead of you.
    Last edited by userxbw; 11-12-2017 at 02:50 PM.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    3
    Wow thanks a lot! This actually helps out, makes more sense now. Need to still figure some things out, but this will do. Thank you.

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by QuantumStrix View Post
    Wow thanks a lot! This actually helps out, makes more sense now. Need to still figure some things out, but this will do. Thank you.
    you're welcome !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. armstrong no.
    By sachin@123 in forum C Programming
    Replies: 1
    Last Post: 03-27-2016, 05:40 PM
  2. Help in find armstrong number
    By san12345 in forum C Programming
    Replies: 1
    Last Post: 12-03-2015, 11:46 AM
  3. Logical error in Armstrong number code
    By dumb09 in forum C Programming
    Replies: 5
    Last Post: 10-21-2014, 05:18 PM
  4. armstrong nos.
    By joybanerjee39 in forum C Programming
    Replies: 6
    Last Post: 11-16-2011, 10:48 AM
  5. having trouble with armstrong program code
    By cooldude in forum C Programming
    Replies: 2
    Last Post: 09-06-2009, 11:58 PM

Tags for this Thread