Friday, 21 October 2016

11C-prg-14. Write a program to calculate and print the sum of odd integers of the first n natural numbers.

14.   Write a program to calculate and print the sum of odd integers of the
first n natural numbers.

Input:
1) n
Output:
1) sum of odd
Methd:
1.start
2.Read n
3.Prit n
4.//logic for sum
   sum_o=0;
  for(i=0;i<n;i++)
   {
    sum_o = sum_o + i;
    i=i+1;
   }
5. print sum_o
6.stop

#include<iostream.h>

void main()
{
   int n,i,sum_o;

  cout<<"Enter n value";
  cin>>n;
  cout<<"\n n="<<n;

  //logic for sum
   sum_o=0;
  for(i=1;i<n;i++)
   {
    sum_e = sum_e + i;
    i=i+1;
   }
  cout<<"\n sum of first n natural odd numbers ="<<sum_o;
}

No comments:

Post a Comment