Here is the question: Write a function which accepts an integer N and returns the sum of first N numbers.

and here are my codes:

#include<stdio.h>
#include<stdlib.h>
int Myfun(int x);
int main()
{
    int a, sum;
    printf("Enter an integer: ");
    scanf("%d", &a);

    printf("sum is %d\n", sum);

    system("pause");
    return (1);

}
int Myfun(int x)
{
    int a, count, sum=0;

    for(count=1;count<=a;count++){
        sum=sum+count;

        }
       return (sum);

    }

The problem here is i do not get the correct output. If i enter 4 it should have give the output 10 but it gives a 0. I need help if there are missing codes are any mistake help me.

main() is not calling MyFunc(). You need to add a call to that function between lines 8 and 10.

MyFunc() doesn't use the paramter x in the loop on line 20. Sould be: count <= x

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.