You are on page 1of 6

Unit 3 static question

1. Write a program that inputs a character and determines its predecessor according to two
rules:
If the character is 'a' or 'A', map it to 'z' or 'Z'
For any other character, map it to the one immediately before it.

Solution:

#include <stdio.h>
void processInput() {
char ch;
scanf(" %c", &ch);
if (ch == 'a' || ch == 'A') {
ch = (ch == 'a') ? 'z' : 'Z';
} else {
ch = ch - 1;
}
printf("%c\n", ch);
}
int main() {
processInput();
return 0;
}

2. Write a program to print DuckNumber using function. A Duck number is a positive


number which has zeroes present in it, For example 3210, 8050896, 70709 are all Duck
numbers. Please note that a numbers with only leading 0s is not considered as Duck
Number.

Solution:

#include <stdio.h>
int isDuckNumber(int n)
{
while (n > 0) {
int digit = n % 10;
if (digit == 0)
{
return 1;
}
n=n/10;
}
return 0;
}
int main() {
int num;
scanf("%d", &num);
if (isDuckNumber(num)) {
printf("The number is a duck number.\n");
} else {
printf("The number is NOT a duck number.\n");}
return 0;
}

3. Implement a function to check whether an integer n is an Armstrong number or not using a


call-by-reference function. Assist him in completing the program. An Armstrong number is
a number that is equal to the sum of its digits each raised to the power of the number of
digits.

Function Specifications: void checkArmstrong(int *n)

Input format :

The input consists of an integer n.

Output format :

If n is an Armstrong number, the output prints "Armstrong number". If n is not an Armstrong


number, the output prints "Not an Armstrong number".

Solution:

#include <stdio.h>
void checkArmstrong(int *n) {
int originalNumber, remainder, result = 0, digits = 0, temp;
originalNumber = *n;
temp = *n;
while (temp != 0) {
temp /= 10;
++digits;
}
temp = *n;
while (temp != 0) {
remainder = temp % 10;
int product = 1;
for (int i = 0; i < digits; ++i) {
product *= remainder;
}
result += product;
temp /= 10;
}
if (result == *n)
printf("Armstrong number");
else
printf("Not an Armstrong number");
}
int main() {
int n;
scanf("%d", &n);
checkArmstrong(&n);
return 0;
}

4. Imagine you are working on a financial application where users input various transaction
amounts. As part of a data analysis feature, you need to implement a function
countZeros(int n) to count the occurrences of zeros in the transaction amounts.

Write a program that uses recursion to achieve this.

Input format:

The input consists of an integer n, representing the transaction amount.

Output format :

The output prints the count of zero digits in the entered transaction amount.

Solution:

#include <stdio.h>
int countZeros(int n) {
if (n == 0)
return 1;
if (n < 10)
return 0;
return (n % 10 == 0) + countZeros(n / 10);
}
int main() {
int n;
scanf("%d", &n);
int zeros = countZeros(n);
printf("%d", zeros);
return 0;
}

5. Develop a program for a community event planning committee to identify special numbers,
defined as positive integers whose sum of proper divisors (excluding itself) equals the
number.

Utilize the function called sumOfDivisors to calculate the sum of divisors for a given positive
integer.

Example: If the number is 12, the sum of its divisors would be 1+2+3+4+6+12 = 28.
Input format :

The input consists of an integer n, representing the number of chocolates.

Output format :

The output prints the sum of the divisors of n (inclusive).

Solution:

#include <stdio.h>
int sumOfDivisors(int num, int divisor) {
if (divisor == 1) {
return 1;
} else {
if (num % divisor == 0) {
return divisor + sumOfDivisors(num, divisor - 1);
} else {
return sumOfDivisors(num, divisor - 1);
}
}
}
int main() {
int number;
scanf("%d", &number);
int result = sumOfDivisors(number, number);
printf("%d\n", result);
return 0;
}

6. Alex is developing a fitness application that utilizes a Collatz-like sequence to gamify step
goals. The program employs a static storage specifier. Implement a function
collatzSequence(int num), which calculates and displays the sequence for a given number of
steps.
The sequence is generated based on even and odd rules.
If n is even, then n = n / 2.
If n is odd, then n = 3*n + 1.
Repeat the above steps, until it becomes 1.
Example
Input:
3
Output:
3 10 5 16 8 4 2 1
Explanation:
The input of 3, and the Collatz sequence unfold as follows: 3 is odd, so it becomes 3 * 3 + 1 = 10.
Then, 10 is even, halving to 5, and the sequence continues: 16, 8, 4, 2, 1.

Solution
#include <stdio.h>
void collatzSequence(int num) {
static int step = 1;
printf("%d ",num);
if (num == 1) {
return;
}
if (num % 2 == 0) {
collatzSequence(num / 2);
} else {
collatzSequence(3 * num + 1);
}
}
int main() {
int number;
scanf("%d", &number);
collatzSequence(number);
return 0;
}

7. Meet Alex, a budget-savvy shopper who wants to give discounts to customers in the
following ways:

If the amount is greater than 5000 (not inclusive), the discount is 20%.

If it is greater than 2000 (not inclusive), the discount is 15%.

10% discount for a purchase amount greater than 1000 (not inclusive).

Write a program that takes the amount as input and calculates the discounts using a function
named applyDiscount. The discount rates are set as 10%, 15%, and 20% as global double
datatype variables.

Input format :

The input consists of a double value representing the amount.

Output format :

The output displays "Discounted amount: " followed by a double value representing the
discounted amount, rounded to two decimal places.

Solution:

#include <stdio.h>

double discount10 = 0.10;

double discount15 = 0.15;

double discount20 = 0.20;

double applyDiscount(double amount) {


if (amount > 5000) {

return amount - (amount * discount20);

} else if (amount > 2000) {

return amount - (amount * discount15);

} else if (amount > 1000) {

return amount - (amount * discount10);

} else {

return amount;

int main() {

double purchase;

scanf("%lf", &purchase);

double discountedAmount = applyDiscount(purchase);

printf("Discounted amount: %.2lf\n", discountedAmount);

return 0;

You might also like