Find maximum and minimum number in array
c++ code
#include
#include
#includeint main() { const int size=50; array with size of 50 int array[size]; for random numbers srand(time(0)); for(int i=0;i
{
initializing array number is less than 100
array[i]=rand()%100;
displaying array value
cout<
}
initializing max, min
int max=array[0];
int min=array[0];
scanning array to find
minimum and maximum
number
for(int i=0;i
initializing array number is less than 100
array[i]=rand()%100;
displaying array value
cout<
}
initializing max, min
int max=array[0];
int min=array[0];
scanning array to find
minimum and maximum
number
for(int i=0;i
{
finding minimum number in array
if(min>array[i])
finding minimum number in array
if(min>array[i])
{
min=array[i];
}
finding maximum number in array
if(max
min=array[i];
}
finding maximum number in array
if(max
{
max=array[i];
}
}
displaying output
cout<<"maximum number is :"<
cout<<"minimum number is:"<
max=array[i];
}
}
displaying output
cout<<"maximum number is :"<
cout<<"minimum number is:"<
return 0;
}
}
Bubble sort in c++ code example
#include
#includeint main()
{
eclaring array
int array[5];
cout<<"enter 5 numbers randomly : "<
for(int i=0; i<5 i="" nbsp="" span="">5>
{
taking input
in array
cin>>array[i];
}
cout<
cout<<"input array is: "<
for(int j=0; j<5 j="" nbsp="" span="">5>
{
displaying array
cout<<"\t\t\tvalue at "<
}
cout<
bubble sort starts here
int temp;
for(int i2=0; i2<=4; i2++)
{
for(int j=0; j<4 j="" nbsp="" span="">4>
{
swapping
element in if statement
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
displaying sorted array
cout<<" sorted array is: "<
for(int i3=0; i3<5 i3="" nbsp="" span="">5>
{
cout<<"\t\t\tvalue at "<
}
return 0;
}
C++ program to find factorial using recursion
#include
#includeint recursive_function(int n)
{
static int i=1;
to make one time initialization
cout<
counting the function calls
if(n==1)
{
return 1;
base case
}
else
{
i++;
return n=n*recursive_function(n-1);
}
}
int main()
{
cout<<"enter to number to find factorial: ";
int num;
cin>>num;
num=recursive_function(num);
function call
cout<<"\n\n\n\t\t\tfactorial of number is: "<
cout<<"\n\n\n\n\n";
return 0;
}
No comments:
Post a Comment