PPSC EXERCISE-11

 

EXERCISE-11

1. Write a program in C to add numbers using call by reference.
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*)malloc(sizeof(int)*n);
    printf("Enter %d elements : ",n);
    for(i=0;i<n;i++)
        scanf("%d",&eles[i]);
    sum = add(eles, n);
    printf("Sum of all elements is : %d\n",sum);
    return 0;
}
int add(int *eles, int n){
    int i, sum=0;
    for(i=0;i<n;i++)
        sum = sum + eles[i];
    return sum;
}
OUTPUT:
Enter number of elements : 4
Enter 4 elements : 2 6 1 5
Sum of all elements is : 14

2. Write a program in C to find the largest element using Dynamic Memory Allocation.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
    int n, *eles, i, largest;
    printf("Enter number of elements : ");
    scanf("%d",&n);
    eles = (int*)malloc(sizeof(int)*n);
    printf("Enter %d elements : ",n);
    for(i=0;i<n;i++)
        scanf("%d",&eles[i]);
    largest = eles[0];
    for(i=0;i<n;i++){
        if(largest<eles[i])
            largest = eles[i];
    }
    printf("Largest element is : %d\n",largest);
    return 0;
}
OUTPUT:
Enter number of elements : 5
Enter 5 elements : 5 4 3 9 7
Largest element is : 9

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...