I want o/p for variable i/p numbers(no: of digits>5).How could I do that

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishnanand oc
    New Member
    • Sep 2010
    • 6

    I want o/p for variable i/p numbers(no: of digits>5).How could I do that

    ***ARMSTRONG NUMBER CHECKING***
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,d,b,c=0,e;//i'm getting output for 4digit nos and less
    clrscr();//i want output for 5 digits and more
    printf("Enter the number\n");//i used long but the o/p is wrong
    scanf("%d",&a);//I think i dont get something correct for my wants
    d=a;//someone help me
    e=nod(a);
    do
    {
    b=a%10;
    c+=pwr(b,e);
    a=a/10;
    }while(a>0);
    if(c==d)
    printf("%d is an armstrong number\n",c);
    else
    printf("%d is not an armstrong number",d);
    getch();
    }
    
    
    pwr(int a,int b)
    {
     int c,d;
     c=1;
     for(d=0;d<b;d++)
     {
     c=c*a;
     }
     return(c);
    }
    
    
    
    
    
    int nod(int a)
    {
     int c=0;
     while(a>0)
     {
     c++;
     a=a/10;
     }
     return(c);
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    i used long but the o/p is wrong
    The code you posted does not use long. How exactly did you use long before?

    Comment

    • krishnanand oc
      New Member
      • Sep 2010
      • 6

      #3
      like this

      I just replaced the data type int inside every function
      with long int .And the o/p went wrong.So then I decided to
      use int itself .By using that way I'll get output for atleast 4 digit numbers.

      When I tried to replace int from function names nod & pwr with long int compiler showed error as type mismatch.

      I also replaced int from parameters with long int but the o/p was wrong

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Did you change the scanf and printf format strings to tell those functions that you were using long? That is, change "%d" to "%ld".

        Comment

        • krishnanand oc
          New Member
          • Sep 2010
          • 6

          #5
          Yes It worked and thank.But there seems to be another new problem.Please look into this code
          Code:
          #include<stdio.h>
          #include<conio.h>
          void main()
          {
          unsigned long int a,d,c=0;
          unsigned int b,e;
          clrscr();
          printf("Enter the number\n");
          scanf("%ld",&a);
          d=a;
          printf("\nd=%ld",d);
          e=nod(a);
          printf("\ne=%d",e);
          while(a>0)
          {
          b=a%10;
          c+=pwr(b,e);
          printf("\nc=%ld\n",c);
          a=a/10;
          }
          if(c==d)
          printf("\n%ld is an armstrong number\n",c);
          else
          printf("\n%ld is not an armstrong number",d);
          getch();
          }
          
          nod(long int a)
          {
           int c=0;
           while(a>0)
           {
           c++;
           a=a/10;
           }
           return(c);
          }
          
          pwr(int a,int b)
          {
          
           long int c=1;
           int d;
           for(d=0;d<b;d++)
           {
           c=c*a;
           }
           return(c);
          }
          As usual the i/p for upto 9474 works fine But for 54748
          and 92727 the story is different Please see the o/p shown below
          Enter the number
          54748

          d=54748
          e=5
          c=-32768
          c=-31744
          c=-14937
          c=-13913
          c=-10788

          54748 is not an armstrong number
          ***
          Enter the number
          92727

          d=92727
          e=5
          c=16807
          c=16839
          c=33646
          c=33678
          c=27191

          92727 is not an armstrong number

          For the second input it can be seen that upto 4 times
          the pgm works fine.For the fifth time the situation changes.
          why this happens.Please help
          Last edited by krishnanand oc; Sep 10 '10, 02:25 PM. Reason: grammer mistakes

          Comment

          • krishnanand oc
            New Member
            • Sep 2010
            • 6

            #6
            I think the problem is with pwr function .
            but i don't know what it is please help

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You are sloppy with the types you pass to and from functions nod and pwr. Put function prototypes for these functions at the top of the file and then fix the subsequent compiler errors.

              Some of the type sloppiness may not be illegal, so compare the definitions of nod and pwr against where you call them. Look at the types of the passed arguments; look at the types of the variables that you assign the return values to.

              You need to specify the return type of pwr.

              By the way, the C Standard mandates that main return an int.

              Comment

              • krishnanand oc
                New Member
                • Sep 2010
                • 6

                #8
                Hi it solved the problem.Cool
                Is there any way to manipulate numbers which crosses the limit for long.For Eg:4679307774
                Thank you very much

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  C99 introduced the long long type.

                  Comment

                  • krishnanand oc
                    New Member
                    • Sep 2010
                    • 6

                    #10
                    okay is it possible with turbo c

                    Comment

                    Working...