Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: for

  1. #1
    New Coder
    Join Date
    Jul 2002
    Location
    Malaysia
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts

    for

    Dear sir,

    Refers to the below codes, my program can't be run in properly. Do you know why???

    #include <stdio.h>
    #include <conio.h>

    void main()
    {
    int n, count, factorial;

    clrscr();
    printf("Enter a positive integer: ");
    scanf("%i", &n);
    printf("Integer \t Factorial\n");
    for (n = 1; n >= 1; --n)
    { printf(" %3i ", n);
    factorial = 1;
    for (count = 1; count <= n; ++count)
    factorial *= count;
    printf("%i\n", factorial);
    }
    getch();
    }


    Thanks

    Xiang

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    clrscr();
    printf("Enter a positive integer: ");
    scanf("%i", &n);
    printf("Integer \t Factorial\n");
    for (n = 1; n >= 1; --n)
    { printf(" %3i ", n);
    factorial = 1;
    for (count = 1; count <= n; ++count)
    factorial *= count;
    printf("%i\n", factorial);
    }

    the program can never run, because n is less than one. n-- means use n, then subtract one, but --n means subtract one from n, and then use n. so for (n = 1; n >= 1; --n) means that n is 0, and therefore, for (count = 1; count <= n; ++count) can never work, because count is 1, and greater than 0
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •