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

Function

The document contains programming exercises in C, demonstrating various functions including calculating factorials, checking for prime numbers, identifying Armstrong numbers, and determining even or odd values. It also covers call by value and reference, as well as using recursion to print numbers backwards. Each exercise includes code snippets and explanations of the functionality.

Uploaded by

Hafiz Al Sami
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)
2 views3 pages

Function

The document contains programming exercises in C, demonstrating various functions including calculating factorials, checking for prime numbers, identifying Armstrong numbers, and determining even or odd values. It also covers call by value and reference, as well as using recursion to print numbers backwards. Each exercise includes code snippets and explanations of the functionality.

Uploaded by

Hafiz Al Sami
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

Name: SAMI HAFIZ AL STUDENT ID: 2411604115

Class: Computer Science Class-1

Fundamental of Programming: FUNCTION


Codes
Exercise 1: Create a Function to Calculate Factorials
#include <stdio.h>
int myFact(int inp) {
int f = 1;
for (int i = 1; i <= inp; i++) f *= i;
return f;
}
int main() {
int a;
printf("Number: ");
scanf("%d", &a);
printf("Factorial is: %d\n", myFact(a));
return 0;
}

Exercise 2: Function to Test for Prime Numbers


#include <stdio.h>
int isPrimeNumber(int x) {
if (x < 2) return 0;
for (int t = 2; t * t <= x; t++)
if (x % t == 0) return 0;
return 1;
}
int main() {
int v;
scanf("%d", &v);
if (isPrimeNumber(v)) printf("Prime\n");
else printf("Not prime\n");
return 0;
}

Exercise 3: Function for Armstrong Number Checking


#include <stdio.h>
int powr(int n, int e) {
int ans = 1;
for (int i = 0; i < e; i++) ans *= n;
return ans;
}
int isArmstrongNum(int t) {
int sum = 0, dig = 0, x = t, y = t;
while (x) { dig++; x /= 10; }
while (y) {
sum += powr(y % 10, dig);
y /= 10;
}
return sum == t;
}
int main() {
int z;
scanf("%d", &z);
printf("%s\n", isArmstrongNum(z) ? "Armstrong" : "Not Armstrong");
return 0;
}

Exercise 4: Write Functions to Identify Even and Odd Values


#include <stdio.h>
int evenNum(int n) { return n % 2 == 0; }
int oddNum(int n) { return n % 2 != 0; }
int main() {
for (int d = 1; d <= 10; d++) {
if (evenNum(d)) printf("%d is even\n", d);
if (oddNum(d)) printf("%d is odd\n", d);
}
return 0;
}

Exercise 5: Demonstrate Call by Value and Reference with


Functions
#include <stdio.h>
void testValue(int v) {
v = 99;
printf("Inside testValue: %d\n", v);
}
void testRef(int *w) {
*w = 55;
printf("Inside testRef: %d\n", *w);
}
int main() {
int n1 = 3, n2 = 3;
printf("Before: %d\n", n1);
testValue(n1);
printf("After: %d\n\n", n1);
printf("Before: %d\n", n2);
testRef(&n2);
printf("After: %d\n", n2);
return 0;
}
Exercise 6: Use Recursion to Print Numbers Backwards
#include <stdio.h>
void backwards(int k) {
if (k < 0) return;
printf("%d ", k);
backwards(k - 1);
}
int main() {
backwards(5);
printf("\n");
return 0;
}

You might also like