User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2004
Posts: 2
Reputation: Ginger is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Ginger Ginger is offline Offline
Newbie Poster

Using a for loop to sum an integer n and call function add_it

  #1  
Sep 26th, 2004
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 220
Reputation: frrossk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 9
frrossk's Avatar
frrossk frrossk is offline Offline
Posting Whiz in Training

Re: Using a for loop to sum an integer n and call function add_it

  #2  
Sep 27th, 2004
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:54 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC