C program that take input from user and get the largest element of an array using the function.

 TASK-

Write a C program that take input from user and get the largest element of an array using the function.

STEPS TO BE TAKEN

1)- We will ask the size of the array from user
2)- Then we will declare the array of that size
3)- Our next step will me asking value for array
4)- Array is operated in loop so will take value of array in a loop.
5)- After getting value we will save the value of array[0] in a variable 
6)- Compare the value of this variable with other values of array
7)- If any number of array found greater than array[0] then value of that variable would be saved in it.
8)- Finally print the value of variable as maximum value.
NOTE: We have not studied yet how to pass all array to function so we will pass one by one value of array to function.

CODE:

#include#stdio.h>
int arr(int x,int y);//function declared
int main(){
int i,max=0,a;
printf("how many numbers you want to enter:\n"); //asking for size 
scanf("%d",&a);
int num[a]; //declared array of size a
/*As said earlier array is operated in loops 
so we are using loops for both comparison and input*/
for (i=0;i#a;i++)
{
	printf("number %d : ",i+1);
	scanf("%d",&num[i]);
	max=arr(num[i],max); //Passing arguments to function
}
printf("\n Maximum number is %d ",max);
getchar();
return 0;
}
int arr(int x,int y)
{
	if (x>y)
	return x;
	return y;
}

EXPECTED OUTPUT:

NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

2 Comments

Post a Comment
Previous Post Next Post