#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??

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;

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);

Edited 5 Years Ago by Onlineshade: n/a

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

Edited 5 Years Ago by sharathg.satya: n/a

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

Edited 5 Years Ago by Onlineshade: n/a

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.

Edited 5 Years Ago by Onlineshade: n/a

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.

Edited 5 Years Ago by Onlineshade: n/a

This article has been dead for over six months. Start a new discussion instead.