Power By Dholu Production

Responsive Ads Here

Monday, April 23, 2018

Write a program to print sum of series 1+3+5+...+N using function|C++

CODE:
#include<iostream>
using namespace std;

int display(int S,int N)
{
 int i;
 cout<<"Series ";
 for(i=1;i<=N;i=i+2)
   cout<<i<<"+";
 cout<<"=" ;
 cout<<S<<"\n" ;
 return 0;
}

int main()
{
 int i,n,sum=0,r;
 cout<<"\t Sum Of 'N' Odd Numbers ";
 cout<<"\n Enter the Value Of N : ";
 cin>>n ;
 a:
 cout<<"\n Press '1' for SUM using 'for loop' \n Press '2' for SUM using 'while loop' \n Press '3' For Display";
 cout<<"\n Your Choice :";
 cin>>r ;
 switch(r)
 {
  case 1 :
       for(i=1;i<=n;i=i+2)
       {
        sum=sum+i;
       }
       goto a;
       break;
  case 2 :
      i=1;
      while(i<=n)
      {
       sum=sum+i;
       i=i+2;
      }
      goto a;
      break;
  case 3 :
      display(sum,n);
      break;
  default :
      cout<<"You Entered Wrong choice";
  }

  return 0;
}