1,105,581 Community Members

Why my progroma writes in the answer 1 insted of 0

Member Avatar
SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;

}
Member Avatar
SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
SDDMV
Newbie Poster
3 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
MonsieurPointer
Junior Poster
152 posts since Jun 2011
Reputation Points: 27 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article