We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,186 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Why my progroma writes in the answer 1 insted of 0

#include <stdio.h>
#include <stdlib.h>

int add(int i, int j , int k)
{
       while ( i != 0)
     {
     
     
     j = i % 10;
     k = k * j;
     i = i / 10;
     }
      return k;
}

int main ()
{
 int num, num2=1, r;
 int ret=1;
 int i=0;
 
scanf("%d",&num); 
 int answer = add(num , r ,num2);

while (num > 0)
{
 num = answer;
answer = add(num , r ,num2);
if (num < 10)
{
         printf("%d  \n ",answer);
         system("PAUSE");
          return 0;
        break;
        }
}
 printf("%d  \n ",answer);
 system("PAUSE");
 return 0;

}
6
Contributors
4
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
6
Views
SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Sorry if i didn't give more information it supposed to multiply digits of a number

SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

All through this program...What's the value of r?

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0

It supposed automatically be j. But i already solved the problem by entering if condition.

SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I think the issue is with this line:

i = i / 10;

Since i is an integer, and you are performing a division, it will cast i / 10 back into an integer. For example, if i / 10 resulted in 0.1, then i will equal 0, because 0.1 will be casted as 0 when defined as an integer.

MonsieurPointer
Junior Poster
143 posts since Jun 2011
Reputation Points: 45
Solved Threads: 15
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0653 seconds using 2.69MB