CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • crook

    MemberJul 12, 2006

    don't know

    😕 my skills in C are degrading for sure.


    waiting for the answer !!

    CrooK
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberJul 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
    Are you sure? This action cannot be undone.
    Cancel
  • rick

    MemberJul 13, 2006

    wow !!

    I cannot solve this 😔😔

    answer please mayank
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberJul 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Neha

    MemberAug 10, 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??
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberSep 23, 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:
    #-Link-Snipped-#



    Thanks and Regards,
    Mayank Shukla
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 25, 2006

    Great job!

    Hey Great Job, Mayank! 👍

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

    -The Big K-
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberSep 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");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Jerry

    MemberSep 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");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberSep 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
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberSep 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.😔
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberSep 29, 2006

    and one more thing. what i actually meant was n==0||n==3.but the code will work for n==0 alone.
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 29, 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-
    Are you sure? This action cannot be undone.
    Cancel
  • Mayank

    MemberOct 5, 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.
    Are you sure? This action cannot be undone.
    Cancel
  • anil_sjce

    MemberOct 29, 2006

    😕

    itoa and atoi functions use / and % internally................
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 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();
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Prasad Ajinkya

    MemberDec 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?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register