Pages

Monday, January 2, 2012

C Programming to find sum of first n odd natural number


/*to find the sum of first n odd natural number*/
#include<stdio.h>
#include<stdlib.h>
int main( )
{
    int i,j=1,n,sum=0;
    printf ("enter n: ");
    scanf ("%d",&n);
    for (i=1; i<=n; i++)
    {
        sum+=j;
        j+=2;
    }
    printf ("\nsum of odd natural number=%d",sum);
    system ("pause");
    return 0;
}

2 comments:

  1. your program is not working idiot

    ReplyDelete
    Replies
    1. The logic is basically the same in every C compiler as for me, I'm using DEV C++ compiler. I think you're using Turbo C++ so it's not working.

      Delete