'Write a program in C to display the n terms of even and odd natural number and their sum using functions
#include <stdio.h>
int sum_even(int n);
int sum_odd(int m);
int main() {
int n;
scanf("%d", &n);
int m;
scanf("%d", &m);
int evensum;
evensum = sum_even(int n);
int oddsum;
oddsum = sum_odd(int m);
printf("the sum of even numbers is %d", evensum);
printf("the sum of odd numbers is %d", oddsum);
return 0;
}
int sum_even(int n) {
int sum = 0, i;
for (i = 2; i <= n; i += 2) {
sum += i;
}
return sum;
}
int sum_odd(int m) {
int SUM = 0, j;
for (j = 1; j <= m; j = j + 2) {
SUM = SUM + j;
}
return SUM;
}
please tell me what is wrong with my code, I am new to coding, I am able to solve questions without using functions but I am confused when I have to use functions
Solution 1:[1]
While calling functions you just pass the arguments and not specify its data type.
i.e. You would write it like this :-
evensum = sum_even(n);
oddsum = sum_odd(m);
The general syntax of calling a function which returns a value is :-
return-value = function-name(arg-list);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Suraj Upadhyay |