C Unit 3 Answers
C Unit 3 Answers
Problem Statement
Arun is developing a program for his alphabet printing service. It prints the specified number of
alphabet characters from a to z based on client input.
Create a program for Arun's alphabet printing service. It takes an integer N as input and prints the
requested alphabet characters using the function printAlphabet().
Input format :
The input consists of an integer N, representing the number of alphabet characters requested.
Output format :
The output prints the consecutive alphabet characters starting from 'a' till the Nth position,
separated by a space.
Code constraints :
In this scenario, the test cases fall under the following constraints:
1 ≤ N ≤ 26
Input 1 :
Output 1 :
Input 2 :
Output 2 :
abcdef
Input 3 :
13
Output 3 :
abcdefghijklm
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
Answer
#include <stdio.h>
void printAlphabet(int N) {
}
}
int main() {
int N;
return 0;
Problem Statement
Manoj wants to explore numbers. The number should be even and also a multiple of 10. Write a
program to obtain a number x and check if it is even or not. If even, check whether it is a multiple of
10 or not.
Function Specifications:
Input format :
Output format :
The first line displays "Even" if x is even or "Not even" otherwise.
If x is even, the second line displays "Multiple of 10" if x is a multiple of 10 or "Not a multiple of 10"
otherwise.
Code constraints :
In this scenario, the test cases fall under the following constraints:
1 ≤ x ≤ 106
Input 1 :
50
Output 1 :
Even
Multiple of 10
Input 2 :
13
Output 2 :
Not even
Input 3 :
1
Output 3 :
Not even
Input 4 :
1456
Output 4 :
Even
Not a multiple of 10
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
void iseven(int x) {
if (x % 2 == 0)
printf("Even\n");
else
printf("Not even\n");
void ismultiple(int x) {
if (x % 10 == 0)
printf("Multiple of 10\n");
else
printf("Not a multiple of 10\n");
int main() {
int x;
iseven(x);
if (x % 2 == 0)
ismultiple(x);
return 0;
```
This program defines two functions `iseven` and `ismultiple` to check whether a given number is
even and whether it is a multiple of 10, respectively. In the `main` function, it reads an integer `x`
from the user input and then calls `iseven` to check if `x` is even or not. If `x` is even, it further calls
`ismultiple` to check if `x` is a multiple of 10. Finally, it returns 0 to indicate successful execution.
Problem Statement
Beula is solving a mathematical problem and seeks your assistance in writing a program. She is
particularly interested in finding numbers with exactly 9 factors.
Help her by creating a program that takes an integer N as input and identifies numbers from 1 to N
that have exactly 9 factors.
Function Specifications:
check_9_factors - Prints the integers that have exactly 9 divisors as well as the total number of such
integers.
Note: This question helps in clearing technical coding tests for companies like Infosys.
Input format :
Output format :
The first line displays space-separated integers representing numbers from 1 to N that have exactly 9
factors.
The second line displays a single integer representing the count of numbers with exactly 9 factors.
Refer to the sample output for formatting specifications.
Code constraints :
In this scenario, the test cases fall under the following constraints:
100 ≤ N ≤ 1500
Input 1 :
100
Output 1 :
36 100
Input 2 :
150
Output 2 :
36 100
Input 3 :
1500
Output 3 :
36 100 196 225 256 441 484 676 1089 1156 1225 1444
12
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
int count = 0;
if (num % i == 0) {
count++;
count += 2;
void check_9_factors(int N) {
int count = 0;
printf("%d", has_9_factors(36) ? 36 : ""); // 36 has 9 factors, so print it first if it's within the
range
if (has_9_factors(i)) {
printf("\n%d\n", count); // Print the total count of numbers with exactly 9 factors
int main() {
int N;
return 0;
```
- `check_9_factors` iterates through numbers from 1 to N and prints those that have exactly 9
factors, along with counting them.
In the `main` function, it takes an integer input `N` from the user and then calls `check_9_factors`
to identify and print the numbers with exactly 9 factors. Finally, it returns 0 to indicate successful
execution.
Problem Statement
Shabu is exploring factorials and wants a program to calculate the sum of a series involving factorials.
Develop a simple program that should take an integer n as input and calculate the sum of the series:
1!/1 + 2!/2 + 3!/3 + 4!/4 + 5!/5 + ... + n!/n.
Implement a program to help Shabu with a call-by-value function called factorial to calculate
factorials, and the main program should compute the sum of the series.
Input format :
Output format :
The output displays a single integer, representing the sum of the series.
Code constraints :
In this scenario, the test cases fall under the following constraints:
2 ≤ n ≤ 10
Input 1 :
2
Output 1 :
Input 2 :
Output 2 :
154
Input 3 :
10
Output 3 :
409114
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
int main() {
int n, sum = 0;
return 0;
```
This program defines a function `factorial` to calculate the factorial of a number recursively. In the
`main` function, it takes an integer input `n` from the user. Then, it iterates from 1 to `n`, calculates
the factorial of each number using the `factorial` function, divides it by the number itself, and adds
it to the sum. Finally, it prints the sum of the series.
Problem Statement
To help Alice, a new property owner in a city, calculate the property tax, let's create a simple
program. Implement a function called propertyTaxCalc, which takes the assessed value as an input
argument.
The function should determine the taxable amount by subtracting 8% from the assessed value and
also calculate the property tax by applying a tax rate of 1.05% to the taxable amount.
Input format :
The input consists of a double value that represents the assessed value.
Output format :
The first line displays "Taxable Amount: " followed by a double value, representing the taxable
amount with two decimal places.
The second line displays "Property Tax: " followed by a double value, representing the property tax
with two decimal places.
Code constraints :
In this scenario, the test cases fall under the following constraints:
Input 1 :
200000.88
Output 1 :
Input 2 :
987500.49
Output 2 :
Taxable Amount: 908500.45
Input 3 :
1000000.00
Output 3 :
Input 4 :
100000.0
Output 4 :
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
// Calculate property tax by applying a tax rate of 1.05% to the taxable amount
int main() {
double assessedValue;
return 0;
```
This program defines a function `propertyTaxCalc` to calculate the taxable amount and property
tax based on the assessed value.
- It first calculates the taxable amount by subtracting 8% from the assessed value.
- Then, it calculates the property tax by applying a tax rate of 1.05% to the taxable amount.
- In the `main` function, it takes a double input `assessedValue` from the user and then calls
`propertyTaxCalc` to calculate and print the taxable amount and property tax. Finally, it returns 0
to indicate successful execution.
Problem Statement
Abigail, a curious mind fascinated by the occurrences of the digit '3' in numbers. Abigail has a keen
interest in counting the number of times the digit '3' appears in a range of numbers. She wants to
know the total count of occurrences from the number 2 to the given number n.
Write a program that takes the input n, calculates, and displays the total count of occurrences of the
digit '3' in the range from 2 to n.
Function Signatures:
int count_3s(int n) - Counts and returns the number of occurrences of the digit 3 in a given integer.
int count_in_range(int n) - Calculates and returns the total count of the digit 3 in all numbers from 2
to n.
Input format :
The input consists of an integer n, representing the upper limit of the range for counting 3s.
Output format :
The output displays the total count of the digit 3 from 2 to n (inclusive) as an integer.
Code constraints :
The given test cases will fall under the following constraints:
50 ≤ n ≤ 1500
50
Output 1 :
15
Input 2 :
100
Output 2 :
20
Input 3 :
1500
Output 3 :
500
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
int count = 0;
count++;
return count;
}
// Function to calculate the total count of the digit 3 in all numbers from 2 to n
int count_in_range(int n) {
int total_count = 0;
total_count += count_3s(i); // Add the count of 3s in each number to the total count
return total_count;
int main() {
int n;
printf("%d\n", count_in_range(n)); // Calling function to calculate and display the total count
return 0;
```
- `count_3s` takes an integer as input and counts the occurrences of the digit '3' in that number.
- `count_in_range` calculates the total count of the digit '3' in all numbers from 2 to `n`, inclusive.
In the `main` function, it takes an integer input `n` from the user and then calls `count_in_range` to
calculate and print the total count of the digit '3' from 2 to `n`. Finally, it returns 0 to indicate
successful execution.
Problem Statement
Tony is exploring functions and needs your help. Write a program to assist Tony where he inputs an
integer N. The program should display the factors of N and count how many digits N has.
Function Specifications:
Input format :
Output format :
The first line of output prints "Factors: " followed by the factors of N, separated by a space.
The second line of output prints "Total digits: " followed by the total digits of N.
The given test cases will fall under the following constraints:
3 ≤ N ≤ 1000
Input 1 :
99
Output 1 :
Factors: 1 3 9 11 33 99
Total digits: 2
Input 2 :
458
Output 2 :
Total digits: 3
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
void factors(int N) {
printf("Factors: ");
printf("\n");
*count = 0;
while (N != 0) {
++(*count);
N /= 10;
int main() {
int N;
int digit_count;
return 0;
```
Explanation:
- The `factors` function takes an integer `N` as input and prints its factors separated by space.
- The `count_digits` function takes an integer `N` and a pointer to an integer `count`. It counts the
digits of `N` and stores the count in the memory location pointed to by `count`.
- In the `main` function, it takes an integer input `N` from the user. Then it calls `factors` to display
the factors of `N`. After that, it calls `count_digits` to count the digits of `N` and stores the count in
`digit_count`. Finally, it prints the total digits of `N`.
Problem Statement
In a team-oriented context, you are tasked with managing a rearrangement process for team
members represented by three integers: a, b, and c.
Create a program that takes initial input values for a, b, and c and displays them. Then, perform a
shift operation by moving a to b, b to c, and c to a using a call-by-reference function and print the
updated values after the shift.
Input format :
The input consists of three space-separated integers a, b, and c representing the team members.
Output format :
The first line displays the values of the team members before shifting, separated by a space.
The second line displays the values of the team members after shifting, separated by a space.
Code constraints :
In this scenario, the test cases fall under the following constraints:
1 ≤ a, b, c ≤ 1000
Input 1 :
15 41 23
Output 1 :
15 41 23
23 15 41
Input 2 :
Output 2 :
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
// You are using GCC
#include <stdio.h>
//a=c
//b=a
//c=b
*a = *c;
*b = temp;
*c = temp2;
int main() {
int a, b, c;
return 0;
Problem Statement
Imagine you are tasked with creating a program that calculates the cube of a given number.
Your goal is to write a program that takes an integer n from the user, calculates its cube using a call-
by-reference function, and then prints the result.
Input format :
Output format :
Code constraints :
In this scenario, the test cases fall under the following constraints:
1 ≤ n ≤ 103
Input 1 :
Output 1 :
Input 2 :
Output 2 :
125
Input 3 :
1000
Output 3 :
1000000000
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
}
int main() {
int n;
return 0;
```
Explanation:
- The `cube` function takes a pointer to an integer `n` as a parameter. It calculates the cube of the
value pointed to by `n` and returns the result.
- In the `main` function, it takes an integer input `n` from the user. Then, it calls the `cube` function
by passing the address of `n` (`&n`). The result is stored in the `result` variable.
- Finally, it prints the result, which is the cube of the input number.
Problem Statement
Benjamin is studying polar coordinates and wants to convert them to Cartesian coordinates. Develop
a program that takes the radius (r) and angle (θ) in degrees as input, converts the angle to radians
using inbuilt mathematical functions, and outputs the Cartesian coordinates (x, y).
Formula
x = radius * cos(radian)
y = radius * sin(radian)
The pi value is calculated using M_PI constant from the math library.
Input format :
Output format :
The output prints two space-separated double values representing the Cartesian coordinates(x, y),
both rounded to one decimal place.
Refer to the sample output for the formatting specifications.
Code constraints :
In the given scenario, the test cases fall under the following constraints:
1.0 ≤ r ≤ 50.0
Input 1 :
5.0
45.0
Output 1 :
3.5 3.5
Input 2 :
50.0
359.0
Output 2 :
50.0 -0.9
Input 3 :
1.0
10.0
Output 3 :
1.0 0.2
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
#include <stdio.h>
void recursiveFunc(int n) {
auto m = 3;
if (n > 0) {
recursiveFunc(n - 1);
int main() {
int n;
scanf("%d", &n);
recursiveFunc(n);
return 0;
Problem Statement
Help Alex, a new programmer, create a straightforward program. Ask for a positive number, use a
function named isDuckNumber() to check if it's a duck number (having at least one zero, not as the
first digit), and print the result in the main function.
This tool aids Alex in spotting duck numbers, enhancing their programming journey.
Note: This question helps in clearing technical coding tests for companies like Amazon and Adobe.
Input format :
Output format :
The output prints "The number is a duck number." if the entered number is a duck number or "The
number is NOT a duck number." otherwise.
Code constraints :
In the given scenario, the test cases fall under the following constraints:
1 ≤ n ≤ 1000
Input 1 :
Output 1 :
Input 2 :
330
Output 2 :
Input 3 :
012
Output 3 :
Input 4 :
1000
Output 4 :
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
#include <stdio.h>
int isDuckNumber(int n) {
// Check if the number is less than or equal to zero (not a duck number)
if (n <= 0) {
return 0;
while (n > 0) {
if (n % 10 == 0) {
return 0;
int main() {
int number;
scanf("%d", &number);
if (isDuckNumber(number)) {
} else {
return 0;
Arun wants to 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.
Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable
Solutions.
Input format :
Output format :
Code constraints :
In this scenario, the test cases fall under the following constraints:
1 ≤ n ≤ 1000
Input 1 :
153
Output 1 :
Armstrong number
Input 2 :
29
Output 2 :
Input 3 :
Output 3 :
Armstrong number
Input 4 :
1000
Output 4 :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
#include <stdio.h>
while (original != 0) {
original /= 10;
num_of_digits++;
while (original != 0) {
original /= 10;
if (sum == *n) {
printf("Armstrong number\n");
} else {
int main() {
int number;
scanf("%d", &number);
return 0;
Problem Statement
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.
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.
Code constraints :
In the given scenario, the test cases fall under the following constraints:
100 ≤ n ≤ 105
Input 1 :
505
Output 1 :
Input 2 :
12345
Output 2 :
Input 3 :
100
Output 3 :
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
```c
#include <stdio.h>
int countZeros(int n) {
if (n == 0) {
return 1;
// Recursive case: Check the last digit and count zeros in remaining digits
if (n % 10 == 0) {
} else {
int main() {
int transactionAmount;
scanf("%d", &transactionAmount);
return 0;
}
```
Explanation:
- Takes an integer `n` as input, representing the transaction amount or a part of it.
- **Base case:**
- If `n` is zero, it means a zero digit has been encountered, so the function returns 1 to count it.
- **Recursive case:**
- If it's zero, `return 1 + countZeros(n / 10);` counts 1 for the current zero digit and calls itself
recursively with `n / 10` to count zeros in the remaining digits.
- If it's not zero, `return countZeros(n / 10);` simply calls itself recursively with `n / 10` to
process the remaining digits.
2. **`main` function:**
- Calls the `countZeros` function with the transaction amount to count zeros.
Problem Statement
Nimisha is assigned to calculate the sum of divisors for a given positive integer. She needs a function
called sumOfDivisors to calculate the sum of divisors for a given positive integer.
Help her with the program.
Example: If the number is 12, the sum of its divisors would be 1+2+3+4+6+12 = 28.
Input format :
Output format :
Code constraints :
In the given scenario, the test cases fall under the following constraints:
4 ≤ n ≤ 1000
Input 1 :
12
Output 1 :
28
Input 2 :
357
Output 2 :
576
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
Here's the C code for Nimisha's program to calculate the sum of divisors:
```c
#include <stdio.h>
int sumOfDivisors(int n) {
if (n % i == 0) { // Check if i is a divisor
if (i != n / i) {
sum += n / i;
}
return sum;
int main() {
int number;
scanf("%d", &number);
return 0;
```
Explanation:
1. **`sumOfDivisors` function:**
- Takes an integer `n` as input, representing the number for which the sum of divisors needs to be
calculated.
- **Initialization:**
- `sum = 1;` initializes the `sum` variable with 1 to account for 1 being a divisor of every number.
- A loop iterates from `i = 2` to the square root of `n` (inclusive) because any divisor greater than
the square root will have a corresponding pair less than the square root (e.g., for 12, 6 is a divisor,
and its corresponding pair is 2).
- If it's a divisor, `sum += i;` adds the divisor `i` to the `sum`.
- To avoid counting divisors twice (e.g., 6 and 2 for 12), we check if `i` is not equal to `n / i`. If
they are not equal, it means `i` has a corresponding divisor (`n / i`), so we add `n / i` to the `sum` as
well.
- **Return sum:**
- After the loop iterates through all potential divisors, the function returns the final `sum` which
represents the sum of all divisors of `n`.
2. **`main` function:**
Problem Statement
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%.
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 output displays "Discounted amount: " followed by a double value representing the discounted
amount, rounded to two decimal places.
Code constraints :
In this scenario, the test cases fall under the following constraints:
Input 1 :
2000.0
Output 1 :
Input 2 :
1100.50
Output 2 :
Input 3 :
10000.0
Output 3 :
Discounted amount: 8000.00
Input 4 :
1000.0
Output 4 :
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
Here's the C code for Alex's discount program using global variables:
```c
#include <stdio.h>
} else {
int main() {
double purchaseAmount;
scanf("%lf", &purchaseAmount);
return 0;
```
Explanation:
- We declare three global double variables `discount10`, `discount15`, and `discount20` to store
the discount rates (10%, 15%, and 20%).
2. **`applyDiscount` function:**
- Uses a series of `if-else if` statements to check the amount and apply the appropriate discount:
- If `amount` is greater than 5000, it applies a 20% discount by multiplying `amount` with (1 -
discount20).
- Similar logic is applied for discounts of 15% and 10% based on the amount range.
3. **`main` function:**
- Calls the `applyDiscount` function with the entered amount to calculate the discounted amount.
- Optionally, you can use `round(discountedAmount * 100) / 100.0` to round the discounted
amount to two decimal places before printing it (uncomment the line if needed).
- Prints the "Discounted amount" message followed by the calculated discounted amount using
`printf` with a format specifier for two decimal places.
Problem Statement
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.
If n is even, then n = n / 2.
Input:
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.
Input format :
Output format :
The output should display the Collatz sequence separated by space starting from the entered
number.
Refer to the sample output for the formatting specifications.
Code constraints :
In this scenario, the test cases will fall under the following constraints:
1 ≤ n ≤ 100
Input 1 :
100
Output 1 :
100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Input 2 :
Output 2 :
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Input 3 :
Output 3 :
3 10 5 16 8 4 2 1
Note :
The program will be evaluated only after the “Submit Code” is clicked.
Extra spaces and new line characters in the program output will result in the failure of the test case.
Here's the C code for Alex's fitness application program that implements the Collatz-like sequence
using a static variable:
```c
#include <stdio.h>
void collatzSequence(int num) {
if (num == 1) {
printf("1 ");
return;
if (num % 2 == 0) {
collatzSequence(num / 2);
} else {
int main() {
int steps;
scanf("%d", &steps);
collatzSequence(steps);
printf("\n");
return 0;
```
Explanation:
- We declare a `static int count = 0;` variable inside the `collatzSequence` function. The `static`
keyword makes it retain its value between function calls. This is used to track the number of times
the function has been called within a single sequence calculation.
2. **`collatzSequence` function:**
- Takes an integer `num` as input, representing the starting number for the sequence.
- **Base case:**
- If `num` is 1, it prints 1 (the end of the sequence) and resets the `count` to 0 for the next
sequence calculation. This ensures the sequence starts fresh for each call to the function.
- If `num` is even, it calls itself recursively with `num / 2` to follow the Collatz rule for even
numbers (divide by 2).
- If `num` is odd, it calls itself recursively with `3 * num + 1` to follow the rule for odd numbers
(multiply by 3 and add 1).
3. **`main` function:**
- Calls the `collatzSequence` function with the entered number of steps to initiate the sequence
calculation.
- Prints a newline character after the sequence is displayed.