Add two numbers stored in Heap

Question - 
Given two numbers , we have to stored in heap and calculate the Addition of those numbers.
Example - 
Input : 2, 3
Output : sum is : 5
Input : 45, 10
Output : sum is : 55

Approach - 
we can store numbers in heap using malloc() defined in <stdlib.h> header file.

Implementation -


/****************************************************
We add two numbers stored in heap using pointer 
variable using C. 
****************************************************/

#include<stdio.h>
#include<stdlib.h>
int main()
{
    //Dynamic memory allocations using mellow defined in <stdlib.h>

    int *a=(int*)malloc(sizeof(int));
    int *b=(int*)malloc(sizeof(int));
    int *sum=(int*)malloc(sizeof(int));
        
    printf("Enter two numbers to Add :\n");
    scanf("%d",a);
    scanf("%d",b);
    
    //operate sum opetation

    (*sum)=(*a)+(*b);
    
    printf("Sum of %d and %d is : %d\n",*a,*b,*sum);
    
    return 0;
}





Comments