Problem 1:-
You're given a positive integer, and you need to check whether its digits form a "pyramid
pattern". Here, the pyramid pattern means digits first increase, then decrease (like 12321,
45654). ( Hint. You can solve it using one while loop)
My code:-
#include <stdio.h>
int isPyramid(int num) {
int prevDigit = num % 10;
num /= 10;
int increasing = 1;
int hasIncreased = 0;
int hasDecreased = 0;
while (num > 0) {
int currentDigit = num % 10;
if (increasing) {
if (currentDigit < prevDigit) {
hasIncreased = 1;
} else if (currentDigit > prevDigit) {
if (!hasIncreased) {
return 0;
}
increasing = 0;
hasDecreased = 1;
} else {
return 0;
}
} else {
if (currentDigit < prevDigit) {
hasDecreased = 1;
} else {
return 0;
}
}
prevDigit = currentDigit;
num /= 10;
}
return hasIncreased && hasDecreased;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (isPyramid(n)) {
printf("%d forms a pyramid pattern.\n", n);
} else {
printf("%d does NOT form a pyramid pattern.\n", n);
}
return 0;
}
Output:-
Problem 02:-
In the Kingdom of Numeron, pairs of knights are sent on missions. But only special
knight pairs (i, j) are allowed to go together.
A pair (i, j) is considered special if:
●1≤i<j≤n
● i * j is divisible by i + j
Your task is to count all such special pairs between 1 and n.
(Hint. Use nested for loop.)
My code:-
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if ((i * j) % (i + j) == 0) {
count++;
}
}
}
printf("Total number of special knight pairs: %d\n", count);
return 0;
}
Output:-