YouTip LogoYouTip

C Examples Dynamic Memory Allocation Largest

# C Example - Find the Largest Number [![Image 3: C Examples](#) C Examples](#) Determine the largest value by taking user-specified numbers as input. ## Example - Find the Largest Number #include#includeint main(){int i, num; float *data; printf("Enter the number of elements (1 ~ 100): "); scanf("%d", &num); // Allocate memory for 'num' elements data = (float*)calloc(num, sizeof(float)); if(data == NULL){printf("Error!!! Memory not allocated."); exit(0); }printf("n"); // User input for(i = 0; i<num; ++i){printf("Enter number %d: ", i + 1); scanf("%f", data + i); }// Loop to find the largest value for(i = 1; i<num; ++i){// To find the smallest value, change if(*data< *(data + i)) *data = *(data + i); }printf("Largest element = %.2f", *data); return 0; } The output is: Enter the number of elements (1 ~ 100): 5Enter number 1: 12Enter number 2: 32Enter number 3: 6Enter number 4: 56Enter number 5: 21Largest element = 56.00 [![Image 4: C Examples](#) C Examples](#)
← C Examples Vowel Consonant FreC Examples Access Array Pointe β†’