#include<stdio.h>
#include<math.h>
int main()


        {


    int n,nstart,nstop,nstep,i,j,zNumber;

`


        printf("Please enter a value of a positive integer nstart:    ");


    scanf("%d",&nstart);
    printf("Please enter a value of a positive integer nstop:     ");
    scanf("%d",&nstop);
    printf("Please enter a value of a positive integer nstep:     ");
    scanf("%d",&nstep);

    printf("Number\t\tzNumber\n");
    printf("------\t\t-------\n");

    n=nstart;

    zNumber= double pow(double_n,double_i);

    while(i<=nstop){
        zNumber = 1;
        i=1;
        while(i<=n){
            zNumber +=i;
            i++;
        }

    printf("%d\t\t%d\n",n,zNumber);
    n += nstep;
    }

    return 0;


    }

Recommended Answers

All 9 Replies

Do you have a query?

Include problem and errors, as well as any questions.

A first step to coding a solution to any problem is a clear understanding (and statement) of the problem.

What is the expected input (validate input to make sure ok.)

What is the desired output (is the program to loop for more input, if invalid ... or to loop again.)

A common problem, for new coders, is to handle invalid input and ... the '\n' char ... or other characters left behind in an input stream.

Once you see how to handle this ... your coding will become much more fun!

You may like to see the example below ...

/* getValidInt.c  */  /* 2012-08-11 */

/* this demo takes in valid int's and sums them ... */

/* http://developers-heaven.net/forum/index.php/topic,46.0.html */

#include <stdio.h>

int getValidInt( const char prompt[] )
{
    for( ; ; ) /* an example of a C/C++ forever loop ... until 'return' */
    {
        int numGood, testInt;
        fputs( prompt, stdout );
        fflush( stdout );
        numGood = fscanf( stdin, "%d", &testInt );
        while( getchar() != '\n' ); /* 'flush' stdin ... as we go ... */
        if( numGood == 1 ) return testInt;
        /*else ...*/
        puts( "Invalid input! Integers only please ..." );
    }
}

int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    int reply;
    fputs( "More (y/n) ? ", stdout );
    fflush( stdout );
    reply = getchar();
    if( reply != '\n' ) while( getchar() != '\n' ); /* 'flush' stdin buffer */
    if( reply == 'n' || reply == 'N' ) return 0;
    /* else ... */
    return 1;
}



int main()
{
    int count = 0, sum = 0;

    do
    {
        int testInt = getValidInt( "Enter next integer to sum: " );
        ++count;
        sum += testInt;
    }
    while( more() ); /* make sure stdin is 'empty' before calling 'more()' */

    printf( "\nFor %d numbers entered, sum was %d and average was %.2f\n",
            count, sum, (float)sum / count );

    fputs( "\nPress 'Enter' to continue/exit ... ", stdout);
    fflush( stdout );
    getchar(); /* keep 'Window' open until 'Enter' key is pressed ... */

    return 0;
}

Modify your Program 1 above so that, when executed, it will do the following in the indicated sequence:

    Ask the user to enter on the keyboard a starting value nstart of a positive integer number n.


Ask the user to enter on the keyboard the ending value nstop of n.
Ask the user to enter on the keyboard the increment nstep in n from one value to the next.
The program is then to display on the console a table consisting of two columns, one displaying values of n starting at n1 and the other displaying the corresponding zNumber values. Make sure that your display includes suitable headers for the two columns. 

sorry, the professor wants me to use looping with the while statement
i get one error saying that i should use double pow..
but that didnt work either..
thank you for the responses there is nothing major to change..i think i have a few mistakes...

... only a few ?

Coding can really be fun ... once one knows what to do :)

It may take quit a while ... for that to happen !

Writing lots of code and trying it out until one sees what each part does ... that's the way to gain confidence in your coding.

You might like to see the (edited) code below:

/*  loopToSumCubes.c */

/*
    Ask the user to enter on the keyboard a starting value nstart
    of a positive integer number n.

    Ask the user to enter on the keyboard the ending value nstop of n.

    Ask the user to enter on the keyboard the increment nstep in n
    from one value to the next.

    The program is then to display on the console a table consisting
    of two columns, one displaying values of n starting at n1
    and the other displaying the corresponding zNumber values ( sums ? )

    where ... double zNumber = pow( double_n, double_i );

    Make sure that your display includes suitable headers for the
    two columns.

*/

#include <stdio.h>
#include <math.h>



int main()
{
    int n, nstart = 1, nstop = 10, nstep = 1;

    double sum = 0.0;

    int bad1, bad2, bad3;
    do
    {
        bad1 = bad2 = bad3 = 0; /* initial all to be NOT bad ... */

        printf("Please enter a value of a positive integer nstart: ");
        scanf("%d", &nstart);
        if( nstart < 1 ) bad1 = 1;

        printf("Please enter a value of a positive integer nstop:  ");
        scanf("%d", &nstop);
        if( nstop <= nstart ) bad2 = 1;

        printf("Please enter a value of a positive integer nstep:  ");
        scanf("%d", &nstep);
        if( nstep < 1 ) bad3  = 1;

        while( getchar() != '\n' ) ; /* 'flush' stdin now ... */

        if( bad1 || bad2 || bad3 )
            printf( "\nBad data entered ... try again.\n");
    }
    while( bad1 || bad2 || bad3 );




    printf("\nNumber\t\tRunning Sum\n");
    printf("------\t\t-------\n");

    n = nstart;

    while( n <= nstop )
    {
        /* if all are to be cubed ... then summed ... */
        sum += pow( n, 3 ); 

        printf( "%d\t\t%f\n", n, sum );

        n += nstep;
    }


    printf( "\nPress 'Enter' to continue/exit ... " );
    getchar();
    return 0;
}

;/thank you for adding few things!!
well as you can tell this is my second week of coding! :D
but at first the program didnt run because i had to figure out i was supposed use 3 in pow function as 3.0 to make it double
and now...
its only calculating cube power of numbers not their sum.. :/
any idea how to solve it?

If what you want do, is to sum a series of numbers cubed, you can very simply just,

for each next real number x,

add

x*x*x to the sum.

Note: the sum was set to an inital value of 0.0

You can show the ...

------     -----      --------------------
Next x     x*x*x      Running Sum of Cubes
------     -----      --------------------

in a table format as you go ...

The example code above, outputs a similar table.

Note that the prototype for pow is:

double pow (double base, double exponent);

In C, if you pass in an int type value for base and/or exponent, each int type will be 'promoted' to type double ... inside the pow function ... since the numbers are passed in by value.

thank you so much!! Solved!

include<stdio.h>
include<math.h>
int main(){


int n,nstart,nstop,nstep,i,zNum;
int sum = 0.0;      //must initilaze and give double zero value to sum function

printf("please enter a value of a positive integer nstart: ");
scanf_s("%d",&nstart);
printf("Please enter a value of a positive integer nstop:  ");
scanf_s("%d",&nstop);
printf("Please enter a value of a positive integer nstep:  ");
            scanf_s("%d", &nstep);

printf("Number\t\tzNum\n");
printf("------\t\t----\n");

n=nstart;       //n value must start with the given nstart value

while(n<nstop){      //function loops until it reaches to value nstop
    zNum = 1;
    i=1;
    sum=0;
    while(i<=n){  
          sum += i*i*i; 
          i++;
    }
    printf( "%d\t\t%d\n", n, sum );
    n += nstep;         //every n value increase in increments of value of nstep
}


return 0;


}

Congratulations ... you got it working.

But ...

I hope you have the right solution, because that is not how I understood your coding problem ... (see above.)

Ask the user to enter on the keyboard a starting value nstart of a positive integer number n. CHECK

Ask the user to enter on the keyboard the ending value nstop of n. CHECK

Ask the user to enter on the keyboard the increment nstep in n from one value to the next. CHECK

The program is then to display on the console a table consisting of two columns, one displaying values of n starting at n1 ? (nstart?) and the other displaying the corresponding zNumber (what is a zNumber) values. Make sure that your display includes suitable headers for the two columns. ??? maybe NOT CHECK ??? depending on what is a zNumber ... and what the first column is to be ???

Note, below, the clean-ups and comments

and NEW heading on the output ...

to try to show you what your code was actually doing:

/* edited version */

#include <stdio.h>
#include <math.h>


int main()
{
    int n, nstart, nstop, nstep; /*  zNum; */ /*  NOT used here */

    printf("Please enter a positive integer for nstart : ");
    scanf("%d", &nstart);

    printf("Please enter a positive integer for nstop  : ");
    scanf("%d", &nstop);

    printf("Please enter a positive integer for nstep  : ");
    scanf("%d", &nstep);

    printf("Start\t\tStop\t\tSumOfCubes\n");
    printf("-----\t\t----\t\t----------\n");

    n = nstart; /* start with the given nstart value */

    while( n < nstop ) /* function loops until it reaches nstop */
    {      
        /* zNum = 1; */  /* Not used here ??? */
        int i = 1;
        int sum = 0;
        while( i <= n ) /* sum up 1^3 + 2^3 +... +n^3 */
        {
            sum += i*i*i; 
            ++i;
        }
        printf( "%d\t\t%d\t\t%d\n", 1, n, sum );

        n += nstep; /* n increases in increments of nstep */
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.