#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
class Factorial
{
int i,fact,n;
public:
Factorial()
{
cout<<"\n Enter value of n : ";
cin>>n;
}
void for_fact()
{
fact=1;
for(i=1;i<=n;i++)
{
fact=fact * i;
}
cout<<"\n "<<n<<"! = "<<fact;
}
void do_fact()
{
fact=1;
i=1;
do
{
fact = fact * i;
i++;
}while(i<=n);
cout<<"\n "<<n<<"! = "<<fact;
}
void while_fact()
{
fact=1;
i=1;
while(i<=n)
{
fact=fact*i;
i++;
}
cout<<"\n "<<n<<"! = "<<fact;
}
};
int main()
{
clrscr();
int choice;
char ch;
Factorial obj1;
do
{
cout<<endl<<"1:Factorial using For Loop ";
cout<<endl<<"2:Factorial using Do..While Loop ";
cout<<endl<<"3:Factorial using While Loop ";
cout<<endl<<"4:Exit";
cout<<endl<<"\nEnter your choice=";
cin>>choice;
switch(choice)
{
case 1:
obj1.for_fact();
break;
case 2:
obj1.do_fact();
break;
case 3:
obj1.while_fact();
break;
case 4:
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