C++ Program – Find the 2 largest number from the given set of numbers

by Nideesh C on May 9, 2011 · 0 comments

in C++ projects




//Downloaded From theonlinetutorials.com
#include<iostream.h>
#include<conio.h>
int main()
 {
 int a[1000],n,max,max1;
 cout<<"How many no you will input"<<endl;
 cin>>n;
 for(int i=0;i<n;i++)
 {
 cin>>a[i];
 }
 //Escape When two input are not avaliable
 if(n<=1)
 {
 cout<<"Maximu two No are"<<endl;
 for(int i=0;i<n;i++)
 {
 cout<<a[n]<<endl;
 }
 return 0;
 }
 //store maximu value between a[0] and a[1]
 max=a[0]>a[1]?a[0]:a[1];
 max1=a[0]<a[1]?a[0]:a[1];
 for(i=2;i<n;i++)
 {
 if(a[i]>max1)
 {
 max1=a[i];
 if(max<max1)
 {
 int temp=max;
 max=max1;
 max1=temp;
 }
 }
 }
 cout<<"Maximum of two No are"<<endl<<max<<endl<<max1;
 getch();
 return 0;
 }



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to read in four integer numbers into an array and find the average of largest two of the given numbers without sorting the array. The program should output the given four numbers and the average with suitable headings.
  2. C program to sort given N elements using SELECTION sort method using functions a) To find maximum of elements b) To swap two elements
  3. C program to read N integers (zero, +ve and -ve) into an array A and to a) Find the sum of negative numbers b) Find the sum of positive numbers and c) Find the average of all input numbers Output the various results computed with proper headings
  4. C++ class program – perform rational number arithmetic
  5. C++ program – multiply two numbers without using multiplication operator

Leave a Comment

Previous post:

Next post: