C Programming: Solve Math question with C Programming



Objective of this post: A guy from C community asked me to find using numbers with condition so I suggested him as code snippet below since it can be reused or enhanced..
 == My suggestion is ==
#include <stdio.h>
void vdSumA_incrementbyB_notGreaterthanC(int iArgInitial, int iArgIncrementPertime, int iArgNotgreaterThan)
{
    int iInitial=iArgInitial, iIncreasePertime=iArgIncrementPertime, iSumOf_a_to_b=iArgInitial;
    int iCount=0;
    for( iSumOf_a_to_b ; iSumOf_a_to_b+iIncreasePertime <= iArgNotgreaterThan ; (iSumOf_a_to_b+=iIncreasePertime)  )
    {
       
// uncomment it to see using number

          //printf("%d + %d = %d\n",iSumOf_a_to_b, iIncreasePertime, iSumOf_a_to_b+iIncreasePertime);
        iCount++;
    }
     printf("Initial number=%d, increase %d perTime, The last number=%d <= %d. \nTherefore used number= %d numbers\n",iArgInitial,iArgIncrementPertime,iSumOf_a_to_b, iArgNotgreaterThan,iCount+1);
}
int main(int argc, char *argv[])
{
    vdSumA_incrementbyB_notGreaterthanC(1,1,10); // 1+1(1+1)+...+n <= 10
    vdSumA_incrementbyB_notGreaterthanC(1,2,10); // 1+(1+2)+...+n <=10
    vdSumA_incrementbyB_notGreaterthanC(1,1,3545); // 1+(1+1)+...+n <=3545
    printf("\n\nYou may test it, debug or simplify by using For loop, algorithm or any purposes");
    return 0;
}


Compile with DevC++ in my computer




OnlineGCC version C99 compiler at https://www.jdoodle.com/c-online-compiler

You may leave comment to improve it
Thank you so much from THAILAND
















Popular posts from this blog

C Programming: How to improve C to support more Array N dimensions

C Programming: Superb E-Book from beginner to expert [C career semi retired]

C Programming: Understand For loop syntax in deep detail