Program to display series and find sum of 1+3+5+……..+n.

Program to display series and find sum of 1+3+5+……..+n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf(“Enter any no: ”);
scanf(“%d”,&n);
for(i=1;i<n;i=i+2)
{
printf(“%d+”,i);
sum=sum+i;
}
printf("%d",n);
printf("\nsum=%d",sum+n);
getch();
}
Output:
Enter any no: 7
1+3+5+7
Sum=16

No comments:

Post a Comment