Thread: a problem

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    a problem

    hi i'm a newbie to c and am trying to create a program that calculates the "greatest common divisor". i'm always getting 0 as the result. here's my src:
    Code:
     #include <stdio.h>
    
    main() {
       int m, n, i;
       
       printf("Enter two integers: ");
       scanf("%d%d", &m, &n);
       do {
          i = m / n;
          n = m % n;
          m = i;
       } while ( n != 0 ) ;
       printf("Greatest common diviser: %d", m);
       return 0;
    }
    i would be grateful if someone can help me. btw i'm using Devcpp.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think there's something wrong with your formula.
    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
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    I normally try not to give away the whole answer, but this pretty much sums it
    up very neatly, so I thought I'd be nice and post it. Take the pseudocode
    shown here and convert it to actual code, and that's it.

    EDIT: main returns int, see my sig
    Last edited by Richie T; 09-03-2006 at 12:10 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    thanks alot yes there was a problem in my formula. this is my new src:
    Code:
     #include <stdio.h>
    
    main() {
       int m, n, i;
       
       printf("Enter two integers: ");
       scanf("%d%d", &m, &n);
       do {
          i = n;
          n = m % n;
          m = i;
       } while ( n != 0 ) ;
       printf("Greatest common diviser: %d", m);
       return 0;
    }
    thanks alot guys. i don't know i'v been working on it for an hour yesterday and couln't get it, today i fixed it within 15 min. anyways i'm also trying to make a program that ask the user to enter a number, then reverse it's digits. i know how to do it with a limited number of digits, but how can i do it with an almost unlimited(as the int type doesn't support all numbers) number of digits.
    thanks again.

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Read the data into a string and print it in reverse.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    i didn't learn about strings yet all i know is the basics(ex. loops, if statement).

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well we'll use int then:

    1. Read in a number
    2. print number % 10
    3. divide number by 10 and save result in number (variable name in this context)
    4. repeat steps 2 and 3 while number is not 0.

    That should do it for any int value.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    thanks richie it worked
    Last edited by Abda92; 09-03-2006 at 12:49 PM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    it is good practice to specify the int before the main, even thought is dosn't matter as the compiler would assume it as an int. So change it to
    Code:
    int main()
    {
       ...
       ...
       return 0;
    }
    to follow the good programming practice

    ssharish2005

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    what's the difference between "main()" and "int main"?

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    int main() is right ... or int main( void ) is right http://faq.cprogramming.com/cgi-bin/...&id=1043284376

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    whats a "boolean"?
    Last edited by Abda92; 09-04-2006 at 08:49 AM.

  13. #13
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    It's a type which vars can be only one or zero.
    Code:
    typedef enum 
    {
      FALSE = 0,
      TRUE
    } MY_BOOL;
    Last edited by andor; 09-04-2006 at 07:35 AM.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    thanks guys i'm sorry but i'm having some trouble. i'm trying to write a program that prints a one-month calendar, the user specifies the number of days in the month and the day of wich the month begins. here's my src:
    Code:
    #include <stdio.h>
    main() {
       int i, n, j;
       
       printf("Enter number of days in month: ");
       scanf("%d", &n);
       printf("Enter starting day of the week (1=Sun, 7=Sat): ");
       scanf("%d", &j);
       for (i = 1; i <= n; i++) {
          printf("j = %d", j);
          j++;
          if (j > 7) {
             printf("\n");
             j = 1;
          }
       }
       return 0;
    }
    i'm sorry i have two probs:1) i don't know how to make the the month begin on a specific day(ex. the month begins on thursday). 2) i don't know how to make the program start a new line. i think this problem is because of the first prob.
    thanks

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (i = 1; i <= n; i++)
    Before you do this, you need a loop involving 'j' to work out how far to indent the first line.

    > printf("j = %d", j);
    Print i, which contains the day number.
    To make the formatting look nicer, try
    printf( "%2d ", i );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM