//find wheather a number is in the form of 3pow(n)
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
void recur(int);
int i=1,n;
void main()
{ clrscr();
// extern int n;
cout<<"enter the number: ";
cin>>n;
recur(3);
}
void recur(int x)
{ if(x==n)
{ cout<<"number is multiple";
getch();
exit(0);
}
if(x>n)
{ cout<<"number is not multiple";
getch();
exit(0);
}
else
{ recur(pow(3,++i));
}
}
- Upinder Singh Dhami
- Bangalore, Karnataka, India
- Extending one hand to help someone has more value rather than joining two hands for prayer
write a program to check whether the given number was of the form 3^n. ( i.e. 3 to the power n).
Wednesday, February 3, 2010Posted by Upinder Singh Dhami at Wednesday, February 03, 2010 2 comments
There are two sorted arrays, a1 and a2 of size n1 and size n2 respectively. array a1 is full, array a2 has exactly n1(size of array a1) empty space. Example, a1[]=1234, a2[]=56789_ _ _ _ Write a function to merge these two arrays to form a sorted array without any extra memory use.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,m,i,j,temp;
cout<<"enter the size of first array: ";
cin>>n;
cout<<"enter the size of second array: ";
cin>>m;
int *p=new int[n];
int *q=new int[n+m];
cout<<"enter the sorted elements of first array: ";
for(i=0;i<n;i++)
cin>>p[i];
cout<<"enter the sorted elements of second array: ";
for(i=0;i<m;i++)
cin>>q[i];
i=0;j=0;
while(1)
{ while(1)
{ if(p[i]<q[j])
{ temp=m;
for(temp=m;temp>=j;temp--)
q[temp]=q[temp-1];
m++;
break;
}
j++;
if(j==m)
{ temp=n;
for(i=0;i<temp;i++)
{ q[m]=p[i];
m++;
}
cout<<"after sorting elements are: ";
for(i=0;i<m;i++)
cout<<q[i]<<",";
getch();
return;
}
}
i++;
n--;
if(n==-1)
{ for(i=0;i<m+n;i++)
cout<<q[i];
break;
}
}
getch();
Posted by Upinder Singh Dhami at Wednesday, February 03, 2010 0 comments
suppose u r given a 4*3 rectangle like (take these values from user) Now u have to calculate the no. of squares in this rectangle like: No. of squares of dimension 1 is 12 No. of squares of dimension 2 is 6 No. of squares of dimension 3 is 2 No. of squares of dimension 4 is 0 Total no. of squares are 20.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,m,temp;
cout<<"enter the length and breadth of the rectangle: ";
cin>>n>>m;
int k=n<m?n:m;
for(int i=1;i<=k;i++)
{ cout<<"number of cubes when dimension is"<<i<<" :";
temp=(n-i+1)*(m-i+1);
if(temp<0)
cout<<"0";
else if(temp==0)
{ temp=(n>m?n:m)-k+1;
cout<<temp;
}
else
{ cout<<temp;
}
cout<<endl;
}
getch();
}
Posted by Upinder Singh Dhami at Wednesday, February 03, 2010 0 comments