Thread: help on assignment

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    help on assignment

    Could someone give me some clues as to where to start on this assignment?

    Also could someone please explain to me how this works?


    Write and compile a program to input a five digit integer using one scanf statement and display the sum of its digits. For example if user enters 12345, then your program should output 1+2+3+4+5 = 15. You will need to use % and / operators.


    Please help I am a newbie and I really need to get the basics down, thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    scanf("%1.1i", &integer);
    I think that reads in one char with scanf(), because the maximum chars read is 1, and so is the minimum.

    [edit]
    Ignore the above, I misread your post.

    12345 / 100 % 10 = 2

    Does that give you any ideas?
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Ok, you lost me there

    where did the 100 % 10 come from?


    where is a good link to understand this assignment a little better?

  4. #4
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, you could check out some of the tutorials on the CProgramming home page to be better prepared for your next assignment. I'll help you out here for this one.

    100 % 10 is a use of the modulus operator which I'm surprised you don't know about. The modulus operator divides two numbers, but the answer returned is the remainder from that division. I'll get to that in a second.

    You know how to scan the number in, I assume. If not, it's simple.

    Code:
    int entry;
    
    scanf("%i", &entry);
    But, then you have to single out each digit. It's not that hard, once you have an algorithm. That's what dwks gave. I'll break it down.
    Code:
    (12345 / 100) % 10 //Dividing by 100gets rid of the last three digits.
    (12) % 10 //Modulus by 10, removes the first digit.
    2 //You are left with the second digit, 2
    To get the first digit, you would change the 100 and the 10. See if you can figure it out.
    Code:
    void function(void)
     {
      function();
     }

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    The '%' is an operator.

    Just like the regular *BS* you're used to such as '+', '-' yada, yada yada- it is no different.


    >What is it?

    It is the bog standard way for denoting the remainder in c programming.

    Let's illustrate with an example.
    ----------------------------------------
    What's 35/10= 3r5

    So the remainder is five.

    What's 12/6= 2r0

    So the remainder is 0.


    And that is all it is. However, it would be written as such:

    int ans=35%10;

    When you print this you should get 5.

    The question is how does this help with your assignment?
    I'll leave that for you to think over. Then get back when you got some code.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Well this is what i've come up with but i know it isn't right:
    Code:
    #include <stdio.h>
    #include<conio.h>
    int main(){
    
    int a;
    12345 % 10000 % 1000 % 100 % 10;
     (12345 % 10000 % 1000 % 100) / 10; 
     (12345 % 10000 % 1000 ) / 100;
     (12345 % 10000 ) / 1000;
     (input /10000 )%10;
    
    scanf("%i",&a);
    printf("you entered",a);
        
     getchar();   
    }


    I had this hint but still can not grasp the concept of all the % 10000 % 1000.

    You need to use / and % operator to solve this problem.

    Let's take our example 5 digit number as 12345

    unit digit of 12345 is 5, we get like this: 12345 % 10000 % 1000 % 100 % 10
    10th digit of 12345 is 4, we get like this: (12345 % 10000 % 1000 % 100) / 10
    100th digit of 12345 is 3, we get like this: (12345 % 10000 % 1000 ) / 100
    1000th digit of 12345 is 2, we get like this: (12345 % 10000 ) / 1000)
    10000th digit of 12345 is 1, we get like this: (input /10000 )%10

    sum of this 5 digit number= unit digit+10th digit+100th digit+1000th digit+10000th digit


    I have read all of my notes and searched everywhere but I still can not find the explanation I am looking for.


    how do you put this to code and how does it work in simple terms.


    All of this is getting very confusing?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Come on, it's not that hard. If you can't figure this out, seriously, consider a different hobby / career. Don't you see any pattern in the above code? You don't see anything that you could repeat? Any steps for which you could do something over and over, while you don't have your answer?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    n00b
    Join Date
    May 2005
    Location
    Lakefield
    Posts
    12
    Is 12345 / 100 % 10 not 3?
    The divide by 100 strips the 45 and the remainder of 123 / 10 would be 3.

    unit digit 5 would be 12345 % 10
    tens digit would be (12345 / 10) % 10
    hundreds digit would be (12345 / 100) % 10 ...

  9. #9
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    And in that code of yours, which also works, by the way, you do not do anything with those values. They're just sitting there and since you don't assign them to a variable, they are lost.
    Code:
    int digits[5];
    digit[0] = ...
    digit[1] = ...
    digit[2] = ...
    digit[3] = ...
    digit[4] = ...
    If you want to see how that works, work it out on a piece of paper. It's 3rd grade math.
    Code:
    void function(void)
     {
      function();
     }

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I got it thanks to the people that helped me:

    here is my code

    Code:
    I got it : 
     
    #include <stdio.h>  
    #include<conio.h>  
    int main(){  
     
    int number,total=0;  
     
     
    printf("enter a five digit integer: "); 
     
    scanf("%d",&number);  
     
    printf("%d+",number % 10000 % 1000 % 100 % 10); /* prints the 1st digit + */ 
     
    printf("%d+",number % 10000 % 1000 % 100 / 10 ); /* 10ths + */ 
     
    printf("%d+",number % 10000 % 1000 / 100 ); /* 100th + */ 
     
    printf("%d+",number % 10000 / 1000 ); /* 1000th + */ 
     
    printf("%d=",number /10000 %10); /* 10000th = */ 
     
    total=("%d+",number % 10000 % 1000 % 100 % 10)+ /* adds the 1st digit + */ 
     
    (number % 10000 % 1000 % 100 / 10 )+ /* 10ths + */ 
     
    (number % 10000 % 1000 / 100 )+ /* 100th + */ 
     
    (number % 10000 / 1000 )+ /* 1000th + */ 
     
    (number /10000 %10); /* 10000th = */ 
     
    printf ("%d\n",total); 
     
     
     
    getch();  
    }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't be surprised when you fail this assignment. That's a horrible way to do it. On that note:
    Code:
    printf("%d+",number % 10000 % 1000 % 100 % 10); /* prints the 1st digit + */
    What's the "first digit"? The left, or the right?

    Left:
    Code:
    printf("%d+", number / 10000 );
    Right:
    Code:
    printf("%d+", number % 10 );
    In either case, there's a much better way to do what you're doing.

    You can avoid that whole mess by reading my last reply.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374

    Thumbs up

    Beauty personified.

    Code:
    I got it : 
     
    #include <stdio.h>  
    #include<conio.h>  
    int main(){  
     
    int number,total=0;  
     
     
    printf("enter a five digit integer: "); 
     
    scanf("%d",&number);  
     
    printf("%d+",number % 10000 % 1000 % 100 % 10); /* prints the 1st digit + */ 
     
    printf("%d+",number % 10000 % 1000 % 100 / 10 ); /* 10ths + */ 
     
    printf("%d+",number % 10000 % 1000 / 100 ); /* 100th + */ 
     
    printf("%d+",number % 10000 / 1000 ); /* 1000th + */ 
     
    printf("%d=",number /10000 %10); /* 10000th = */ 
     
    total=("%d+",number % 10000 % 1000 % 100 % 10)+ /* adds the 1st digit + */ 
     
    (number % 10000 % 1000 % 100 / 10 )+ /* 10ths + */ 
     
    (number % 10000 % 1000 / 100 )+ /* 100th + */ 
     
    (number % 10000 / 1000 )+ /* 1000th + */ 
     
    (number /10000 %10); /* 10000th = */ 
     
    printf ("%d\n",total); 
     
     
     
    getch();  
    }
    I'd pass you. lol

    Code:
    #include <stdio.h>
    #include <conio.h>  
    int main()
    {
       
        int treenef;
        printf("Enter ANY digit number:");
        scanf("%d",&treenef);
        int tree_n_ef_ff=treenef;int tre_e_n_ef_ff=0;
        while (tree_n_ef_ff>0)
        {tree_n_ef_ff=tree_n_ef_ff/10;tre_e_n_ef_ff++;}
        int treee=0;int treee_e_e=1;
        for (int i=0; i<tre_e_n_ef_ff; i++)
        {int tree_n_ef=treenef/treee_e_e;
        int j=tree_n_ef%10;printf("%d + ",j);
        treee=treee+j;treee_e_e=treee_e_e*10;}   
        printf(" = "); printf("%d ",treee);
        getch();
    }
    Last edited by treenef; 07-08-2005 at 01:52 AM.

  13. #13
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    a % 10000 % 1000 == a % 1000. Always.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM