Thread: 6 digit number

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question 6 digit number

    Code:
    /* 
    Write a program that inputs a six-digit number, separates the number into its 
    individual digits, multiple each separated digits by 2 and prints the digits 
    separated from one another by three spaces each. (Hint: use the integers 
    divisions and modulus operator.) For example, if the user types in 42339 the
    program should print 8 4 6 6 6 18 
    */
    Can some one give me some more hints in how to solve this program. What iam thinking is once the user enters an integer 245246 read each number as a character and then times it by 2. But the question is what should i do? Thanks for your help and guidance.
    Wise_ron
    One man's constant is another man's variable

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    % and /

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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Can some one give me some more hints in how to solve this program.
    Code:
    1) Prompt user to input number
    2) Read number into an int or long
    3) Use a loop to separate and print each digit
       a) Use modulus operator to extract digit
       b) Use division operator to update number for next modulus
       c) Print digit * 2
    The above will print the digits in reverse order, so if the digits must be printed in the order entered, you'll need to use an array to reverse them, or a recursive function instead of a loop.

  4. #4
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thank you guys

    Thanks for the explanantion and the tips.
    Wise_ron
    One man's constant is another man's variable

  5. #5
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Question little problem

    This is how my program looks now but now i want the numbers to be multiply by 2. Each digit should be multiply by two, example if enter 423339 the output should be 8 4 6 6 6 18

    Thanks
    [tag]
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num;
    	
    	printf("Enter a six-digit number:");
    	scanf("%d", &num);
       	
    	printf("%3d",num/100000);num=num/100000;num=num%100000;
    	printf("%3d",num/10000); num=num%10000;
    	printf("%3d",num/1000);  num=num%1000;
    	printf("%3d",num/100);   num=num%100;
    	printf("%3d",num/10);    num=num%10;
    	printf("%3d",num/1);
    	printf("   \n");
    	num=num%1;
    		return 0;
    }
    [/tag]
    Wise_ron
    One man's constant is another man's variable

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You need to use your brain on that one, man.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hint: you don't want to divide the number by anything after each printf(), because if you do, some digits will be lost.
    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.

  8. #8
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    the problem was so simple

    Sorry guys for asking that question. I figure it out, thanks for your help i only needed to put the * 2 where it was it was needed to be place. When i ask the question i was not thinking properly. I check the same problem in 2 hours time and i saw the simple mistake. It was so funny

    later and thanks
    Wise_ron
    One man's constant is another man's variable

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    hey wise_ron..do you mind telling me how did you extract the digits from the number and put them in order? eg. 1234 ---> 1 2 3 4 instead of getting 4 3 2 1(i know how to get them using /10 and %10)

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Good god, guy. There's a thread just below yours that asked the same question. Crazy. You guys in the same class or something ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    9
    happy_reaper do u know how to do it?..cheers

  12. #12
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Answer redx2evil

    Basically this problem is very simple, but you have to think about how to getting the numbers. Iam asking the user for a 6 digit number so i divide by 100,00 and then get the remainder of %100,000 of the same digit and then continue with the same logic until i reach the number / 1 and then %1.
    The logic is that the number is / and then %. This is all the program is doing you have to know how the % (remainder) works and thats it.

    My advice is to get a piece of paper and work it out how the program is getting solve and eventually you will get the picture. Is just some basic calculations. I have commented the code to help you... Hope it helps you.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int num;
    	
    	printf("Enter a six-digit number:");
    	scanf("%d", &num);
       	
    	printf("%3d",num/100000);num=num%100000; // 6 digit number is divided by 100,000 and then optain remainder %100,000 
    	printf("%3d",num/10000); num=num%10000;  // 5 digit is divide by 10,000 obtain remainder %10,000
    	printf("%3d",num/1000);  num=num%1000;   // 4 digit is divided by 1000 obtain remainder %1000
    	printf("%3d",num/100);   num=num%100;    // 3 digit is divide by 100   %1000
    	printf("%3d",num/10);    num=num%10;     // 2 digit is divide by 10    %100
    	printf("%3d",num/1);     num=num%1;      // 1 digit is divide by 1     %1 same logic  
    	printf("   \n");
    		return 0;
    }
    Wise_ron
    One man's constant is another man's variable

  13. #13
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    /*
    Write a program that inputs a six-digit number, separates the number into its
    individual digits, multiple each separated digits by 2 and prints the digits
    separated from one another by three spaces each. (Hint: use the integers
    divisions and modulus operator.) For example, if the user types in 42339 the
    program should print 8 4 6 6 6 18
    */
    1. 42339 is not a 6 digit number
    2. if you multiply each digit by 2 you get 5 numbers 8 4 6 6 18 where did the other 6 come from.

    not a very good specification.

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Quote Originally Posted by redx2evil
    happy_reaper do u know how to do it?..cheers
    Yes, I do. Something similar was even answered in the other thread.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    printf("%3d",num/1);     num=num%1;
    I wonder it compiles. My program crashed when I used modulus with 1.
    Your num is always 0 when you use modulus with 1.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  2. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. 4 digit number
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 06-21-2005, 04:41 AM
  5. outputs number digit by digit
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 05-17-2002, 04:43 PM