PPSC EXERCISE-13

 

EXERCISE-13

1. Write a program in C to show how a function returning pointer.
SOURCE CODE:
#include<stdio.h>
int* findLarger(int*, int*);
int main() {
int num1, num2;
int *result;
printf("Enter two numbers : ");
scanf("%d %d", &num1, &num2);
result = findLarger(&num1, &num2);
printf("Largest number : %d\n" , *result);
return 0;
}

int* findLarger(int *num1, int *num2){
return *num1>*num2?num1:num2;
}
OUTPUT:
Enter two numbers : 4 6
Largest number : 6

2. Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc( ) function.
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]);
    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 : 2 6 1 5
Sum of all elements is : 14

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...