let the number be num
so code is this
int ans=0;
while(num>=3)
{
num=num-3;
ans++;
}
No need to use itoa. This is simple division algo based on subtraction.
On Sat, Jul 30, 2011 at 2:09 PM, Ankit Minglani <[email protected]>wrote:
> #include<stdio.h>
> #include<stdlib.h>
> #include<conio.h>
> #include<string.h>
> #include<math.h>
>
> int multiply(int a,int b)
> {
>
> int i;
> int temp=a;
> printf("\na=%d b=%d\n",a,b);
> for(i=1;i<b;i++)
> a+=temp;
> printf("\nfinal a = %d",a);
> return(a);
>
> }
>
> void main ()
> {
> int x,rem,quo=0,i,j;
> char p[20];
> clrscr();
> scanf("%d",&x);
> itoa(x,p,3);
>
> rem=(int)p[strlen(p)-1]-48;
> printf("%dRemainder\n",rem);
>
> for(i=strlen(p)-2,j=0;i>=0;i--,j++)
> {
> printf("p[j] = %d",p[j]-48);
> // quo+=(p[j]-48)*pow(3,i);
> quo+=multiply(p[j]-48,pow(3,i));
> printf("\nquo=%d",quo);
>
> }
>
> printf("\nQuotient=%d",quo);
>
> printf("\n%s",p);
> getch();
>
> }
>
> taking base 3 will convert the number into the base 3 form ..
> for example let x=100 the number to be divided by three.
>
> so 100 in base 10 = 10201 in base 3 .
> we get base 3 by consecutive divisions by 3 so the last number will always
> be the remainder ie 1 .
>
> rest 1 0 2 0 will be the quotient.
> | | | |
> index: 3 2 1 0
> so p[j]-48 will convert the char to integer and mutiply it wil 3 ^ power (
> index )
> the answer will be the quotient.
>
>
>
> On Sat, Jul 30, 2011 at 12:15 AM, aditi garg <[email protected]>wrote:
>
>> @Samm : Im not able to understand ur logic...im not getting the correct
>> ans...can u explain the working taking n as 7?
>>
>>
>> On Sat, Jul 30, 2011 at 12:25 PM, SAMM <[email protected]> wrote:
>>
>>> Single bit shift...
>>>
>>> int divide(int n)
>>> {
>>> n-=1;
>>> n>>=1;
>>> return n;
>>> }
>>>
>>> On 7/30/11, tech rascal <[email protected]> wrote:
>>> > hw will u get the ans on repeated subtraction from the sum of the
>>> digits??
>>> > I mean ....if I hv 2 divide 27 by 3 thn first I'll find sum of the
>>> digits
>>> > i.e, 2+7=9
>>> > then I'll apply repeated subtraction on 9, so hw will i reach to the
>>> ans??
>>> >
>>> >
>>> > On Sat, Jul 30, 2011 at 10:05 AM, nivedita arora <
>>> > [email protected]> wrote:
>>> >
>>> >>
>>> >> @brijesh-
>>> >> itoa basically converts integer to string ..we are using the fact tht
>>> >> a number is multiple of 3 if its sum is multiple of 3
>>> >> . we have int as string and we can traverse it ..for each character
>>> >> apply
>>> >> int sum+=*c-'0' (ankur missed the star :P)
>>> >> then on sum we use repeated subtraction...i hope its clear .
>>> >>
>>> >> we are using all this just coz we have to use itoa ..otherwise there
>>> >> are more methods (check my frst post )
>>> >>
>>> >>
>>> >> On Jul 30, 4:34 am, brijesh <[email protected]> wrote:
>>> >> > @ankur I didnt get this... could u or anyone please elaborate!
>>> >> >
>>> >> > On Jul 30, 12:43 am, Ankur Khurana <[email protected]>
>>> wrote:
>>> >> >
>>> >> >
>>> >> >
>>> >> >
>>> >> >
>>> >> >
>>> >> >
>>> >> > > when you use itoa , what you get is a string. get the sum of all
>>> the
>>> >> digits
>>> >> > > , using c-'0' and then use repeated subtraction . . .
>>> >> >
>>> >> > > On Sat, Jul 30, 2011 at 1:01 AM, sukhmeet singh <
>>> >> [email protected]>wrote:
>>> >> >
>>> >> > > > repeated subtraction !!
>>> >> >
>>> >> > > > On Sat, Jul 30, 2011 at 12:52 AM, nivedita arora <
>>> >> > > > [email protected]> wrote:
>>> >> >
>>> >> > > >> Without using /,% and * operators. write a function to divide a
>>> >> number
>>> >> > > >> by 3. itoa() function is available.
>>> >> >
>>> >> > > >> all i cn thnk of is to use shift operator and addition ,
>>> >> x/3=e^(logx-
>>> >> > > >> log3) or repetitive subtraction
>>> >> >
>>> >> > > >> but none of them uses itoa() ..ne idea how its done?
>>> >> > > >> thnks !
>>> >> >
>>> >> > > >> --
>>> >> > > >> You received this message because you are subscribed to the
>>> Google
>>> >> Groups
>>> >> > > >> "Algorithm Geeks" group.
>>> >> > > >> To post to this group, send email to
>>> [email protected].
>>> >> > > >> To unsubscribe from this group, send email to
>>> >> > > >> [email protected].
>>> >> > > >> For more options, visit this group at
>>> >> > > >>http://groups.google.com/group/algogeeks?hl=en.
>>> >> >
>>> >> > > > --
>>> >> > > > You received this message because you are subscribed to the
>>> Google
>>> >> Groups
>>> >> > > > "Algorithm Geeks" group.
>>> >> > > > To post to this group, send email to [email protected]
>>> .
>>> >> > > > To unsubscribe from this group, send email to
>>> >> > > > [email protected].
>>> >> > > > For more options, visit this group at
>>> >> > > >http://groups.google.com/group/algogeeks?hl=en.
>>> >> >
>>> >> > > --
>>> >> > > Ankur Khurana
>>> >> > > Computer Science
>>> >> > > Netaji Subhas Institute Of Technology
>>> >> > > Delhi.
>>> >>
>>> >> --
>>> >> You received this message because you are subscribed to the Google
>>> Groups
>>> >> "Algorithm Geeks" group.
>>> >> To post to this group, send email to [email protected].
>>> >> To unsubscribe from this group, send email to
>>> >> [email protected].
>>> >> For more options, visit this group at
>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>> >>
>>> >>
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> Groups
>>> > "Algorithm Geeks" group.
>>> > To post to this group, send email to [email protected].
>>> > To unsubscribe from this group, send email to
>>> > [email protected].
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/algogeeks?hl=en.
>>> >
>>> >
>>>
>>>
>>> --
>>> Somnath Singh
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>
>>
>> --
>> Aditi Garg
>> Undergraduate Student
>> Electronics & Communication Divison
>> NETAJI SUBHAS INSTITUTE OF TECHNOLOGY
>> Sector 3, Dwarka
>> New Delhi
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> The more you sweat in the field, the less you bleed in war."
>
> Ankit Minglani
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
Abhishek Gupta
MCA
NIT Calicut
Kerela
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.