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

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Perhaps a while loop, (straight from my own Turbo C/C++ compiler )

    Code:
    #include <stdio.h>
    
    int main(void)   {
    
       long number;
       int sum, c;
    
       /* get your number up here */
    
       sum = 0;
       while(number > 0)   {
          sum += number &#37; 10;  /* same as sum = sum + number % 10 */
          number /= 10;              /* same as number = number / 10 */
          printf("\n  sum = %d, number = %ld ", sum, number);
       }
    
       printf("\n\n\t\t\t  Hit Enter When Ready ");
       while((c = getchar()) != '\n');
       c = getchar();
       
       return 0;
    }

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    83
    I have done it .. it was difficult to find out but i use Modulus and divide... and did it... i will post the code here.

  3. #18
    Registered User
    Join Date
    Mar 2008
    Location
    BITS,Pilani
    Posts
    2
    Quote Originally Posted by RahulDhanpat View Post
    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 !
    insert
    Code:
    #include<math.h>
    main()
    {int num,d1,d2,d3,d4,sum;
    scanf ("%d",&num);
    if (num>=1000 && num <=9999)
    {d1=num%10;
    d2=(num%100)/10;
    d3=(num%1000)/100;
    d4=(num%10000)/1000;  sum=d1+d2+d3+d4;
    printf ("%d",sum);}}
    i've done it for 4digit num u can xtend it further easily

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by rags_to_riches View Post
    Damn, flashbacks to Turbo Pascal from that screenshot!
    Me too, I saw that and thought "Man that's familiar, I remember that IDE," and shivered in disgust.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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