PROGRAM FIND SUM OF N ELEMENTS

No comments

/*PROGRAM FIND SUM OF N ELEMENTS*/


#include <stdio.h>
#include <stdlib.h>
intmain()
{
            intn,i,*ptr,sum=0;
            printf("Enter number of elements: ");
            scanf("%d",&n);
            ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc
    
            if(ptr==NULL){
            printf("Error! memory not allocated.");
            exit(0);
            }

            printf("Enter elements of array: ");
            for(i=0;i<n;++i){
            scanf("%d",ptr+i);
            sum+=*(ptr+i);
            }          

            printf("Sum=%d\n",sum);
            free(ptr);
            return 0;
}


OUTPUT :-


No comments :

Post a Comment

Subscribe

Milan Panda
Admin
About Me | Contact
Copyright 2023-2024 © Programming1011 . 🎀 Developed and Design By- Milan Panda. Happy Holi All Of You 🎀