EXERCISE-14

1. Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc( ) function. Understand the difference between the above two programs.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
    int n, *eles, i, sum=0;
    printf("Enter number of elements : ");
    scanf("%d",&n);
    eles = (int*)calloc(n,sizeof(int));
    printf("Enter %d elements : ",n);
    for(i=0;i<n;i++)
        scanf("%d",&eles[i]);
    for(i=0;i<n;i++)
        sum = sum + eles[i];
    printf("Sum of all elements is : %d\n",sum);
    return 0;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 5 4 3 2
Sum of all elements is : 14

2. Write a program in C to convert decimal number to binary number using the function.
SOURCE CODE:
#include <stdio.h>
long decimalToBinary(int);
int main() {
int d;
long int b;
printf("Enter a positive decimal number : ");
scanf("%d", &d);
b=decimalToBinary(d);
printf("The binary number of decimal %d is : %ld\n", d, b);
return 0;
}
long decimalToBinary(int d){
long int bn=0, revbn=0;
int c=0;
while(d>0){
revbn=(revbn*10)+(d%2);
d=d/2;
c++;
}
while(c>0){
bn=(bn*10)+(revbn%2);
revbn=revbn/10;
c--;
}
return bn;
}
OUTPUT:
Enter a positive decimal number : 36
The binary number of decimal 36 is : 100100