One Easy" C " Question. Please Solve and Explain.

This is a discussion on One Easy" C " Question. Please Solve and Explain. within the C Programming forums, part of the General Programming Boards category; Question If a five Digit number is input through the keyboard , Write a programe to calculate the sum of ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    83

    Question One Easy" C " Question. Please Solve and Explain.

    Question
    If a five Digit number is input through the keyboard , Write a programe to calculate the sum of its Digits?

    Note:-Please when you write the code the explain it also, then only it will help me.And one more thing , I m using Turbo c++ Lite so please explain in easy terms.

    Thanks in Advance !

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    This is an assignment or not?
    You shouldn't be using Turbo C(++) either. Get a better IDE, if you can,
    http://cpwiki.sf.net/Integrated_Development_Environment
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Please when you write the code the explain it also, then only it will help me.
    This sounds a lot like you intend to steal the code, pretend it's yours, and turn it in for a grade.
    My best code is written with the delete key.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, since I'm so kind, here you go:
    Code:
    #include <stdio.h>
    int main()
    {
    	// Declare the variables. 
    	int l1,ll,lox;
    	char buf[10];
    	// Initalize variables. 
    	lox = 1;
    	ll = lox;
    	printf("Enter a number:");
    	fflush(stdout);
    	scanf("%d", &l1);   // Read the number.
    	ll--;
    	while(lox < l1) lox = ((lox << 2) + (lox)) << 1;  // Figure out the multiplier. 
    	while(lox) { ll += l1 % 10; l1 /= 10; lox /= 10; }; // Sum it up. 
    	sprintf(buf, "%%d"); // Make a format string. 
    	printf(buf, ll);  // Display the result. 
    	return 0;
    }




    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Answer
    Do your own damn homework!

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    This no assignment or anything... I m just going through a book called as " Let us C" by Yashwant kanetkar. I got this problem there and i was not able to do it so i asked you.I m attaching a screen shot for that.

    And also I have MS Visual C++ and Boroland C++5.02 but i do not know how to use those compiler.....

    Pleae check the attached Image.
    Attached Images Attached Images  

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >" Let us C" by Yashwant kanetkar
    Every time I see that book mentioned, bad code is involved. Would it be too forward of me to suggest that your book might suck?

    >I got this problem there and i was not able to do it
    Well, book exercises tend to give you everything you need in the chapter that the exercise shows up. So if I were you, I'd start by re-reading the chapter. It probably talks about division, remainder, and general numeric processing.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by Elysia View Post
    This is an assignment or not?
    You shouldn't be using Turbo C(++) either. Get a better IDE, if you can,
    http://cpwiki.sf.net/Integrated_Development_Environment
    Quote Originally Posted by Prelude View Post
    >Please when you write the code the explain it also, then only it will help me.
    This sounds a lot like you intend to steal the code, pretend it's yours, and turn it in for a grade.
    Quote Originally Posted by Prelude View Post
    >" Let us C" by Yashwant kanetkar
    Every time I see that book mentioned, bad code is involved. Would it be too forward of me to suggest that your book might suck?

    >I got this problem there and i was not able to do it
    Well, book exercises tend to give you everything you need in the chapter that the exercise shows up. So if I were you, I'd start by re-reading the chapter. It probably talks about division, remainder, and general numeric processing.
    The problem is in me " Not in the book or somewhere else" See... if we write a Number SAY " 21212 then i do not know how can write a code which will add them together like 2+1+2+1+2

    that is the problem i m facing ..i feel here we need to use modulus operator " %" but i do not know how?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Whiles my code may not be the easiest to read, it actually shows using the % operator perfectly fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i do not know how can write a code which will add them together like 2+1+2+1+2
    Here's a huge hint. For a number in base b, (number % base) will give you the least significant digit and (number / b) will remove the least significant digit.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by matsp View Post
    Ok, since I'm so kind, here you go:
    Code:
    #include <stdio.h>
    int main()
    {
    	// Declare the variables. 
    	int l1,ll,lox;
    	char buf[10];
    	// Initalize variables. 
    	lox = 1;
    	ll = lox;
    	printf("Enter a number:");
    	fflush(stdout);
    	scanf("%d", &l1);   // Read the number.
    	ll--;
    	while(lox < l1) lox = ((lox << 2) + (lox)) << 1;  // Figure out the multiplier. 
    	while(lox) { ll += l1 % 10; l1 /= 10; lox /= 10; }; // Sum it up. 
    	sprintf(buf, "%%d"); // Make a format string. 
    	printf(buf, ll);  // Display the result. 
    	return 0;
    }




    --
    Mats
    I can understand that your code is just like the falling codes of matrix movie ...... you are one of expert of this forum.I m beginner.Please Help accordingly.lol ...sometimes i feel shame that what i m doing in b/w these masters ......

    Please if you want to help me then write this programe as easy as possible. and explain it.it will be a great help for me as i m not taking any tution or doing schooling.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    Quote Originally Posted by Prelude View Post
    >i do not know how can write a code which will add them together like 2+1+2+1+2
    Here's a huge hint. For a number in base b, (number % base) will give you the least significant digit and (number / b) will remove the least significant digit.
    I feel this helps me alot....let me think and do it my self. and i m not able to do it then i will be back again to ask you all guys.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    if anyother hints are there then please post it....

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if anyother hints are there then please post it....
    When I said it was a big hint, I basically meant that's all you need. That's the solution, and you only need to put it into code.
    My best code is written with the delete key.

  15. #15
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Damn, flashbacks to Turbo Pascal from that screenshot!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie function return question
    By faifas in forum C Programming
    Replies: 2
    Last Post: 06-29-2009, 10:19 AM
  2. Help, please explain to me more
    By farewellbahrain in forum C++ Programming
    Replies: 4
    Last Post: 10-26-2005, 04:49 PM
  3. Can someone explain to me
    By rEtard in forum Windows Programming
    Replies: 1
    Last Post: 06-22-2005, 11:09 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. help me out with this question on arrays
    By datainjector in forum C Programming
    Replies: 10
    Last Post: 08-12-2002, 09:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21