The program simultaneously prints the source code and also shows its execution. As the user runs program some choices appear, the user selects particular choice and then its source code appear and then the program asks the user to input the required parameters, when the user inputs required data the desired results automatically appear. The program has the advantage that unlike all other programs in console format in c++, its interactivity outweighs all of them. The user has complete command over the program from the beginning till end. The program can easily be modified and there is no limit to the scope of this program. From the “HELLO WORLD” program to “INTERACTIVE GAMES” program it can do all that things with little modification in the program.Here is the sample code.
//-----------------------------------------------the start----------------------------------------------------------------
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int choice;
int decision;
int main()
{
do{
cout<<"1:Enter 1 to print --factorial"<<endl<<endl;
cout<<"2:Enter 2 to print --Multiplication table" <<endl<<endl;
cout<<"3:Enter 3 to print --Letter triangle" <<endl<<endl;
cout<<"4:Enter 4 to print --ODD or EVEN number" <<endl<<endl;
cout<<"5:Enter 5 to print --Power of a number" <<endl<<endl;
cout<<"6:Enter 6 to print --SWAPING of two numbers" <<endl<<endl;
cout<<"7:Enter 7 to print --ASCII value of a character" <<endl<<endl;
cout<<"8:Enter 8 to print --Printing True Random number" <<endl<<endl;
cout<<"9:Enter 9 to print --Print PYRAMID" <<endl<<endl;
cout<<"10:Enter 10 to print --Table making" <<endl<<endl;
cout<<"11:Enter 11 to print --SUM OF ARRAY" <<endl<<endl;
cin>>choice;
switch(choice){
case 1:
{cout<<"1: FACTORIAL of a number"<<endl;
cout<<"code:"<<endl;
cout<<"#include <iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<" cout<<int i,n,factorial=1;"<<endl;
cout<<"Enter positive number"<<endl;
cout<<"cin>>n;"<<endl;
cout<<"for(i=1;i<=n;++i){"<<endl;
cout<<"factorial*=i;"<<endl;
cout<<"}"<<endl;
cout<<"'Factorial of'"<<"<<'n'<<=<<"<<"factorial"<<endl;
cout<<"--------------OPERATION OF LOOP---------------"<<endl;
cout<<"CODE"<<endl;
int i, n,factorial=1;
cout<<"Enter number to get factorial"<<endl;
cin>>n;
for(i=1;i<=n;i++){
factorial*=i;}
cout<<"factorial of number"<<n<<"="<<factorial<<endl;
cout<<"----------------------------------------------------------------------------------------------------------------"<<endl;
break;}
case 2:
{cout<<"2:Print MULTIPLICATION TABLE"<<endl;
cout<<"code"<<endl;
cout<<"#include<iostream>"<<endl<<endl;
cout<<"using nameplace std:"<<endl<<endl;
cout<<"int main(){"<<endl<<endl;
cout<<"int i;no;table=1"<<endl<<endl;
cout<<"'Enter any number for which you want table'"<<endl<<endl;
cout<<"cin>>no;"<<endl;
cout<<"cout<<'table of'<<no;"<<endl<<endl;
cout<<"for(i=1;i<=10;i++){"<<endl<<endl;
cout<<"table=no*i;"<<endl<<endl;
cout<<"'\n'<<table;"<<endl;
cout<<"cout<<endl;"<<endl;
cout<<"}}"<<endl<<endl;
cout<<"----------------------OPERATION OF------------------"<<endl;
cout<<"CODE"<<endl;
int i,no,table=1;
cout<<"Enter any number"<<endl<<endl;
cin>>no;
cout<<"table of"<<no;
for(i=1;i<=10;i++){
table=no*i;
cout<<"\n"<<i<<"*"<<no<<"="<<table;
cout<<endl;
}cout<<"8:Print TRUE Random number"<<endl;
cout<<"-------------------------------------------------------------------------------------------------"<<endl;}
break;
case 3:
cout<<"3:Print ALPHABET TRIANGLE"<<endl;
cout<<"#include <iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"int i,j;"<<endl;
cout<<"char input,temp='A';"<<endl;
cout<<"cout<<'Enter uppercase letter that you want in last row of triangle';"<<endl;
cout<<"cin>>input;"<<endl;
cout<<"for(i=1;i<=(input-'A'+1);++i)"<<endl;
cout<<"{for(j=1;j<=1;++j)"<<endl;
cout<<"{cout<<temp<<" ";"<<endl;
cout<<"++temp;"<<endl;
cout<<"cout<<endl;}}"<<endl;
cout<<"}"<<endl;
cout<<"---------------------------------------OPERATION --------------------------"<<endl;
cout<<"CODE"<<endl;
{
int i,j;
char input,temp='A';
cout<<"Enter uppercase character you want in triange at last row: ";
cin>>input;
for(i=1;i<=(input-'A'+1);++i)
{
for(j=1;j<=i;++j)
cout<<temp<<" ";
++temp;
cout<<endl;
}
break;
}
cout<<"--------------------------------------------------------------------------------------------------------------------------"<<endl;
break;
case 4:
cout<<"4:Print ODD OR EVEN NUMBER"<<endl;
cout<<"#include<iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"int n"<<endl;
cout<<"'Enter an integer';"<<endl;
cout<<"cin>>n;"<<endl;
cout<<"if(n%2==0){"<<endl;
cout<<"cout<<n<<'is even';}"<<endl;
cout<<"else {"<<endl;
cout<<"cout<<'n<<'is odd';"<<endl;
cout<<"}"<<endl;
cout<<"-----------------------------------OPERATION-----------------------------------------------"<<endl;
cout<<"CODE"<<endl;
{
int n;
cout<<"Enter an integer:";
cin>>n;
if(n%2==0){
cout<<n<<"is even.";
}
else{
cout<<n<<"is odd";
}
cout<<"--------------------------------------------------------------------------------------------------------------------------"<<endl;
break;
}
case 5:
cout<<"5:Print POWER OF A NUMBER"<<endl;
cout<<"include<iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int i,p,b,answer,r=1;";
cout<<"in main(){"<<endl;
cout<<"cout<<'Enter base and power'<<endl;"<<endl;
cout<<"cout<<'base=';";
cout<<"cin>>b";
cout<<"cout<<'power=';";
cout<<"cin>>p";
cout<<"for(i=1;i<=p;i++){"<<endl;
cout<<"r*=b;}"<<endl;
cout<<"answer=r"<<endl;
cout<<"cout<<'answer='<<answer<<endl;}";
cout<<"---------------------------------OPERATION-------------------------------------------"<<endl;
cout<<"CODE"<<endl;
{
int i,p,b,answer,r=1;
cout<<"Enter base and power"<<endl;
cout<<"base=";
cin>>b;
cout<<endl;
cout<<"power=";
cin>>p;
cout<<endl;
for(i=1;i<=p;i++){
r*=b;
}
answer=r;
cout<<"answer="<<answer<<endl;
cout<<"----------------------------------------------------------------------------------------------------------------------------"<<endl;
break;
}
case 6:
cout<<"6:Print SWAPPING OF TWO NUMBERS"<<endl;
cout<<"#include<iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(0{"<<endl;
cout<<"int a,b,c;"<<endl;
cout<<"cout<<'Enter number a';"<<endl;
cout<<" cin>>a;";
cout<<"cout<<'Enter number b';"<<endl;
cout<<" cin>>b;";
cout<<"c=a;";
cout<<"a=b;";
cout<<"b=c;";
cout<<"cout<<'After swap a='<<a<<'AND b='<<b<<endl;"<<endl;
cout<<"}";
cout<<"--------------------------------OPERATION-----------------------------"<<endl;
cout<<"CODE"<<endl;
{
int a,b,c;
cout<<"Enter number a="<<endl;
cin>>a;
cout<<endl;
cout<<"Enter number b="<<endl;
cin>>b;
c=a;
a=b;
b=c;
cout<<"After SWAP a="<<a<<"AND b="<<b<<endl;
cout<<"-------------------------------------------------------------------------------------------------------------"<<endl;
break;
}
case 7:
cout<<"7:Print ASCII VALUE OF CHARACTER ENTERED BY USER"<<endl;
{cout<<"#include <iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"char ascii;"<<endl;
cout<<"cout<<'Enter a Character:'"<<endl;
cout<<"cout<<'its ascii value is:'<<(int)ascii<<endl;"<<endl;
}
cout<<"---------------------OPERATION---------------------------------------"<<endl;
cout<<"CODE"<<endl;
{char ascii;
cout<<"Enter a character "<<endl;
cin>>ascii;
cout<<"its ascii value is:"<<(int)ascii<<endl;
cout<<"-----------------------------------------------------------------------------------------------------------------"<<endl;
break;
}
{case 8:
cout<<"8:Print TRUE Random number"<<endl;
cout<<"#include<iostream>"<<endl;
cout<<"#include<cstdlib>"<<endl;
cout<<"#include<ctime>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"srand(time(0));"<<endl;
cout<<"for(int x=1;x<=25;x++){"<<endl;
cout<<"cout<<rand()%9;}}"<<endl;
cout<<"---------------------------------OPERATION-----------------------------------"<<endl;
cout<<"CODE"<<endl;
srand(time(0));
for(int x=1;x<25;x++){
cout<<x<<"="<<(rand()%9)<<endl;
}
cout<<"----------------------------------------------------------------------------------------------------------------"<<endl;
break;
}
case 9:
cout<<"8:Print PYRAMID"<<endl;
cout<<"#include<iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"int rows,stars,trows,space;"<<endl;
cout<<"for(rows=1;rows<=trows;++rows){"<<endl;
cout<<"for(space=1;space<+trows-rows;++space){"<<endl;
cout<<"cout<<' ';}"<<endl;
cout<<"for(stars=1;stars<=rows;++stars){"<<endl;
cout<<" *}}}"<<endl;
cout<<"------------------------OPERATION--------------------------------"<<endl;
cout<<"Code"<<endl;
{
int rows,trows,space,stars;
cout<<"Enter no of rows to print"<<endl;
cin>>trows;
for(rows=1;rows<=trows;++rows){
for(space=1;space<=trows-rows;++space){
cout<<" ";
}
for(stars=1;stars<=rows;++stars){
cout<<" *";
}
cout<<endl;
}
cout<<"----------------------------------------------------------------------------------------------------------------"<<endl;
}
break;
case 10:
{cout<<"PRINTING TABLE"<<endl;
cout<<"#include<iostream>"<<endl;
cout<<"using namespace std;<<"<<endl;
cout<<"int main(){"<<endl;
cout<<"int bucky[5][5]={{1,2,3,4,5},{2,3,4,5,6},{1,4,3,2,4},{1,3,4,2,3},{2,4,3,2,2}};"<<endl;
cout<<"for(int rows=0;rows<5;rows++){"<<endl;
cout<<"cout<<bucky[rows][column];"<<endl;
cout<<"cout<<" ";}"<<endl;
cout<<"cout<<endl<<endl;"<<endl;
cout<<"-------------------------OPERATION----------------------------"<<endl;
cout<<"CODE"<<endl;
int bucky[5][5]={{1,2,3,4,5},{2,3,4,5,6},{1,4,3,2,4},{1,3,4,2,3},{2,4,3,2,2}};
for(int rows=0;rows<5;rows++){
for(int column=0;column<5;column++){
cout<<bucky[rows][column];
cout<<" ";
}
cout<<endl<<endl;
}
cout<<"----------------------------------------------------------------------------------------------------------------"<<endl;
}
case 11:
cout<<"#include <iostream>"<<endl;
cout<<"using namespace std;"<<endl;
cout<<"int main(){"<<endl;
cout<<"int Arr[],n,i,sum=0;"<<endl;
cout<<"cout<<'Enternumber of elements you want to print"<<endl;
cout<<"cin>>n"<<endl;
cout<<"for(i=0;i<n;i++){"<<endl;
cout<<"cout<<'Enter elements for which you want sum;'"<<endl;
cout<<"cin>>Arr[i]"<<endl;
cout<<"sum+=Arr[i];"<<endl;
cout<<"}"<<endl;
cout<<"sum="<<"sum"<<endl;
cout<<"}"<<endl;
cout<<"------------------------------OPERATION----------------------------------"<<endl;
cout<<"CODE"<<endl;
{
int Arr[100],n,i,sum=0;
cout<<"enter number of elements you want to print"<<endl;
cin>>n;
for(i=0;i<n;i++){
cout<<"Enter "<<i<<" element for which you want sum"<<endl;
cin>>Arr[i];
sum+=Arr[i];
}
cout<<"The sum of"<<n<<"elements is="<<sum<<endl;
}
}
cout<<"ENTER O TO EXIT FROM PROGRAM"<<endl;
cin>>decision;
}
while(decision!=0);
}
//--------------------------------------------the end-------------------------------------------------------------
This is how our starting program would look like, based on your choice program will first print source code of you program and then will take parameters from you for which you want to run your program, instead of these choices we could also have added some games program instead and it would have run that game too.
1 comments:
Write commentsAlthough this program may look difficult but it is extremely easy to understand and this program could also be written in 75 lines of code using files , I will upload them after one week uptil then try to figure out how this could be done with files.
ReplyEmoticonEmoticon