Function Programming in C : Prob-5

Evaluate below equation using c function.

sum=x + x^2  +  x^3  + ...........+ x ^ n.

Source Code in C:

#include<stdio.h>
int main()
{
     int a;
     printf("\nEnter your number : ");
     scanf("%d",&a);
     sum(a);
}

int sum(int b)
{
     int i,c=1,sum=0;
     for(i=1; i<=5; i++)
         {
              c=b*i;
              sum+=c;
         }
   
    printf("\nThe sum is: %d", sum);
}

16 comments:

  1. #include
    #include
    main()
    {
    int a;
    printf("Enter your number :");
    scanf("%d",&a);
    sum(a);
    }

    int sum(int b)
    {
    int i,c=1,sum=0;
    for(i=1;i<=5;i++)
    {

    c=b*i;
    sum+=c;
    }
    printf("%d",sum);
    }

    ReplyDelete
  2. #include
    main()
    {
    int a,b;
    printf("Enter the value of x : ");
    scanf("%d",&a);
    printf("\nEnter series's boundry : ");
    scanf("%d",&b);
    printf("\nThe serise is : \n");
    serise(a,b);
    }

    int serise (int x, int y)
    {
    int i,sum=0,j;
    j=x;
    for(i=1;i<=y;i++)
    {
    if(i==1)
    printf(" \n%d + ",x);
    else if(i==y)
    printf("%d^%d = ",x,y);
    else
    printf("%d^%d + ",x,i);
    sum+=j;
    j=j*x;
    }
    printf("%d\n",sum);
    }

    ReplyDelete
  3. #include
    #include
    int *funcx(int a, int b);
    int main()
    {
    int m,n,result;
    int (*pFn)(int,int);

    printf("\n Enter value and limit:");

    scanf("%d %d",&m,&n);
    pFn=funcx;
    result=(*pFn)(m,n);
    printf("\n The sum is %d",result);
    getch();
    return 0;
    }

    int *funcx(int a,int b)
    {
    int i,sum=0,j;
    j=a;
    for(i=1;i<=b;i++)
    {
    if(i==1)
    printf("\n %d+",a);
    else if(i==b)
    printf("%d^%d",a,i);
    else
    printf("\n %d^%d+",a,i);
    sum+=j;
    j*=a;
    }
    return sum;


    }

    ReplyDelete
  4. #include
    #include
    main()
    {
    int n;
    printf("Enter your number :");
    scanf("%d",&n);
    sum(n);
    }

    int sum(int b)
    {
    int i,p=1,sum=0;
    for(i=1;i<=5;i++)
    {

    p=b*i;
    sum+=p;
    }
    printf("%d",sum);
    }

    ReplyDelete


  5. #include
    #include
    int funcx(int m,int n);
    int main()
    {
    int k,l,result;
    printf("\n Enter values:");
    scanf("%d",&k);
    printf("\n Enter limit:");
    scanf("%d",&l);
    result=funcx(k,l);
    printf("\n The sum is %d",result);
    getch();
    return 0;

    }

    int funcx(int m,int n)
    {
    int i,sum=0,p;
    p=m;
    for(i=1;i<=n;i++)
    {
    if(i==1)
    printf(" %d+",m);
    else if(i==n)
    printf("%d^%d",m,i);
    else
    printf("%d^%d+",m,i);
    sum+=p;
    p*=m;
    }

    return sum;
    }

    ReplyDelete
  6. #include

    void serise (int x, int y)
    {
    int i,sum=0,j;
    j=x;
    for(i=1; i<=y; i++)
    {
    if(i==1)
    printf(" \n%d + ",x);
    else if(i==y)
    printf("%d^%d = ",x,y);
    else
    printf("%d^%d + ",x,i);
    sum+=j;
    j=j*x;
    }
    printf("%d\n",sum);
    }

    void main()
    {
    int a,b;
    printf("Enter the value of x : ");
    scanf("%d",&a);
    printf("\nEnter series's boundary : ");
    scanf("%d",&b);
    printf("\nThe series is : \n");
    serise(a,b);;
    }

    ReplyDelete
  7. Id :201430310
    Name : Mehrin
    #include
    using namespace std;
    int equation_sum(int a)
    {
    int i,p=1,sum=0;
    for(i=1;i<=a;i++)
    {
    p =a*i;
    sum =p;
    }
    cout<>a;
    equation_sum(a);
    }

    ReplyDelete
  8. Id-201210803

    #include
    using namespace std;

    int sum(int b)
    {
    int i,c=1,sum=0;
    for(i=1;i<=5;i++)
    {
    c=b*i;
    sum+=c;
    }
    cout<>a;
    sum(a);
    }

    ReplyDelete
  9. #include
    using namespace std;

    class summation
    {
    public:
    int number,range,sum,mul;
    summation();
    void process();
    void result();
    };
    summation::summation()
    {
    cout<<"Enter a number: ";
    cin>>number;
    cout<<"Enter a range: ";
    cin>>range;
    }

    void summation::process()
    {
    mul=1;
    sum=0;
    int i;
    for(i=1;i<=range;i++)
    {
    mul=mul*number;
    sum=sum+mul;
    }
    }
    void summation::result()
    {
    cout<<"Summation : "<<sum;
    }
    main()
    {
    summation ob;
    ob.process();
    ob.result();
    }

    ReplyDelete
  10. #include
    using namespace std;
    class equation
    {
    public:
    int num,sum=0,mul=1,n;
    equation();
    void show();
    ~equation();
    };
    equation::equation()
    {
    cout<<"Enter the number: ";
    cin>>num;
    cout<>n;
    cout<<endl;
    }
    void equation::show()
    {

    for(int i=1; i<=n; i++){
    mul=mul*num;
    sum=sum+mul;
    }
    cout<<sum<<endl;
    }
    equation::~equation()
    {

    }
    int main()
    {
    equation obj;
    obj.show();
    return 0;
    }

    ReplyDelete
  11. #include
    using namespace std;
    class expo
    {
    public:
    int i,sum=0,b,r;
    expo();
    void show();

    };
    expo::expo()
    {
    cout<<" First Input Base Nimber Than Input Range: ";
    cin>>b>>r;
    }
    void expo::show()
    {
    for(i=1;i<=r;i++)
    sum=sum+b*i;
    cout<<"Result is : "<<sum<<endl;
    }
    int main()
    {
    expo appel;
    appel.show();
    }

    ReplyDelete
  12. using C++

    #include
    using namespace std;

    class cons
    {
    public:
    int pow, firstNum;
    cons(int i, int j);
    ~cons();

    void result();
    };

    cons::cons(int i, int j)
    {
    pow = i;
    firstNum = j;
    }

    void cons::result()
    {
    int sum = 0, total = 1, i, j;
    for (i = 1; i <= pow; i++){
    total *= firstNum;
    sum += total;
    }
    cout << "Total value is --: " << sum << "\n";
    }

    cons::~cons()
    {
    cout << "\n[+]MEMORY RELEASED[+]";
    }

    int main()
    {
    int i, j;
    cout << "Enter your number --: ";
    cin >> i;
    cout << "Enter your power --: ";
    cin >> j;

    cons obj(j ,i);
    obj.result();

    return 0;
    }

    ReplyDelete
  13. using constructor
    mamun ur rashid

    #include
    using namespace std;
    class eqation
    {
    public:
    int i,c=1,b,t=0;
    eqation();

    ~eqation();
    void result();
    };
    eqation::eqation()
    {
    cout<<"Enter a number :" ;
    cin>>b;
    }
    eqation::~eqation()
    {

    }
    void eqation::result()
    {

    for(i=1;i<=5;i++)
    {
    c=b*i;
    t+=c;
    }
    cout<<t;
    }

    int main()
    {
    eqation sb;
    sb.result();
    }

    ReplyDelete
  14. #include
    using namespace std;
    class equation
    {
    public:
    int num,pow,i,sum=0,total=1;
    equation();
    ~equation()
    {
    cout<<"Distructor active";
    }
    int func()
    {
    for(i=1;i<=pow;i++)
    {
    total*=num;
    sum+=total;
    }
    cout<<"The sum is: "<>num;
    cout<<"Enter the power: ";
    cin>>pow;
    }

    int main()
    {
    equation obj;
    obj.func();
    return 0;
    }

    id:201510660

    ReplyDelete
  15. #include
    using namespace std;
    class equation
    {
    public:
    int num,pow,i,sum=0,total=1;
    equation();
    ~equation()
    {
    cout<<"Distructor active";
    }
    int setu()
    {
    for(i=1;i<=pow;i++)
    {
    total*=num;
    sum+=total;
    }
    cout<<"The sum is: "<>num;
    cout<<"Enter the power: ";
    cin>>pow;
    }

    int main()
    {
    equation obj;
    obj.setu();
    return 0;
    }

    id:201510992

    ReplyDelete
  16. using c++

    #include
    using namespace std;

    class cons
    {
    public:
    int pow, firstNum;
    cons(int i, int j);
    ~cons();

    void result();
    };

    cons::cons(int i, int j)
    {
    pow = i;
    firstNum = j;
    }

    void cons::result()
    {
    int sum = 0, i, total = 1;
    for (i = 1; i <= pow; i++){
    total = firstNum*pow;
    sum += total;
    }
    cout << "Total value is --: " << sum << "\n";
    }

    cons::~cons()
    {
    cout << "\n[+]MEMORY RELEASED[+]";
    }

    int main()
    {
    int i, j;
    cout << "Enter your number --: ";
    cin >> i;
    cout << "Enter your power --: ";
    cin >> j;

    cons obj(j ,i);
    obj.result();

    return 0;
    }

    ReplyDelete