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

C Function Programs

The document contains C programs demonstrating basic function variants, including a swap function, a prime number checker, a function to find the largest element in an array, and checks for Armstrong and perfect numbers. Each program includes a main function that tests the respective functionality and prints the results. The code snippets are structured to illustrate fundamental programming concepts in C.

Uploaded by

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

C Function Programs

The document contains C programs demonstrating basic function variants, including a swap function, a prime number checker, a function to find the largest element in an array, and checks for Armstrong and perfect numbers. Each program includes a main function that tests the respective functionality and prints the results. The code snippets are structured to illustrate fundamental programming concepts in C.

Uploaded by

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

C Programs for Basic Function Variants

3. Swap Numbers Function Variant

#include <stdio.h>

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

int main() {
int n1 = 2, n2 = 4;
printf("Before swapping: n1 = %d, n2 = %d\n", n1, n2);
swap(&n1, &n2);
printf("After swapping: n1 = %d, n2 = %d\n", n1, n2);
return 0;
}

7. Prime Check Function Variant

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int main() {
int num = 5;
if (isPrime(num))
printf("The number %d is a prime number.\n", num);
else
printf("The number %d is not a prime number.\n", num);
return 0;
}

8. Largest Element in Array Function Variant

#include <stdio.h>

int findLargest(int arr[], int n) {


int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max)
max = arr[i];
}
C Programs for Basic Function Variants

return max;
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int n = 5;
int max = findLargest(arr, n);
printf("The largest element is: %d\n", max);
return 0;
}

9. Armstrong & Perfect Number Check

#include <stdio.h>
#include <math.h>
#include <stdbool.h>

bool isArmstrong(int num) {


int original = num, sum = 0, digits = 0;
int temp = num;
while (temp > 0) {
digits++;
temp /= 10;
}
temp = num;
while (temp > 0) {
sum += pow(temp % 10, digits);
temp /= 10;
}
return sum == original;
}

bool isPerfect(int num) {


int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0)
sum += i;
}
return sum == num;
}

int main() {
int num = 371;
if (isArmstrong(num))
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);

if (isPerfect(num))
printf("%d is a Perfect number.\n", num);
C Programs for Basic Function Variants

else
printf("%d is not a Perfect number.\n", num);

return 0;
}

You might also like