Practice Program
Solution:
void main()
{
int a[20],n,i;
int max = 0;
printf("Enter the number: ");
scanf("%d",&n);
//taking array elements as input.
for(i=0;i<n;i++)
{
printf("\nEnter the Element: ");
scanf("%d",&a[i]);
}
//printing the array elements
printf("Array Elements are: \n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
//Determining the maximum Element.
for(i=0;i<i++)
{
if(a[i] > max){//Comparing each element with max variable.
//if element at j index is greater the max value than,
max = a[i]; //we are assigning value at j index as new ,
//value of variable max and going for the next iteration.
}
}
printf("\nMaximum Element is %d",max);
}
void main() { int a[20],n,i; int max = 0; printf("Enter the number: "); scanf("%d",&n); //taking array elements as input. for(i=0;i<n;i++) { printf("\nEnter the Element: "); scanf("%d",&a[i]); } //printing the array elements printf("Array Elements are: \n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); } //Determining the maximum Element. for(i=0;i<i++) { if(a[i] > max){//Comparing each element with max variable. //if element at j index is greater the max value than, max = a[i]; //we are assigning value at j index as new , //value of variable max and going for the next iteration. } } printf("\nMaximum Element is %d",max); }
Output1
Enter the number of elements: 5
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Array Elements are:
1 2 3 4 5
Maximum Element is 5
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Array Elements are:
1 2 3 4 5
Maximum Element is 5
Output2
Enter the number of elements: 10
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Array Elements are:
1 2 3 4 5 6 7 8 9 10
Maximum Element is 10.
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Array Elements are:
1 2 3 4 5 6 7 8 9 10
Maximum Element is 10.
Output3
Enter the number of elements: 15
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element:11
Enter the Element:12
Enter the Element: 13
Enter the Element: 14
Enter the Element:15
Array Elements are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Maximum Element is 15.
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element:6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element:11
Enter the Element:12
Enter the Element: 13
Enter the Element: 14
Enter the Element:15
Array Elements are:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Maximum Element is 15.
defining a function 'give_max()' which will return maximum element.
int give_max(int a[],int n)
{
int i,max = 0;
for(i=0;i<i++)
{
if(a[i] > max){//Comparing each element with max variable.
//if element at j index is greater the max value than,
max = a[i]; //we are assigning value at j index as new ,
//value of variable max and going for the next iteration.
}
}
return max;
}
int give_max(int a[],int n) { int i,max = 0; for(i=0;i<i++) { if(a[i] > max){//Comparing each element with max variable. //if element at j index is greater the max value than, max = a[i]; //we are assigning value at j index as new , //value of variable max and going for the next iteration. } } return max; }
Using above define function 'give_max()':
void main()
{ int result;
int a[10] = {1,2,3,4,5,6,7,8,9,10};
result = give_max(a,10);
printf("Maximum Element is %d",result);
}
void main() { int result; int a[10] = {1,2,3,4,5,6,7,8,9,10}; result = give_max(a,10); printf("Maximum Element is %d",result); }
Output1
Maximum Element is 10.
Using above define function 'give_max()':
void main()
{ int result;
int a[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
result = give_max(a,15);
printf("Maximum Element is %d",result);
}
void main() { int result; int a[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; result = give_max(a,15); printf("Maximum Element is %d",result); }
Output2
Maximum Element is 15.
Using above define function 'give_max()':
void main()
{ int result;
int a[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
result = give_max(a,20);
printf("Maximum Element is %d",result);
}
void main() { int result; int a[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; result = give_max(a,20); printf("Maximum Element is %d",result); }
Output3
Maximum Element is 20.
0 Comments
Post a Comment