Wednesday, February 23, 2011

C++ Programs (Data Structures)

Best Blogger Tips

/*Q1.WAP to perform following operations on integer entered as Command Line
  Argument:
i)   To check if a number is prime or not
ii)  To print the sum and products of the number's digits
iii) To reverse the number
*/


#include <iostream.h>
#include <conio.h>
#include <math.h>


class operation     
{
int r,i,p,q,z;
public:
void prime(int);
void sum_and_pro(int);
void rev(int);
};


void operation::prime(int x)     
{

r=-1;
for(int i=2;i<=(x/2);i++)     
{
if(x%i==0)     
{
r=0;
break;
}
}
if(r<0)     
{
cout<<"\nThe entered number is a prime number.\n";
}
else
cout<<"\nThe entered number is not a prime.\n";
}


void operation::sum_and_pro(int x)     
{
z=0;
p=1;
q=x;
while(x!=0)     
{
y=x%10;
z+=y;
x/=10;
p*=y;
}
cout<<"\nSum of the entered number is "<<z<<"\n";
cout<<"\nProduct of the entered number is "<<p<<".\n";
}


void operation::rev(int q)     
{
cout<<"\nReverse of the entered number is ";
while(q!=0)     
{
y=q%10;
q=q/10;
cout<<y;
}
}


int main(int args,char *argv)    
{
clrscr();
int j,num;
char ch;
num=atoi(argv[1]);
operation op;
do     
{
cout<<"\n--------MENU--------\n"<<"\n"<<"1. Prime\n";
cout<<"2. Sum & Product\n"<<"3. Reverse\n";
cout<<"\nEnter your choice:";
cin>>j;
switch(j)     
{
case 1:
op.prime(num);
break;
case 2:
op.sum_and_pro(num);
break;
case 3:
op.rev(num);
cout<<".\n";
break;
default:
cout<<"\n\aInvalid Command\n";
break;
}
cout<<"\nDo you want to continue(y/n)?";
cin>>ch;
}while(ch=='y'||ch=='Y');
return 0;
}


/*----------OUTPUT-----------
--------MENU---------


1. Prime
2. Sum & Product
3. Reverse


Enter your choice:1


Enter a number:13


The entered number is a prime number.


Do you want to continue(y/n)?y


--------MENU---------


1. Prime
2. Sum & Product
3. Reverse


Enter your choice:2
Sum of the entered number is 4


Product of the entered number is 3.


Do you want to continue(y/n)?y


--------MENU---------


1. Prime
2. Sum & Product
3. Reverse


Enter your choice:3


Reverse of the entered number is 31.


Do you want to continue(y/n)?n
*/

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
x

Get Our Latest Posts Via Email - It's Free

Enter your email address:

Delivered by FeedBurner