WAP to find the largest and smallest among n numbers


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

using namespace std;

int main()
{
int n;
cout<<"How many numbers : ";cin>>n;

int *p=new int(n);

cout<<"\nEnter the "<<n<<" numbers : \n";

for(int i=1;i<=n;i++)
{
cin>>*(p+i);
}

int smallest = *p, largest= *p;

for(int i=1;i<=n; i++)
{
if((smallest>*(p+i)))
{
smallest=*(p+i);
   
}
if(largest<*(p+i))
   largest=*(p+i);


}
cout<<"\nLargest number = "<<largest;
cout<<"\nSmallest number = "<<smallest;



getch();
return(0);
}

No comments:

Post a Comment