Tuesday, October 06, 2015

Write a program to find the sum of first ‘n’ odd natural numbers, where ‘n’ is entered by the user.

Solution:-
     #include<conio.h>
     #include<stdio.h>
     void main ( )
     {
              int i,n,sum,odd;
              clrscr( );
              scanf("%d",&n);
              sum = 0;
              odd = 1;
              for(i=1;i<=n;i++)
              {
                      sum=sum+odd;
                      odd=odd+2;
              }
              printf("%d\t",sum);
              getch( );
     }