Thread: Need Help with Armstrong number Program

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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