PDA

View Full Version : C pro



Aladdin
03-01-2011, 17:53
Hey Guy's , thanks for the help you're doing for me.

So, How can I write this C programming using for I think.

An alternating sequence : 1 - 1/2 + 1/3 - 1/4 ....... (-1)^n+1(1/n).

Also , how can I draw a triangle using for or while or any other statement.

Thanks for the help , I'm you'll help me with this,please.

danbaron
04-01-2011, 08:41
#include <stdio.h>
#include <math.h>

double seriessum(long n);
void drawtriangle(long n);

//--------------------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
double total;
long num;
num = 79;

total = seriessum(num);
printf("for %d terms, the series sum = %18.14f\n", num, total);

drawtriangle(num);

return 0;
}

//--------------------------------------------------------------------------------------------

double seriessum(long n)
{
long i;
double sum;
if(n<=0)return 0;
for (i=1;i<=n;i++) sum +=pow(-1.0, i+1)/i;
return sum;
}

//--------------------------------------------------------------------------------------------

void drawtriangle(long n)
{
long i, j;
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)printf("*");
printf("\n");
}
for(i=n-1;i>=1;i--)
{
for(j=1;j<=i;j++)printf("*");
printf("\n");
}
printf("\n");
printf("\n");
}

//--------------------------------------------------------------------------------------------