•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,582 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,683 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2949 | Replies: 1
![]() |
•
•
Join Date: Sep 2004
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
I am to write a program to read a non-negative integer n and call function add_it() to calculate the sum ... If n is 5, the sum 1+2+3... would be computed. n must be less than 8943923. Use a for loop, rather than the summation formula. I am to put main() and add_it() in the same file by calling add_it(). The following is what I have come up with, but I am getting the wrong answer main() {
int main() {
int n,sum; {
printf("Enter a non_negative number to sum:\n");
scanf("%d", &n);
int add_it() {
int n,sum; {
n<8943923;
for (n=0;
n<8943923;
n*(n+1)/2) {
printf("%d\n",n);
sum=n*(n+1)/2;
if (n<8943923) break;
}}}
printf("The summation of %d is %d\n",n,sum);
return(sum);
}}
When I execute the program I get the following:
Enter a non-negative number to sum: (I enter say 20)
The summation of 20 is 4.
This is what I want except the answer should not be 4.
Can anyone help me figure out where I went wrong in this for loop and my equation for getting the sum? Thank you to all who may respond.
int main() {
int n,sum; {
printf("Enter a non_negative number to sum:\n");
scanf("%d", &n);
int add_it() {
int n,sum; {
n<8943923;
for (n=0;
n<8943923;
n*(n+1)/2) {
printf("%d\n",n);
sum=n*(n+1)/2;
if (n<8943923) break;
}}}
printf("The summation of %d is %d\n",n,sum);
return(sum);
}}
When I execute the program I get the following:
Enter a non-negative number to sum: (I enter say 20)
The summation of 20 is 4.
This is what I want except the answer should not be 4.
Can anyone help me figure out where I went wrong in this for loop and my equation for getting the sum? Thank you to all who may respond.
Maybe this will work:
#include <stdio.h>
#include <stdlib.h>
int add_it(int n);
int main()
{
int n,sum;
printf("Enter a non_negative number to sum:\t");
scanf("%d", &n);
if (n >= 8943923) printf ("n too big\n");
else
{
sum = add_it (n);
printf("The summation of %d is %d\n",n,sum);
}
system ("PAUSE");
}
int add_it(int n)
{
int sum=0, i;
for (i=1; i<=n; i++)
{
printf("%d\n", i);
sum+=i;
}
return sum;
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
- Warning: Unknown(): Unable to call () - function does not exist in Unknown on line 0 (PHP)
- how to call a function in href? (ASP.NET)
- Need help with some syntax errors (C++)
- Call Function (ASP.NET)
- how to call this function...using menu..! (C++)
Other Threads in the C Forum
- Previous Thread: how to place a specific info from input file
- Next Thread: A puzzl about function


Linear Mode