|Display series and sum of 1+3+..+n in c| | display n number of series in c|

C PROGRAM TO DISPLAY SERIES AND FIND SUM OF 1+3+5+....+n.

Code:

#include<stdio.h>
int main()
{
int i,n,sum=1;
printf("Enter any number n:");
scanf("%d",&n);
    printf("1");
for(i=3;i<=n;i++)
{
printf("+ %d ",i);
sum=sum+i;
i++;
}
printf("\nSum is: %d", sum);
}

Explanation:

  • First we are declaring the variables in integer datatype.
  • Next is we are asking the user to enter a number so that we can find the sum of series and display it.
  • We are storing the user entered data in the variable "n".
  • using a for loop to perform the sum of series
  • Finally, displaying the result with the sum of series.

OUTPUT:


Comments

Popular posts from this blog

Use of Backslash "\n" in C language

COHESION AND COUPLING material

Coding and Testing in software engineering