problem , need pointers. thanks

This is a discussion on problem , need pointers. thanks within the C Programming forums, part of the General Programming Boards category; Hi, This is my first time posting, i'm having some problem with some school work. I was wondering if anyone ...

  1. #1
    mobius
    Guest

    Question problem , need pointers. thanks

    Hi,
    This is my first time posting, i'm having some problem with some school work. I was wondering if anyone could give me some pointers. greatly appreciated. thank you in advance.

    Determine the sum of integers: read in a positive integer n, determien the sum of the first n odd integers, the sum of the first n even integers, and then print out these sums.
    eg: input 5.
    the sum of the integers 1,3,5,7 and 9, and determine the sum of the first five even integers, the sum of the integers 2,4,6,8 and 10.


    What methods should i use to solve this problem?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    use loops.... look at this snippet..
    int number,i;
    printf("enter number");
    scanf("%d",&number);
    for (i=1;i<(2*number);i+=2)
    printf("%d\n",i);

    thats odd done.... now you do even...
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    mobius
    Guest
    Hi Stoned_coder,
    Thank you for your help.
    Heres what I did.
    /**************************************************/
    int number,i,ii;
    printf("You have chosen option [1].\n");
    printf("\n");
    printf("Please enter a positive number: ");
    scanf("%d",&number);
    printf("\n");
    printf("The sum of odd integers are: ");
    for(i=1; i<(2*number);i+=2)
    printf("%d ", i);
    printf("\n");

    printf("The sum of even integers are: ");
    for(ii=2; ii<(2*number);ii+=2)
    printf("%d ", ii);
    printf("\n\n");
    /**************************************************/

    output:
    Please enter a positive number: 5

    The sum of odd integers are: 1 3 5 7 9
    The sum of even integers are: 2 4 6 8

    -------------

    Why does it only output 4 sums for the even integer? I don't get it. thanks

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok firstly i was only showing you the method... you are not summing up the first five numbers but printing them.... easily fixed tho.
    also on the even condition.... think about it... there are 2 options...
    1) condition becomes ii<=(2*number)
    2) condition becomes ii<(2*number+1)

    Code:
    int number,i,ii,sum; 
    printf("You have chosen option [1].\n"); 
    printf("\n"); 
    printf("Please enter a positive number: "); 
    scanf("%d",&number); 
    printf("\n"); 
    printf("The odd integers are: "); 
    sum=0;
    for(i=1; i<(2*number);i+=2) 
    {
    sum+=i;
    printf("%d ", i);
    } 
    printf("\n"); 
    printf("The sum is %d\n",sum);
    printf("The even integers are: "); 
    sum=0;
    for(ii=2; ii<(2*number+1);ii+=2) 
    {
    sum+=ii;
    printf("%d ", ii); 
    }
    printf("\nThe sum is %d\n",sum);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    mobius
    Guest

    Talking

    ah...thanks for the help stoned_Coder, greatly appreciated. thanks mate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 11:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 06:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 05:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM

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