Results 1 to 2 of 2

Thread: C pro

  1. #1

    C pro

    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.

  2. #2
    thinBasic MVPs danbaron's Avatar
    Join Date
    Jan 2010
    Location
    California
    Posts
    1,378
    Rep Power
    151
    #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");
    }
    
    //--------------------------------------------------------------------------------------------
    
    "You can't cheat an honest man. Never give a sucker an even break, or smarten up a chump." - W.C.Fields

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •