0% found this document useful (0 votes)
34 views3 pages

Assignment 1 201

The document contains 6 programs written in C using pointers: 1) find maximum of two numbers, 2) store array elements and print, 3) find factorial, 4) print array in reverse, 5) find array sum using function, 6) find maximum element and its location.

Uploaded by

usmanmahmud234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

Assignment 1 201

The document contains 6 programs written in C using pointers: 1) find maximum of two numbers, 2) store array elements and print, 3) find factorial, 4) print array in reverse, 5) find array sum using function, 6) find maximum element and its location.

Uploaded by

usmanmahmud234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

191103068

Usman Imam Mahmud

1. Program to find the maximum number between two numbers using a pointer:

#include <stdio.h>
int main()
int num1, num2; int *ptr1, *ptr2;
printf ("Input the first number: "); scanf ("%d", &num1);
printf("Input the second number: ");
scanf ("%d", &num2);
ptr1 = &num1;
ptr2 = &num2;
if (*ptr1 > *ptr2) { printf("%d is the maximum number.
\n", *ptr1);
}
else { printf("%d is the maximum number.
\n",*ptr2);
}
Return 0;

2. 2. Program to store n elements in an array and print the elements using a pointer:

#include <stdio.h>
int main() {
int n, i;
int arr[100]; int *ptr;
printf ("Enter the number of elements: ");
scanf ("%d", &n);
printf("Enter the elements:In");
for (i = 0; i < n; i++) {
scanf("%d", &arr [i]);

ptr = arr;
printf(Elements in the array are: ");
for (i = 0; i < n; i++)
printf("%d ", *(ptr + i));
}
Return 0;
}

3. 3. Program to find the factorial of a given


number using pointers:
#include <stdio.h>
long long factorial (int *num);
int main() {
int num; int *ptr;
printf("Enter a number: ");
scanf ("%d", &num);
ptr = &num;
printf(Factorial of %d is %lld|n", *ptr, factorial (ptr));
Return 0;
}
long long factorial (int *num) { long long fact = 1;
int i;
for (i = 1; i <= *num; i++) { fact *=i

return fact;

4. 4. Program to read n number of values in an array and display them in reverse order:

#include <stdio.h>
int main()
int n;
printf("Enter the number of elements:');
scanf("%d", &n);
int arrIn];
printf("Enter the elements: (n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
}
printf("The elements in reverse order are: ");
for (int i = n-1; i >= 0; i--) 1
printf("%d", arr[i]);
}
return 0;

5. 5. Program to find the sum of all elements of the array using a user-defined function:

#include < stdio.h>


int getArraySum(int arr[], int n);
int main()
int n;
printf("Enter the number of elements:");
Scanf ("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i< n; i++)
scanf("%d", &arr[i]);
}
int sum = getArraySum(arr, n);
printf("Sum of array elements: %dIn", sum);
return 0;

int getArray Sum(int arr], int n) {
int sum = 0;
for (int i = 0; i<n; it+)
sum += arr[i];

return sum;

6. #include <stdio.h>

int main()

int array[100], maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");


scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)


scanf("%d", &array[c]);

maximum = array[0];

for (c = 1; c < size; c++)


{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}

printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}

You might also like