for loop problem

This is a discussion on for loop problem within the C Programming forums, part of the General Programming Boards category; I need to write a program that takes a positive integer that is input and then output an integer. the ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    for loop problem

    I need to write a program that takes a positive integer that is input and then output an integer. the ouput is the integer input multiplied by each integer below i. e.g. if 5 was input, i need to ouput 5*4*3*2*1. I can get the number input and store it in a variable, but im not sure about the loop i need. i think it would be a 'for' loop which decreases the value of the input and then multiply that witht he value input

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    > i think it would be a 'for' loop which decreases the value of the input and then multiply that witht he value input
    Sounds right to me
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Post what you have, even if it's not much.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    Here is all i have at the moment

    Code:
    #include <stdio.h>
    
    void main(void) {
    
            printf("Enter a positive integer");
            scanf("%d", &num);
            fflush(stdin);
    
                  while (num>=num && num>0) {
    	     printf("%d", num*num--);
    	}
    }

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Don't use "fflush(stdin)", search the board as to why not.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    So having correctly determined that a for loop was necessary, you go ahead and use a while loop.

    > void main(void)
    int main ( void )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    3
    Here is a similar program i have developed unfourtunatly it still isnt working properly i seem to get 35 or somewhere there abouts by multiplyig say 5*4*3*2*1. Perhaps some one could tell me why?
    [code]
    #include <stdio.h>
    #include <conio.h>

    void main (void)
    {
    int num;
    int x;
    int mult = 0;
    clrscr();

    printf("please enter a number: ");
    scanf("%d", &num);
    fflush(stdin);

    for(x=1;x<=num;x++)
    {
    printf("%d", x);
    printf("*");
    mult++;
    }
    x=mult*x;
    printf("%d", x);


    }
    [\code]

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Heh - Colin and David are at the same college...

    > x=mult*x;
    Try
    1. putting this inside the loop
    2. making it mult=mult*x;

    printing mult (not x) when you're done as well
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    I have changed the code slightly but it is still not outputting the correct value:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main(void) {
       int num;
       int x;
       int div;
         clrscr();
    
    	printf("Enter a positive integer : ");
    	scanf("%d", &num);
    	fflush(stdin);
    
    	    for (x=num; x>0; x--) {
    		 printf("%d", x);
    		 printf("*");
    		 div++;
    		 div=div*x;
    		 }
    
    	    printf(": %d", div);
    	}

  10. #10
    .
    Join Date
    Nov 2003
    Posts
    307
    Code:
    #include <stdio.h>
    
    
    int main(int argc, char *argv[]) {
       int num=0;
       int x=0;
       int div=1;
    
       printf("Enter a positive integer : ");
       scanf("%d", &num);
       /* fflush(stdin); */
       
           for (x=num; x>0; x--) {
       	 printf("%d", x);
       	 if(x>1) printf("*");
       	 /* div++ */
       	 div=div*x;
       	 }   
        printf(": %d\n", div);   
       return 0;	
    }

  11. #11
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    Well, obviously, you don't pick up hints very well:

    Change:

    1) void main(void) -> int main(void)
    2) remove "fflush (stdin);"


    Add a return statement.

    What is the purpose of "div++;"?? You never initialize div... or x or num for that matter.

    edit: beaten.

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    Thumbs up

    thanks salem, but now im geting 325. i ned to write a C program that accepts an int value and returns it s factroal if thats any help

  13. #13
    Registered User
    Join Date
    Nov 2003
    Posts
    3

    Unhappy

    one other small problem im having, i also need to make a program that subtracts one time from another, it is supposed to display in 24 h format, thing is i cant seem to get 00:00 hours to appear by subtracting two times that are the same ie 10:00 - 10:00 I have managed to get a display of 22:00 hoursso what am i doing wrong?
    here is the code
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main (void)
    {
       int hours,hours2=0;
       int mins, mins2=0;
       clrscr();
    
       printf("Enter the time you reqire (hours) 24-h format: ");
       scanf("%d", &hours);
       fflush(stdin);
       printf("\n");
       printf("now enter the minutes: ");
       scanf("%d", &mins);
       fflush(stdin);
    
       if (hours >23)
          hours=0;
       if (mins >=60)
       {
          mins=mins-60;
          hours++;
       }
       printf("the time entered is %2d:%.2d\n", hours, mins);
       printf("\n");
       printf("Enter the time you wish to be subtracted (hours): ");
       scanf("%d", &hours2);
       fflush(stdin);
       printf("\n");
       printf("now enter the minutes: ");
       scanf("%d", &mins2);
       fflush(stdin);
    
       if ((hours2<hours) && (mins2<mins))
         {
          hours=hours2-hours;
          mins=mins2-mins;
         }
      else
        {
         hours=hours-hours2;
         mins=mins2-mins;
         hours--;
        }
    
       if (hours<0)
          hours=hours+23;
    
    
    
    
       printf("the time is now %.2d:%.2d", hours, mins);
    
    }

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Yeah, we're well aware of what you're trying to do, so how about posting your latest effort?

    And HOW many times are you going to ignore the don't use void main and fflush(stdin) ?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by davidmccabe
    I have changed the code slightly but it is still not outputting the correct value:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main(void) {
       int num;
       int x;
       int div;
         clrscr();
    
    	printf("Enter a positive integer : ");
    	scanf("%d", &num);
    	fflush(stdin);
    
    	    for (x=num; x>0; x--) {
    		 printf("%d", x);
    		 printf("*");
    		 div++;
    		 div=div*x;
    		 }
    
    	    printf(": %d", div);
    	}
    How did this process get messed up?

    Code:
    fact = 1;
    
    for(i=input; i > 0; i--)
      {
         printf("%d%s", i, (i > 1 ? "*" : ""));
         fact *= i;
      }
    printf(" = %d", fact);
    Are you sure that you want ints? This value can grow rather rapidly.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21