#include<>stdio.h
main()
{
int n,s=0,i=1;
L1:printf("enter a range of numbers:");
scanf("%d",&n)
if(n<1){printf("\wrong input");
goto L1;}
while
{
s+=i;
i+=2;
}
printf("\nthe sum of the series is=%d,s");
getch();
}

the above program is meant to find out the sum S=1+3+5+7+....+N
can any on explain me,the significance of talking i+=2??

Recommended Answers

All 21 Replies

well in my opinion the way you use your while loop is wrong but if you care to know why i+=2 has to be there. i will say it is there to increment by two each time the while loop runs but there no way on earth your code is going to prints a series no equation for that.

can any on explain me,the significance of talking i+=2??

It's a shorthand version of i = i + 2;

printf("Sum of sequence is %i.", (n+1) * (n+1) / 4))

is the given code correct??
there is no condition for the while loop
it will be displaying an error

In while no condition.:icon_surprised:

printf("\nthe sum of the series is=%d,s");

it should be

printf("\nthe sum of the series is=%d",s);

#include<>stdio.h

it should be

#include<stdio.h>

Your correction version of code:-

#include<stdio.h>
main()
{
int n,s=0,i=1;
L1:printf("enter a range of numbers:");
scanf("%d",&n);
if(n<1){printf("\nwrong input");
goto L1;}
while(n--)
{
s+=i;
i+=2;
}
printf("\nthe sum of the series is=%d",s);
}

now

the above program is meant to find out the sum S=1+3+5+7+....+N
can any on explain me,the significance of talking i+=2??

Here in the sum of n odd positive numbers, numbers are incremented by difference of 2. so every time i is incremented by 2. i+=2 is shorthand of i=i+2;

can any on explain me,the significance of talking i+=2??

it is used for getting the number 3 after 1.If you use i++, it means 1 then 2then 3 etc.
But your program does not do the right.

The actual logic is....

int main()
{
	int num,sum=0,m,n;
	scanf("d",&n);
	m=1;
	while(m<=n)
	{sum=sum+m;
	m+=2;
	}
printf("d",sum);

The actual logic is....

int main()
{
	int num,sum=0,m,n;
	scanf("d",&n);
	m=1;
	while(m<=n)
	{sum=sum+m;
	m+=2;
	}
printf("d",sum);

yes this has to be done

The actual logic is....

yes this has to be done

I am taking summation upto n terms..

I am taking summation upto n terms..

ok

I am taking summation upto n terms..

My code also do the n term. Here n has a value.

My code also do the n term. Here n has a value.

check output for n=3 and n=4

check output for n=3 and n=4

Can you say whats wrong with n=3 and n=4 ?
I do not find any thing wrong.

I have mistecked in my first post of this thread that is
scanf("%d",&n)

Can you say whats wrong with n=3 and n=4 ?

If you are talking summation of n terms your code outputs 4 for n=3 and outputs 4 for n=4.
means 3rd and 4th terms are 4.
where summation of 3 terms is :- 1+3+5=9.
and summation of 4 terms is :- 1+3+5+7=16.

If you are talking summation of n terms your code outputs 4 for n=3 and outputs 4 for n=4.

Misunderstanding!!!!
In my code the "n" is the last digit(range) of the sequence.
Like....
for n=4....1+3=4
and for n=5....1+3+5=9.
If you want like your logic , sum 0f the first n digits where I+=2, it will possible also, I need to coding again.

No dear ,it was not my logic I was correcting his logic and making him understand about the increment.

my code is:

#include<stdio.h>
int main()
{
        int n;
        scanf("%d",&n);
        printf("sum of 1st odd n terms:-%d",n*n);
        return 0;
}

it was not my logic I was correcting his logic and making him understand about the increment.

Yea I understood.I have said the same thing in my last post>

Misunderstanding!!!!

Yep that was there which I want to tell you I am talking about terms.

If you want like your logic , sum 0f the first n digits where I+=2, it will possible also, I need to coding again.

I dont understand what are you trying to prove or show...???
I am here just for learning and helping others if i can not for an argument and frankly speaking I dont like the way u talked.

I request administrator to please stop such things and proudy talks here, may hurt any one.

Hey cse.avinash
I just say I do not understand what you want to say with my code such as check output for n=3 and n=4 .Your concept was not clear to me.But

If you are talking summation of n terms your code outputs 4 for n=3 and outputs 4 for n=4.
means 3rd and 4th terms are 4.
where summation of 3 terms is :- 1+3+5=9.
and summation of 4 terms is :- 1+3+5+7=16.

It has kept clear all to me.
This is our misunderstanding nothing else.
OK.

Onlineshade

Hmm okay... :)

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.