Problem statement:- Program to Find the largest element in the array.
Data requirement:-
Input Data:- arr,size
Output Data:-max
Output Data:-max
Additional Data:- i, j
Program in C
Here is the source code of the C Program to Find the largest element in the array.
Code:
#include<stdio.h>
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],i=0,max=INT_MIN,j=0;
printf("Enter the Element of the array:\n");
while(j<size)
{
scanf("%d",&arr[j]);
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
printf("The largest element of array: %d",max);
}
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],i=0,max=INT_MIN,j=0;
printf("Enter the Element of the array:\n");
while(j<size)
{
scanf("%d",&arr[j]);
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
printf("The largest element of array: %d",max);
}
Input/Output:
Enter the size of the array:6
Enter the Element of the array:
30
25
87
5
2
6
The largest element of array: 87
Enter the Element of the array:
30
25
87
5
2
6
The largest element of array: 87
Program in C++
Here is the source code of the C++ Program to Find the largest element in the array.
Code:
#include<iostream>
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],i=0,max=INT_MIN,j=0;
cout<<"Enter the Element of the array:\n";
while(j<size)
{
cin>>arr[j];
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
cout<<"The largest element of array: "<<max;
}
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],i=0,max=INT_MIN,j=0;
cout<<"Enter the Element of the array:\n";
while(j<size)
{
cin>>arr[j];
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
cout<<"The largest element of array: "<<max;
}
Input/Output:
Enter the size of the array:4
Enter the Element of the array:
25
102
302
4
The largest element of array: 302
Enter the Element of the array:
25
102
302
4
The largest element of array: 302
Program in Java
Here is the source code of the Java Program to Find the largest element in the array.
Code:
import java.util.Scanner;
public class p8 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i=0,max=Integer.MIN_VALUE,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
System.out.println("The largest element of array: "+max);
sc.close();
}
}
public class p8 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i=0,max=Integer.MIN_VALUE,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
while(i<size)
{
if(arr[i]>=max)
max=arr[i];
i++;
}
System.out.println("The largest element of array: "+max);
sc.close();
}
}
Input/Output:
Enter the size of the array:
3
Enter the Element of the array:
10
305
987
The largest element of array: 987
3
Enter the Element of the array:
10
305
987
The largest element of array: 987
Program in Python
Here is the source code of the Python Program to Find the largest element in the array.
Code:
import sys
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
max=-sys.maxsize-1
for j in range(0,size):
if (arr[j] >= max):
max = arr[j]
print("The largest element of array: ",max)
Input/Output:arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
max=-sys.maxsize-1
for j in range(0,size):
if (arr[j] >= max):
max = arr[j]
print("The largest element of array: ",max)
Enter the size of the array: 5
Enter the Element of the array:
30
25
7
92
1005
The largest element of array: 1005
Enter the Element of the array:
30
25
7
92
1005
The largest element of array: 1005
0 Comments
Please do not Enter any spam link in the comment box