Help Please!

This is a discussion on Help Please! within the C Programming forums, part of the General Programming Boards category; if anyone can please help i have to turn in this program by 2moro night and ive been up all ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    Help Please!

    if anyone can please help i have to turn in this program by 2moro night and ive been up all night trying to figure it out. i had to write a program to find all the factors of any given number typed as long as it is a possitive interger. then i have to 1. list the possitive intergers. 2. show how many intergers there are. 3 add all the intergers and 4 multiply them. i will paste what i have so far but i am having trouble with showing 2-4! please just give me advice or anything. thanx

    Code:
    #include <stdio.h>
    
    int main () {
        int val, factor, sum;
        int product, total;
        printf("Enter a positive integer greater than one: \n");
        scanf("&#37;d", &val);
        
        factor=1;
        
        if(val<0){
                  printf("Sorry, that input is not valid. \n""Enter a positive integer greater than one.");
                  }
                  else{
        
        printf("Here is a list of the possitive intregers of %d\n", val);
        while(factor<=val){
        
                 if(val%factor==0){
                
                 printf("%d ", factor);    
                                  
                                  }//if end
        factor=factor+1;
        }//while end
    
    //sum               
        sum=1;
        while(sum<=val);{
                          if(val%sum==0){                                  
                                         }                 
    total= sum+sum;
    
    
    
    }//while end
    printf("%d\n",total);
    }//else end
        printf("\n");
        
        
        system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    you're code arrengement looked horrible to me... Take a look at this: http://irc.essex.ac.uk/www.iota-six...._operators.asp

    Code:
    #include <stdio.h>
    
    int main (void) {
    
        int val, factor=1,count=0,sum=0,multip=1;
    
        printf("Enter a positive integer greater than one: \n");
        scanf("%i", &val);
        
        if(val<0){
                  printf("Sorry, that input is not valid. \n""Enter a positive integer greater than one.\n");
                  return 0;
                       }
                  
        
       printf("\nHere is a list of the possitive integers for %i:\n", val);
     
       while(factor<=val){
                        if(val%factor==0){
                              printf("%i\n", factor);    
                              count++;         /*counts how many integers we found*/
                              sum+=factor;  /*sums them*/
                              multip*=factor; /*multiplies them*/
                                                      }
                        factor++;
                                        }
    
        printf("We have a total of %i integeres.\nWhich are summed together: %i and multiplied:%i\n",count,sum,multip);
        
        return 0;
    }
    Last edited by lilcoder; 09-29-2007 at 04:42 AM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    yeah i know. this is my second program ever so i guess i can use that as an exuse. i got the program to add and multiply but i stayed up unitl 4 trying to figure out how to get it to count. i was going to organize it when i finished though, and play with it so i could have only one while loop. i just wanted it to do what i asked first no matter how sloppy it was. thanx for the help though!

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Quote Originally Posted by lilcoder View Post
    you're code arrengement looked horrible to me... Take a look at this: http://irc.essex.ac.uk/www.iota-six...._operators.asp

    Code:
    #include <stdio.h>
    
    int main (void) {
    
        int val, factor=1,count=0,sum=0,multip=1;
    
        printf("Enter a positive integer greater than one: \n");
        scanf("%i", &val);
        
        if(val<0){
                  printf("Sorry, that input is not valid. \n""Enter a positive integer greater than one.\n");
                  return 0;
                       }
                  
        
       printf("\nHere is a list of the possitive integers for %i:\n", val);
     
       while(factor<=val){
                        if(val%factor==0){
                              printf("%i\n", factor);    
                              count++;         /*counts how many integers we found*/
                              sum+=factor;  /*sums them*/
                              multip*=factor; /*multiplies them*/
                                                      }
                        factor++;
                                        }
    
        printf("We have a total of %i integeres.\nWhich are summed together: %i and multiplied:%i\n",count,sum,multip);
        
        return 0;
    }

    I suppose that if i have completely understand what you want to do, your strategy is wrong.
    Consider a number like 8, first factor is 2, you have 8 / 2 = 4, for the next factor you set the factor to factor++ so you have the number 3 to get divided with the number 4. Thats error as the next number that factor is is still the number 2.
    8 = 2 * 2 * 2;
    Also should you consider creating a function to tell if is a prime number or not, if the given number is prime then say it is prime and print it.
    Think of it.

Popular pages Recent additions subscribe to a feed

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