Write a program to Find the Largest Number Using Dynamic Memory Allocation.
C Program
#include <stdio.h>#include <stdlib.h>int main(){int num;int *dataMemory;printf("Enter the number of elements: ");scanf("%d", &num);// memory allocationdataMemory = (int *)calloc(num, sizeof(int));if (dataMemory == NULL){printf("Error!!! memory not allocated.");exit(0);}printf("Enter numbers:\n");for (int i = 0; i < num; ++i){scanf("%d", dataMemory + i);}// Finding the largest numberfor (int i = 1; i < num; ++i){if (*dataMemory < *(dataMemory + i)){*dataMemory = *(dataMemory + i);}}printf("Largest number is %d", *dataMemory);return 0;}
Input:
Enter the number of elements: 4
Enter numbers:
2
3
4
5
Output:
Largest number is 5
No comments:
Post a Comment