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.;-)
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.;-)
Replies
-
crookdon't know
๐ my skills in C are degrading for sure.
waiting for the answer !!
CrooK -
MayankHey 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 -
rickwow !!
I cannot solve this ๐๐
answer please mayank -
MayankHi 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
#include
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< else
cout< }
Thanks and Regards,
Mayank Shukla. -
Neha
Can u tell me for what is atoi() method used??MayankHey 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 -
Mayank
Hi Neha,nehaCan u tell me for what is atoi() method used??
It does the ascii to integer conversion of a string.
Try this out:
#-Link-Snipped-#
Thanks and Regards,
Mayank Shukla -
Kaustubh KatdareGreat job!
Hey Great Job, Mayank! ๐
Can we have more C, C++ puzzles for Computer Engineering folks?
-The Big K- -
sahanahi
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");
} -
JerrySahana'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"); }
-
MayankGood 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 -
sahanahi 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.๐ -
sahanaand one more thing. what i actually meant was n==0||n==3.but the code will work for n==0 alone.
-
Kaustubh KatdareOff Topic
((bump)) ๐
Great job Mayank & Sahana!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
Hey,sahanahi 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.๐
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๐
itoa and atoi functions use / and % internally................ -
Manish GoyalI don't know c ./..so here is the code in c++
#include
#include void main() { int no; cout<<"enter no"< >no; while(no>0) { no=no-3; } if(no==0) { cout<<"Divisible by 3"< -
Prasad AjinkyaI 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?
You are reading an archived discussion.
Related Posts
Hello people
Can anyone suggest me some projects in JAVA??
I am learning JAVA core...and it would be preferrable if projects are related to media?
I have often seen people doing Ping with IP(internet Protocol) address in continuation when the server is down...Can anyone write down what does it mean and why is it used??
With the latest blasts that rocked the wonderful city;mumbai. I have been thinking of the mentality,perspective of these terrorists who neither bother about old,young;rich,poor; women,men.I wonder what drives them so...
Hi first of all i want to say nice work fellows :clap:
well my suggestion is regarding fact of engineering which might look crazy :hehehe: but act perfectly ;-) and...
Free ebooks @
https://worldebookfair.com/
Check it out ! ๐
-The Big K-