0% found this document useful (0 votes)
7 views2 pages

exam-programs

The document contains a C program that implements various mathematical and pattern-generating functions, including pyramid and diamond patterns, Floyd's triangle, Pascal's triangle, Fibonacci series, and checks for Armstrong, perfect, prime factors, palindrome, and strong numbers. Each function is defined but not called in the main function, which is set up for testing. The program demonstrates basic programming concepts such as loops, conditionals, and recursion.

Uploaded by

Bibin K Bino
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)
7 views2 pages

exam-programs

The document contains a C program that implements various mathematical and pattern-generating functions, including pyramid and diamond patterns, Floyd's triangle, Pascal's triangle, Fibonacci series, and checks for Armstrong, perfect, prime factors, palindrome, and strong numbers. Each function is defined but not called in the main function, which is set up for testing. The program demonstrates basic programming concepts such as loops, conditionals, and recursion.

Uploaded by

Bibin K Bino
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/ 2

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

h>
// Function declarations void pyramidPattern(); void diamondPattern(); void
numberPattern(); void pascalTriangle(); void fibonacci(); void armstrongNum-
ber(); void perfectNumber(); void primeFactors(); void palindromeNumber();
void strongNumber();
int main() { // You can call any function here to test return 0; }
// 1. Pyramid Pattern with numbers void pyramidPattern() { int rows = 5, k
= 0, count = 0, count1 = 0;
for (int i = 1; i <= rows; ++i) { for (int space = 1; space <= rows - i; ++space)
{ printf(” ”); ++count; } while (k != 2 * i - 1) { if (count <= rows - 1) {
printf(”%d ”, i + k); ++count; } else { ++count1; printf(”%d ”, (i + k - 2 *
count1)); } ++k; } count1 = count = k = 0; printf(”\n”); } }
// 2. Diamond Pattern void diamondPattern() { int n = 5; // Upper half for
(int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) printf(” ”); for (int j
= 1; j <= 2 * i - 1; j++) printf(”*”); printf(”\n”); } // Lower half for (int i =
n - 1; i >= 1; i--) { for (int j = 1; j <= n - i; j++) printf(” ”); for (int j = 1; j
<= 2 * i - 1; j++) printf(”*”); printf(”\n”); } }
// 3. Number Pattern - Floyd’s Triangle void numberPattern() { int rows = 5,
number = 1;
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf(”%d ”,
number); ++number; } printf(”\n”); } }
// 4. Pascal’s Triangle void pascalTriangle() { int rows = 5; for (int i = 0; i <
rows; i++) { // Print spaces for (int space = 1; space <= rows - i; space++)
printf(” ”);
int coef = 1; for (int j = 0; j <= i; j++) { printf(”%4d”, coef); coef = coef * (i
- j) / (j + 1); } printf(”\n”); } }
// 5. Fibonacci Series with recursion int fibRecursive(int n) { if (n <= 1) return
n; return fibRecursive(n - 1) + fibRecursive(n - 2); }
void fibonacci() { int n = 10; printf(”Fibonacci Series: ”); for (int i = 0; i < n;
i++) printf(”%d ”, fibRecursive(i)); }
// 6. Armstrong Number checker void armstrongNumber() { int num, original-
Num, remainder, result = 0, n = 0; printf(”Enter an integer: ”); scanf(”%d”,
&num);
originalNum = num;
// Count number of digits while (originalNum != 0) { originalNum /= 10; ++n;
}
originalNum = num;

1
// Calculate result while (originalNum != 0) { remainder = originalNum % 10;
result += pow(remainder, n); originalNum /= 10; }
if (result == num) printf(”%d is an Armstrong number\n”, num); else
printf(”%d is not an Armstrong number\n”, num); }
// 7. Perfect Number checker void perfectNumber() { int num, sum = 0;
printf(”Enter a number: ”); scanf(”%d”, &num);
for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; }
if (sum == num) printf(”%d is a Perfect number\n”, num); else printf(”%d is
not a Perfect number\n”, num); }
// 8. Prime Factors void primeFactors() { int num; printf(”Enter a number: ”);
scanf(”%d”, &num);
printf(”Prime factors of %d are: ”, num);
while (num % 2 == 0) { printf(”2 ”); num = num / 2; }
for (int i = 3; i <= sqrt(num); i += 2) { while (num % i == 0) { printf(”%d
”, i); num = num / i; } }
if (num > 2) printf(”%d”, num); }
// 9. Palindrome Number checker void palindromeNumber() { int num,
reversedNum = 0, remainder, originalNum; printf(”Enter an integer: ”);
scanf(”%d”, &num);
originalNum = num;
while (num != 0) { remainder = num % 10; reversedNum = reversedNum * 10
+ remainder; num /= 10; }
if (originalNum == reversedNum) printf(”%d is a palindrome\n”, originalNum);
else printf(”%d is not a palindrome\n”, originalNum); }
// 10. Strong Number checker (sum of factorial of digits) void strongNumber()
{ int num, originalNum, rem, sum = 0; printf(”Enter a number: ”); scanf(”%d”,
&num);
originalNum = num;
while (num != 0) { rem = num % 10; int fact = 1; for (int i = 1; i <= rem;
i++) { fact *= i; } sum += fact; num /= 10; }
if (sum == originalNum) printf(”%d is a Strong number\n”, originalNum); else
printf(”%d is not a Strong number\n”, originalNum); }

You might also like