Mayank
Mayank
Branch Unspecified
11 Jul 2006

C Coders--->Divisibility by 3, with *Condition Apply

Hi All,

Here is one question from my side in the line of puzzels in C.

Find* whether a given 1/2/3/4/5 digit number is divisible by 3 or not.
*Conditions:
Use of %,/,* not allowed.

Not a big one...so hope ppl will reply to it soon.;-)
crook

crook

Branch Unspecified
13 Jul 2006
don't know

😕 my skills in C are degrading for sure.


waiting for the answer !!

CrooK
Mayank

Mayank

Branch Unspecified
13 Jul 2006
Hey Guys just dont loose ur spirit.....have some ;-) and rethink. I will come up a solid clue, here it is-------> u can use "atoi()".
Now the sea(The 'C') is urs dive n find the pearl. All the best to all of you.

Mayank Shukla
rick

rick

Branch Unspecified
13 Jul 2006
wow !!

I cannot solve this 😔😔

answer please mayank
Mayank

Mayank

Branch Unspecified
17 Jul 2006
Hi All,

Here is the solution to the question :
//Code is using C++ Syntax but its a C code in C++.(No OOP //applied). It can b easily transformed into core C code with few //changes. So i hope u all will still accept it.


#include <fstream>
#include <iostream>
using namespace std;
bool flag=false;
void check(int i)
{
int sum=0;
char n[5];
itoa(i,n,10);
for(int j=0;j<5 && n[j] != '\0';j++)
{
char p = n[j];
sum += atoi(&p);
}
if(sum==3 || sum==6 || sum==9)
flag = true;
else if(sum>9)
check(sum);
}

void main()
{
int i;
cout<<"Enter the number : ";
cin>>i;
check(i);
if(flag == true)
cout<<i<<" is divisible by '3'"<<endl;
else
cout<<i<<" is not divisible by '3'"<<endl;
}




Thanks and Regards,
Mayank Shukla.
Neha

Neha

Branch Unspecified
10 Aug 2006
Mayank
Hey Guys just dont loose ur spirit.....have some ;-) and rethink. I will come up a solid clue, here it is-------> u can use "atoi()".
Now the sea(The 'C') is urs dive n find the pearl. All the best to all of you.

Mayank Shukla
Can u tell me for what is atoi() method used??
Mayank

Mayank

Branch Unspecified
23 Sep 2006
neha
Can u tell me for what is atoi() method used??
Hi Neha,

It does the ascii to integer conversion of a string.
Try this out:
https://www.cplusplus.com/ref/cstdlib/atoi.html



Thanks and Regards,
Mayank Shukla
25 Sep 2006
Great job!

Hey Great Job, Mayank! 👍

Can we have more C, C++ puzzles for Computer Engineering folks?

-The Big K-
sahana

sahana

Branch Unspecified
25 Sep 2006
hi
you said that only %,/ and * are not allowed.what about -

consider this solution
void main()
{
int n,i=0;
printf("enter the number");
scanf("%s",&n);
while(n>=3)
{
n=n-3;
if(n==0||n==1)
i=1;
}
if(i==1)
printf("divisible");
else
printf("not divisible");
}
Jerry

Jerry

Branch Unspecified
25 Sep 2006
Sahana's code should work. Mayank please confirm.

void main()
{ 
int n,i=0;
printf("enter the number");
scanf("%s",&n);
while(n>=3)
{
n=n-3;
if(n==0||n==1)
i=1;
}
if(i==1)
printf("divisible");
else
printf("not divisible");
}
Mayank

Mayank

Branch Unspecified
25 Sep 2006
Good try: BUT check the efficiency!!!

Hi Sahana,

Actually this was the reply that i was expecting first. But it has few undesirable consequences and flaws.

Few small one:
1) Just change the scanf line from "scanf("%s",&n);" to "scanf("%d",&n);"
I can understand that it could be a small typo from your side.
2) Try your code for n=4, It will say "DIVISIBLE". The reson is, the if condition which is "if(n==0||n==1)" should be "if(n==0)".

A bit Bigger reason:
Check the efficiency buddy:
I added a "count" variable in your code and tried to find the number of times the loop will execute. The number comes out to be "33333". If u try the mechanism I suggested above, using a global variable "count", it comes out to be '7'.
So, there it is...... where it makes the code really slow.

Neways, a simple and a nice/good approach given by you. I appreciate it.
But remember that in production environment code efficiency screws you up. 😔


Thanks and Regards,
Mayank Shukla
sahana

sahana

Branch Unspecified
29 Sep 2006
hi mayank.
sorry i do accept my mistake.
i have understood my small mistakes and my big one(didnt think that u wud give a very big input).please tell me ways in which i can improve the efficiency of my programming skills.i m very poor in writing effiecient codes.😔
sahana

sahana

Branch Unspecified
29 Sep 2006
and one more thing. what i actually meant was n==0||n==3.but the code will work for n==0 alone.
29 Sep 2006
Off Topic

((bump)) 😁

Great job Mayank & Sahana! [​IMG] I guess we could have a separate thread for discussions on "efficient code writing". I encourage computer engineers to start similar discussions inside "Technical Discussions -> Computer Science & IT" section.

Keep the spirit high!

-The Big K-
Mayank

Mayank

Branch Unspecified
05 Oct 2006
sahana
hi mayank.
sorry i do accept my mistake.
i have understood my small mistakes and my big one(didnt think that u wud give a very big input).please tell me ways in which i can improve the efficiency of my programming skills.i m very poor in writing effiecient codes.😔
Hey,

Sorry for the late reply, but I appreciate your effort. As far as improving your programming abilities is considered, I'll say that even the same is with me but one thing is true which I can share according to my experience that: Practice makes a man perfect. What you can do is take up tricky question and then try solving it through different ways and all the time just think on how you can make your code run faster.
This will surely help you.

For your next post......

You know the code will work with if(n==0) alone and also with if(n==0||n==3) as you suggested . But you know in your case that is "if(n==0||n==3)" the loop will run one time less than for "if(n==0)".
Now it is you who have to decide whether you want to reduce one iteration against evalution of one more condition at each iteration.
So see, u have found a way to improvise on coding practises.

Thanks and Regards,
Mayank Shukla.
anil_sjce

anil_sjce

Branch Unspecified
30 Oct 2006
😕

itoa and atoi functions use / and % internally................
Manish Goyal

Manish Goyal

Computer Science
27 Dec 2009
I don't know c ./..so here is the code in c++
#include<iostream.h>
#include<conio.h>
void main()
{
int no;
cout<<"enter no"<<endl;
cin>>no;
while(no>0)
{
no=no-3;
}
if(no==0)
{
cout<<"Divisible by 3"<<endl;
}
else
{
cout<<"Not DIVisible"<<endl;
}
getch();
}
Prasad Ajinkya

Prasad Ajinkya

Branch Unspecified
28 Dec 2009
I am not going to put the code down, but heres a quick and dirty logic.

1. Get the sum of all digits (say x)
2. Get the sum of all digits of x till you get a single digit
3. Use switch case (3,6,9) to see if it's the right no.

Mayank, does that hold?

Share this content on your social channels -

Only logged in users can reply.