0

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.

2
Contributors
1
Reply
10
Views
3 Years
Discussion Span
Last Post by Ancient Dragon
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.