Finding largest two numbers from N numbers:
This C program example finds the two largest numbers from the list of 'N' numbers.
Mechanism:
In this program, we have declared the variable of:
- Size
- Input
- First largest
- Second largest
Code:
/* Join us athttp://shapesinc.blogspot.comfor more codes of shapes*/#include<iostream>using namespace std;int main(){cout << "Join http://shapesinc.blogspot.com for more codes of shapes\n\n";cout<<"CAUTION: The input should not be other than numerical values \n\n\a";int i, num, largest = 0, seclargest = 0, size; cout << "Enter the amount of numbers you want to enter\n"; cin>>size; if (size<=0) { cout << "No values entered\n"; goto finish; } cout << "Please enter the numbers from the keyboard.\n" << endl; for (int i=0; i<size; i++ ) { cin >> num; if(i==0) largest=num; else if(i==1) { if(num>largest) { seclargest = largest; largest = num; } else seclargest=num; } else if ( num > largest ) { seclargest = largest; largest = num; } else if ( num > seclargest ) seclargest = num; } cout << "\nThe largest of the numbers entered is " << largest << endl ; cout << "\nThe second largest of the numbers entered is " << seclargest << endl ; finish: return 0; }
Final Words:
By this post, you can learn C online practically. We have tried our best to make this program understandable and optimized.