Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C programming HELP!!

Name: Ahh 2012-05-21 1:48

How do I make the following code using C programming:
Create a program that has the following fields & capabilities:

fields:

type purpose
int Storage for the target sum

methods

name return type purpose parameter(s)
main void Gets the target sum from the user
and then calls getSeries. none
getSeries void Determines the first and last integers in EVERY series of positive, consecutive INTEGERS whose sum is the target sum. Output sent to System.out object. none

For example, if the target sum is 30, your program would output the following:
THE FIRST AND LAST NUMBERS IN EVERY SERIES OF POSITIVE, CONSECUTIVE INTEGERS WHOSE SUM IS 30 ARE:
4...8
6...9
9...11

Name: Anonymous 2012-05-21 3:51

#include <stdio.h>

void getSeries(int target)
{
        printf("THE FIRST AND LAST NUMBERS IN EVERY SERIES OF POSITIVE, CONSECUTIVE INTEGERS WHOSE SUM IS %d ARE:\n",target);
        for(int numToTry=1;numToTry<=target/2;numToTry++)
        {
                int sum = numToTry;
                int endNum = numToTry;
                while (sum<target)
                {
                        endNum++;
                        sum+=endNum;
                        if(sum==target)
                        {
                                printf("%d...%d\n",numToTry,endNum++);
                        }
                }
        }
}
int main()
{
        int inp;
        printf("Input your number: ");
        scanf("%d",&inp);
        getSeries(inp);
        return 0;
}


compiles for me with "gcc -std=99". To make it compatible with C89 you'll have to forgo the implicit declaration in that for loop.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List