By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 2,037 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

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

P: 6
***ARMSTRONG NUMBER CHECKING***
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int a,d,b,c=0,e;//i'm getting output for 4digit nos and less
  6. clrscr();//i want output for 5 digits and more
  7. printf("Enter the number\n");//i used long but the o/p is wrong
  8. scanf("%d",&a);//I think i dont get something correct for my wants
  9. d=a;//someone help me
  10. e=nod(a);
  11. do
  12. {
  13. b=a%10;
  14. c+=pwr(b,e);
  15. a=a/10;
  16. }while(a>0);
  17. if(c==d)
  18. printf("%d is an armstrong number\n",c);
  19. else
  20. printf("%d is not an armstrong number",d);
  21. getch();
  22. }
  23.  
  24.  
  25. pwr(int a,int b)
  26. {
  27.  int c,d;
  28.  c=1;
  29.  for(d=0;d<b;d++)
  30.  {
  31.  c=c*a;
  32.  }
  33.  return(c);
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40. int nod(int a)
  41. {
  42.  int c=0;
  43.  while(a>0)
  44.  {
  45.  c++;
  46.  a=a/10;
  47.  }
  48.  return(c);
  49. }
  50.  
Sep 9 '10 #1

✓ answered by donbock

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.

Share this Question
Share on Google+
9 Replies


Expert 100+
P: 2,398
i used long but the o/p is wrong
The code you posted does not use long. How exactly did you use long before?
Sep 9 '10 #2

P: 6
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
Sep 10 '10 #3

Expert 100+
P: 2,398
Did you change the scanf and printf format strings to tell those functions that you were using long? That is, change "%d" to "%ld".
Sep 10 '10 #4

P: 6
Yes It worked and thank.But there seems to be another new problem.Please look into this code
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6. unsigned long int a,d,c=0;
  7. unsigned int b,e;
  8. clrscr();
  9. printf("Enter the number\n");
  10. scanf("%ld",&a);
  11. d=a;
  12. printf("\nd=%ld",d);
  13. e=nod(a);
  14. printf("\ne=%d",e);
  15. while(a>0)
  16. {
  17. b=a%10;
  18. c+=pwr(b,e);
  19. printf("\nc=%ld\n",c);
  20. a=a/10;
  21. }
  22. if(c==d)
  23. printf("\n%ld is an armstrong number\n",c);
  24. else
  25. printf("\n%ld is not an armstrong number",d);
  26. getch();
  27. }
  28.  
  29. nod(long int a)
  30. {
  31.  int c=0;
  32.  while(a>0)
  33.  {
  34.  c++;
  35.  a=a/10;
  36.  }
  37.  return(c);
  38. }
  39.  
  40. pwr(int a,int b)
  41. {
  42.  
  43.  long int c=1;
  44.  int d;
  45.  for(d=0;d<b;d++)
  46.  {
  47.  c=c*a;
  48.  }
  49.  return(c);
  50. }
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
Sep 10 '10 #5

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

Expert 100+
P: 2,398
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.
Sep 10 '10 #7

P: 6
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
Sep 11 '10 #8

Expert 100+
P: 2,398
C99 introduced the long long type.
Sep 11 '10 #9

P: 6
okay is it possible with turbo c
Sep 11 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.