Monday, June 18, 2012

To find the sum of first n numbers (using while)


/* To find the sum of first n numbers */

#include<stdio.h>

#include<conio.h>

void main()

{

  int n,i,sum=0;

  clrscr();

  printf("\nEnter the value of n: ");

  scanf("%d",&n);

  i=1;

  /* Using WHILE statement */

  while(i<=n)

  {

    sum=sum+i;

    i=i+1;

  }

  printf("\nSum of first %d numbers = %d",n,sum);

  getch();

}

 

OUTPUT

 

Enter the value of n: 10

 

Sum of first 10 numbers = 55

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.