Adsense 728 * 15

Search

Custom Search

C , C++ Program to Find Largest Number in an Unsorted Array

Write a c , c++ program to find largest number in an unsorted array.

To solve this problem, i used very simple logic.

 /* Assign array first element to highest variable. */

   int highest = arr[0];

  /* Iterate till the end of an array. */
	
   for(i=1;i<arrcount;i++){
	
        /* Check if array element is higher
           than value in highest.*/
      	
	if(arr[i]>highest){
			
	         highest = arr[i];
	    }
	}


C,C++ Interview Questions

C++ Program to Find Largest Number in an Unsorted Array


#include <iostream.h>   

using namespace std;

int main() {
	
	int arr[] = {2,1,4,6,7,9,5};
	
	int i;
	
        /* Array count. */

	int arrcount = sizeof(arr)/sizeof(arr[0]);
	
	/* Initialize highest with first element of an array. */
	
	int highest = arr[0];

        for(i=1;i<arrcount;i++){
		
		if(arr[i]>highest){
			
			highest = arr[i];
		}
	}
	
	
	cout<<"Highest number is"<<highest;
        
        return 0;  

   }


C Program to Find Largest Number in an Unsorted Array

#include <stdio.h>
 
int main() {
	
	int arr[] = {2,1,4,6,7,9,5};
	
	int i;
	
	int arrcount = sizeof(arr)/sizeof(arr[0]);
	
	/* Initialize with first element of an array. */
	
	int highest = arr[0];
	
	for(i=1;i<arrcount;i++){
		
		if(arr[i]>highest){
			
			highest = arr[i];
		}
	}
	
	printf("Highest number is %d",highest);
	
	return 0;
}

Output: Highest number is : 9

No comments:

Post a Comment