C++ program to find largest number of array.
Method 1 : C++ program to find largest number of array
#include<iostream>
using namespace std;
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
{
cin>>num[i];
if(i==0)
maxi=num[0];
if(maxi<num[i])
maxi=num[i];
}
cout<<"Largest number is "<<maxi;
return 0;
}
using namespace std;
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
{
cin>>num[i];
if(i==0)
maxi=num[0];
if(maxi<num[i])
maxi=num[i];
}
cout<<"Largest number is "<<maxi;
return 0;
}
Method 2 : C++ program to find largest number of array using recursion
#include<iostream>
using namespace std;
int max(int *num,int n,int i,int maxi)
{
if(i==n)
return maxi;
if(maxi<num[i])
maxi=num[i];
max(num,n,i+1,maxi);
}
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=max(num,n,0,num[0]);
cout<<"Largest number is "<<maxi;
return 0;
}
using namespace std;
int max(int *num,int n,int i,int maxi)
{
if(i==n)
return maxi;
if(maxi<num[i])
maxi=num[i];
max(num,n,i+1,maxi);
}
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=max(num,n,0,num[0]);
cout<<"Largest number is "<<maxi;
return 0;
}
Method 3 : C++ program to find largest number of array using conditional operators
#include<iostream>
using namespace std;
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=num[0];
for(int i=1;i<n;i++)
maxi=maxi<num[i]?num[i]:maxi;
cout<<"Largest number is "<<maxi;
return 0;
}
using namespace std;
int main()
{
int n,num[20],maxi=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=num[0];
for(int i=1;i<n;i++)
maxi=maxi<num[i]?num[i]:maxi;
cout<<"Largest number is "<<maxi;
return 0;
}
Method 4 : C++ program to find largest number of array using pointers
#include<iostream>
using namespace std;
int main()
{
int *p,n,max;
cout<<"How many elements to be entered : ";
cin>>n;
p=new int[n];
cout<<"Enter elements : ";
for(int i=0;i<n;i++)
cin>>p[i];
max=p[0];
for(int i=1;i<n;i++)
{
if(max<p[i])
max=p[i];
}
cout<<"Largest element : "<<max<<endl;
return 0;
}
using namespace std;
int main()
{
int *p,n,max;
cout<<"How many elements to be entered : ";
cin>>n;
p=new int[n];
cout<<"Enter elements : ";
for(int i=0;i<n;i++)
cin>>p[i];
max=p[0];
for(int i=1;i<n;i++)
{
if(max<p[i])
max=p[i];
}
cout<<"Largest element : "<<max<<endl;
return 0;
}
C++ program to find 2nd largest number of array
#include<iostream>
using namespace std;
int main()
{
int n,num[20],maxi=0,y=0,min=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=min=num[0];
for(int i=1;i<n;i++)
{
if(maxi<num[i])
{
maxi=num[i];
y=i;
}
if(min>num[i])
min=num[i];
}
num[y]=maxi=min;
for(int i=0;i<n;i++)
if(maxi<num[i])
maxi=num[i];
cout<<"2nd Largest number is "<<maxi;
return 0;
}
using namespace std;
int main()
{
int n,num[20],maxi=0,y=0,min=0;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
maxi=min=num[0];
for(int i=1;i<n;i++)
{
if(maxi<num[i])
{
maxi=num[i];
y=i;
}
if(min>num[i])
min=num[i];
}
num[y]=maxi=min;
for(int i=0;i<n;i++)
if(maxi<num[i])
maxi=num[i];
cout<<"2nd Largest number is "<<maxi;
return 0;
}
C++ program to find kth largest number of array
#include<iostream>
using namespace std;
int main()
{
int n,num[20],maxi=0,y=0,min=0,k;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
cout<<"Enter the value of k ";
cin>>k;
maxi=min=num[0];
for(int i=0;i<n;i++)
if(min>num[i])
min=num[i];
for(int z=1;z<k;z++)
{
for(int i=0;i<n;i++)
{
if(maxi<num[i])
{
maxi=num[i];
y=i;
}
}
num[y]=maxi=min;
}
for(int i=0;i<n;i++)
if(maxi<num[i])
maxi=num[i];
cout<<"kth Largest number is "<<maxi;
return 0;
}
using namespace std;
int main()
{
int n,num[20],maxi=0,y=0,min=0,k;
cout<<"How many numbers to be entered ";
cin>>n;
cout<<"Enter numbers ";
for(int i=0;i<n;i++)
cin>>num[i];
cout<<"Enter the value of k ";
cin>>k;
maxi=min=num[0];
for(int i=0;i<n;i++)
if(min>num[i])
min=num[i];
for(int z=1;z<k;z++)
{
for(int i=0;i<n;i++)
{
if(maxi<num[i])
{
maxi=num[i];
y=i;
}
}
num[y]=maxi=min;
}
for(int i=0;i<n;i++)
if(maxi<num[i])
maxi=num[i];
cout<<"kth Largest number is "<<maxi;
return 0;
}
No comments:
Post a Comment