Monday, 30 September 2024

Journal Program 9

 #include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<process.h>

class OddEven

{

public:

int sum,n;

OddEven()

{

cout<<"\n Enter value of n : ";

cin>>n;

}

void oddSum()

{

int i,count=0,sum=0;

for(i=1;count<n;i=i+2)

{

count++;

cout<<i<<" + ";

sum=sum+i;

}

cout<<"\b\b\b";

cout<<" = "<<sum;

}

void evenSum()

{

int i,count=0,sum=0;

for(i=0;count<n;i=i+2)

{

count++;

cout<<i<<" + ";

sum=sum+i;

}

cout<<"\b\b\b";

cout<<" = "<<sum;

}

};

int main()

{

clrscr();

int choice;

char ch;

OddEven obj1;

do

{

cout<<endl<<"1:Sum of first "<<obj1.n<<" odd numbers ";

cout<<endl<<"2:Sum of first "<<obj1.n<<" even numbers ";

cout<<endl<<"3:Exit";


cout<<endl<<"\nEnter your choice=";

cin>>choice;

switch(choice)

{

case 1:

obj1.oddSum();

break;

case 2:

obj1.evenSum();

break;

case 3:

exit(0);

break;

default:

cout<<endl<<"Invalid choice";

}

cout<<"\nDo you want to cont...";

cin>>ch;

}while(ch=='y'||ch=='Y');

return 0;

}


No comments:

Post a Comment