How to Write a c program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation in C Programming Language ?
Solution:
/*C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation*/
#include<stdio.h>
#include<conio.h>
void main()
{
int *p,n,i,max=0;
clrscr();
printf("\nEnter the n number: ");
scanf("%d",&n);
p=(int*)malloc(n * sizeof(int));
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("\nEntered elements are:\n ");
for(i=0;i<n;i++)
{
printf("\n\t%d",p[i]);
printf("\n");
}
max=p[0];
for(i=0;i<n;i++)
{
if(max<p[i])
{
max=p[i];
}
}
printf("\nMaximum number is: %d",max);
getch();
}
//OUTPUT:
//Enter the n number: 5
//Enter the elements:1
//2
//3
//4
//5
//Entered elements are:
// 1
// 2
// 3
// 4
// 5
//Maximum number is: 5