Hi

I am working on a program that computes the sum of N in increments of 3. I do not have any errors but when you run the program it does not compute correctly for example if you enter 2 for n the sum should be 5

#include "stdafx.h"
#include "stdio.h"

int
main(void)
{
int n, i, sum;
sum = 1;
printf(" Please enter a positive number\n");
scanf("%d", &n);
for(i=0; i<=n; i++);
{sum+=i;
}
printf("sum is %d", sum);
	  

	return 0;
}

Recommended Answers

All 15 Replies

I don't see any increments of three anywhere.
Shouldn't sum be initialized to zero?

where would the increments of 3 go in the program?

#include "stdafx.h"
#include "stdio.h"

int
main(void)
{
int n, i, sum;
sum = 0;
printf(" Please enter a positive number\n");
scanf("%d", &n);
for(i=0; i<=n; i++);
{sum+=i;
}
printf("sum is %d", sum);
	  

	return 0;
}

If what you want is to just increment that n by three several times then: n = n + 3; through the loop. If initially sum = n; then sum = sum + 3 is a possibility. But I don't think n becoming the flag that would stop that for loop is what you want.
In any case, for(i=0; i<=n; i++); makes the loop do nothing meaningful.

Hi

I am working on a program that computes the sum of N in increments of 3. I do not have any errors but when you run the program it does not compute correctly for example if you enter 2 for n the sum should be 5

#include "stdafx.h"
#include "stdio.h"

int
main(void)
{
int n, i, sum;
sum = 1;
printf(" Please enter a positive number\n");
scanf("%d", &n);
for(i=0; i<=n; i++);
{sum+=i;
}
printf("sum is %d", sum);
	  

	return 0;
}

You know what... we didn't get your question correctly... you said if the input is 2, output should be 5... it simply means you have to add 3 to n, n+3=output... is this what you want?
Or like you said increments of 3, as in... you give 2 out put should be (1+3)+(2+3)=9. if n is the input then (1+3)+(2+3)+(3+3)+(4+3)+(5+3)...+(n+3)=output, is this what you want?... Post the formula so that we can help you...

What I want the program to do is 1+4+7+10+13... in increments of 3. Does that help?

Yes that helps...

int main(int argc, char **argv)
{
	int i;
	int n = 3;
	int sum = 1;
	int total = 0;

	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;/* To take care of 1 */
	printf("\n Total %d",total);
	getchar();
	return 0;
}

Let me know if its work...

commented: Solving someone's homework for them -3

Thank you for the help what I want is the user to be able to type a positive integer the for it to sum the n terms for example N=3 so the sum would be 12 or N=2 sum would be 5.
Yes it does work thank you so much! I learned a lot!

Yes it would do what you want... here just modified it to enable it for the user to enter the value...

int main(int argc, char **argv)
{
	int i;
	int n;
	int sum = 1;
	int total = 0;

	printf("\n Please enter a positive integer : ");
	scanf("%d",&n);
	if(n <= 0){
		printf("\n Error!!!");
		return 1;
	}

	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;/* To take care of 1 */
	printf("\n Total %d",total);
	getchar();
	return 0;
}

Hello,

Thank you for all your help someone brought to my attention that I need to write a function sum that will be called by main().

How can I do that with the code I have done already

#include "stdafx.h"
#include "stdio.h"

int
main(void)
{
int i;
	int n = 3;
	int sum = 1;
	int total = 0;
 printf ("Please enter a positive integer\n");
 scanf ("%d", &n);
	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;
	printf("total %d",total);
	getchar();
	return 0;
}

Hello,

Thank you for all your help someone brought to my attention that I need to write a function sum that will be called by main().

How can I do that with the code I have done already

Do yourself a favor. Don't cheat yourself out of the opportunity of learning about functions. It is a vital part of creating good programs. Take the time to learn it well. Practice, practice, practice, if you are serious about learning.
Here's a basic tutorial. Use your favorite search engine for more. Post examples if you don't understand. Don't ask anyone to do it for you.

Here's what I came up with:

#include "stdafx.h"
#include "stdio.h"

/*Function Prototype*/
int find_sum (int i);

int main()
{
int i, n = 3,sum = 1, total = 0;
	 
 printf ("Please enter a positive integer\n");
 scanf ("%d", &n);

		sum = find_sum + 3;
		total = total+sum;

	printf("total %d",total);
	return 0;
}

int find_sum (int i);
{
	int i, n,sum,total;
	n=3;
	
	for(i=1; i<n; i++) //algorithm
	{
		sum = sum + 3;
		total = total+sum;
	}
	total++;
	printf("total %d",total);
	return 0;
}

Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'

Move the for loop to the function you want... like...

int sum(int n){
int i = 0;
int total = 0;
int sum = 1;
	for(i=1; i<n; i++)
	{
		sum = sum + 3;
		total = total+sum;
	}
total++;

return total;

}

int main(int argc, char **argv)
{
	int i;
	int n;
	int sum = 1;
	int total = 0;
 
	printf("\n Please enter a positive integer : ");
	scanf("%d",&n);
	if(n <= 0){
		printf("\n Error!!!");
		return 1;
	}

total = sum(n);
	printf("\n Total %d",total);
	getchar();
	return 0;
}

Here's what I came up with:

//a lot of code

Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'

On line 22 you are declaring function. There's no semicolon in the end.
Main is also a function. Do you write: int main();

Here are the 2 errors- error C2296: '+' : illegal, left operand has type 'int (__cdecl *)(int)' and cpp(27) : error C2082: redefinition of formal parameter 'i'

You're still compiling your C code as C++ code.

Hi

I think there is some problem in your coding.As you declared sum=1,in loop the value of sum is incremented.not the value of n is incrementing.Do some changes in code that will help you to get the desired input.

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.