Replies
Welcome, guest
Join CrazyEngineers to reply, ask questions, and participate in conversations.
CrazyEngineers powered by Jatra Community Platform
-
@crook-0PFkJv • Jul 12, 2006
-
@mayank-MV7Gjv • Jul 13, 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-JybHX8 • Jul 13, 2006
-
@mayank-MV7Gjv • Jul 17, 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-3vibjn • Aug 10, 2006
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-MV7Gjv • Sep 23, 2006
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 -
@thebigk • Sep 25, 2006
Great job!
Hey Great Job, Mayank! 👍
Can we have more C, C++ puzzles for Computer Engineering folks?
-The Big K- -
@sahana-S4nL10 • Sep 25, 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-5wPKU7 • Sep 25, 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-MV7Gjv • Sep 25, 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-S4nL10 • Sep 29, 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-S4nL10 • Sep 29, 2006
and one more thing. what i actually meant was n==0||n==3.but the code will work for n==0 alone. -
@thebigk • Sep 29, 2006
Off 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-MV7Gjv • Oct 5, 2006
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-PKhypK • Oct 29, 2006
-
@manish-r2Hoep • Dec 27, 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-aSUfhP • Dec 28, 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?