0% found this document useful (0 votes)
58 views54 pages

C Unit 3 Answers

vdfbvdfbvdfbdfbfd

Uploaded by

kartikevergreen7
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)
58 views54 pages

C Unit 3 Answers

vdfbvdfbvdfbdfbfd

Uploaded by

kartikevergreen7
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
You are on page 1/ 54

Question:

Single File Programming Question

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.

Refer to the sample output for formatting specifications.

Code constraints :
In this scenario, the test cases fall under the following constraints:

1 ≤ N ≤ 26

Sample test cases :

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>

// Function to print alphabet characters

void printAlphabet(int N) {

for (int i = 0; i < N; i++) {

printf("%c ", 'a' + i);

}
}

int main() {

int N;

scanf("%d", &N); // Taking input

printAlphabet(N); // Calling function to print alphabet

return 0;

Single File Programming Question

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:

void iseven(int x) - Prints whether the input number is even or not.

void ismultiple(int x) - Prints whether the input number is a multiple of 10 or not.

Input format :

The input consists of an integer x.

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.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ x ≤ 106

Sample test cases :

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.

Here's the C program to solve the given problem:

```c

#include <stdio.h>

// Function to check if a number is even or not

void iseven(int x) {

if (x % 2 == 0)

printf("Even\n");

else

printf("Not even\n");

// Function to check if a number is a multiple of 10 or not

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;

scanf("%d", &x); // Taking input

// Checking if the number is even or not

iseven(x);

// If the number is even, checking if it is a multiple of 10 or not

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.

Single File Programming Question

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 :

The input consists of an integer N.

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

Sample test cases :

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.

Here's the C program to solve the given problem:

```c

#include <stdio.h>

// Function to check if a number has exactly 9 factors

int has_9_factors(int num) {

int count = 0;

for (int i = 1; i * i <= num; i++) {

if (num % i == 0) {

// If i divides num, then increment count

if (num / i == i) // If both divisors are same, count only once

count++;

else // Otherwise count both divisors

count += 2;

return count == 9; // Return true if count is exactly 9

// Function to print numbers with exactly 9 factors

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

for (int i = 37; i <= N; i++) {

if (has_9_factors(i)) {

printf(" %d", i); // Print the number if it has exactly 9 factors


count++;

printf("\n%d\n", count); // Print the total count of numbers with exactly 9 factors

int main() {

int N;

scanf("%d", &N); // Taking input

check_9_factors(N); // Calling function to identify numbers with exactly 9 factors

return 0;

```

This program defines two functions `has_9_factors` and `check_9_factors`.

- `has_9_factors` checks if a number has exactly 9 factors or not.

- `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.

Single File Programming Question

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 :

The input consists of an integer, denoting the value of n.

Output format :

The output displays a single integer, representing the sum of the series.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

2 ≤ n ≤ 10

Sample test cases :

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.

Here's the C program to solve the given problem:

```c

#include <stdio.h>

// Function to calculate factorial of a number

int factorial(int n) {

if (n == 0 || n == 1)

return 1;

else

return n * factorial(n - 1);

int main() {

int n, sum = 0;

scanf("%d", &n); // Taking input


// Computing the sum of the series

for (int i = 1; i <= n; i++) {

sum += factorial(i) / i; // Adding factorial(i) / i to the sum

printf("%d\n", sum); // Printing the sum

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.

Single File Programming Question

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.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

105.00 ≤ Assessed value ≤ 106.00

Sample test cases :

Input 1 :

200000.88

Output 1 :

Taxable Amount: 184000.81

Property Tax: 1932.01

Input 2 :

987500.49

Output 2 :
Taxable Amount: 908500.45

Property Tax: 9539.25

Input 3 :

1000000.00

Output 3 :

Taxable Amount: 920000.00

Property Tax: 9660.00

Input 4 :

100000.0

Output 4 :

Taxable Amount: 92000.00

Property Tax: 966.00

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 program to solve the given problem:

```c

#include <stdio.h>

// Function to calculate property tax

void propertyTaxCalc(double assessedValue) {

// Calculate taxable amount by subtracting 8% from the assessed value

double taxableAmount = assessedValue * 0.92;

// Calculate property tax by applying a tax rate of 1.05% to the taxable amount

double propertyTax = taxableAmount * 0.0105;

// Printing the results with two decimal places

printf("Taxable Amount: %.2lf\n", taxableAmount);


printf("Property Tax: %.2lf\n", propertyTax);

int main() {

double assessedValue;

scanf("%lf", &assessedValue); // Taking input

propertyTaxCalc(assessedValue); // Calling function to calculate property tax

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.

/ Single File Programming Question

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.

Refer to the sample output for the formatting specifications.

Code constraints :

The given test cases will fall under the following constraints:

50 ≤ n ≤ 1500

Sample test cases :


Input 1 :

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.

Here's the C program to solve the given problem:

```c

#include <stdio.h>

// Function to count the occurrences of the digit 3 in a given integer

int count_3s(int num) {

int count = 0;

while (num > 0) {

if (num % 10 == 3) // Check if the last digit is 3

count++;

num /= 10; // Remove the last digit

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;

for (int i = 2; i <= n; i++) {

total_count += count_3s(i); // Add the count of 3s in each number to the total count

return total_count;

int main() {

int n;

scanf("%d", &n); // Taking input

printf("%d\n", count_in_range(n)); // Calling function to calculate and display the total count

return 0;

```

This program defines two functions: `count_3s` and `count_in_range`.

- `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.

Single File Programming Question

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.

Help him with the program using call-by-reference.

Function Specifications:

To display factors - void factors(int );

To display digits - int count_digits(int *);

Input format :

The input consists of an integer N.

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.

Refer to the sample output for formatting specifications.


Code constraints :

The given test cases will fall under the following constraints:

3 ≤ N ≤ 1000

Sample test cases :

Input 1 :

99

Output 1 :

Factors: 1 3 9 11 33 99

Total digits: 2

Input 2 :

458

Output 2 :

Factors: 1 2 229 458

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.

Here's the C program to solve the given problem using call-by-reference:

```c

#include <stdio.h>

// Function to display factors of N

void factors(int N) {

printf("Factors: ");

for (int i = 1; i <= N; ++i) {


if (N % i == 0)

printf("%d ", i);

printf("\n");

// Function to count digits of N and store the count in a pointer variable

void count_digits(int N, int *count) {

*count = 0;

while (N != 0) {

++(*count);

N /= 10;

int main() {

int N;

scanf("%d", &N); // Taking input

factors(N); // Display factors of N

int digit_count;

count_digits(N, &digit_count); // Count digits of N

printf("Total digits: %d\n", digit_count); // Display total digits of N

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`.

Single File Programming Question

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.

Function Specifications: int shift(int *a, int *b, int *c)

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.

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ a, b, c ≤ 1000

Sample test cases :

Input 1 :

15 41 23

Output 1 :

15 41 23

23 15 41

Input 2 :

561 892 784

Output 2 :

561 892 784

784 561 892

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

void shift(int *a, int *b, int *c) {

int temp = *a;

int temp2 =*b;

*a = *c;

*b = temp;

*c = temp2;

int main() {

int a, b, c;

scanf("%d %d %d", &a, &b, &c); // Taking input

// Printing initial values

printf("%d %d %d\n", a, b, c);

shift(&a, &b, &c);

printf("%d %d %d\n", a, b, c);

return 0;

ṅ Single File Programming Question

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.

Function Specifications: int cube(int *n)

Input format :

The input consists of a positive integer n.

Output format :

The output prints an integer which is the cube value of n.

Refer to the sample output for formatting specifications.

Code constraints :
In this scenario, the test cases fall under the following constraints:

1 ≤ n ≤ 103

Sample test cases :

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.

Here's the C program to solve the given problem using call-by-reference:

```c

#include <stdio.h>

// Function to calculate the cube of a number using call-by-reference

int cube(int *n) {

return (*n) * (*n) * (*n);

}
int main() {

int n;

scanf("%d", &n); // Taking input

int result = cube(&n); // Calling cube function

printf("%d\n", result); // Printing the result

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.

Single File Programming Question

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

radian = angle * pi / 180.0

x = radius * cos(radian)

y = radius * sin(radian)

The pi value is calculated using M_PI constant from the math library.

Note: This question helps in clearing AMCAT exam.

Input format :

The first line consists of a double-value r, representing the radius.

The second line consists of a double-value a, representing the angle in degrees.

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

1.0 ≤ a < 360.0

Sample test cases :

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;

printf("Before call(%d): %d\n", n, m);

if (n > 0) {

recursiveFunc(n - 1);

printf("After return(%d): %d\n", n, m);

int main() {

int n;

scanf("%d", &n);

recursiveFunc(n);

return 0;

Single File Programming Question

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 :

The input consists of an integer n, representing the number to be checked.

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.

Refer to the sample output for formatting specifications.

Code constraints :

In the given scenario, the test cases fall under the following constraints:
1 ≤ n ≤ 1000

Sample test cases :

Input 1 :

Output 1 :

The number is NOT a duck number.

Input 2 :

330

Output 2 :

The number is a duck number.

Input 3 :

012

Output 3 :

The number is NOT a duck number.

Input 4 :

1000

Output 4 :

The number is a duck number.

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;

// Check for zero digit (excluding the first digit)

while (n > 0) {

if (n % 10 == 0) {

return 1; // Found a zero digit, it's a duck number

n /= 10; // Move to the next digit by integer division

// No zero digit found, not a duck number

return 0;

int main() {

int number;

scanf("%d", &number);

if (isDuckNumber(number)) {

printf("The number is a duck number.\n");

} else {

printf("The number is NOT a duck number.\n");

return 0;

Single File Programming Question


Problem Statement

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.

Function Specifications: void checkArmstrong(int *n)

Note: This question was asked in companies like Gemini Solution,TCS, Infosys, and Unthinkable
Solutions.

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".

Refer to the sample output for formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1 ≤ n ≤ 1000

Sample test cases :

Input 1 :

153

Output 1 :

Armstrong number

Input 2 :

29

Output 2 :

Not an Armstrong number

Input 3 :

Output 3 :

Armstrong number

Input 4 :

1000

Output 4 :

Not an Armstrong number


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>

#include <math.h> // For pow function

void checkArmstrong(int *n) {

int original = *n; // Store original value

int sum = 0, remainder, num_of_digits = 0;

// Count the number of digits

while (original != 0) {

original /= 10;

num_of_digits++;

original = *n; // Restore original value for calculation

// Calculate sum of digits raised to power of number of digits

while (original != 0) {

remainder = original % 10;

sum += pow(remainder, num_of_digits);

original /= 10;

// Check if sum is equal to original number

if (sum == *n) {

printf("Armstrong number\n");
} else {

printf("Not an Armstrong number\n");

int main() {

int number;

scanf("%d", &number);

checkArmstrong(&number); // Pass address of number

return 0;

Single File Programming Question

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.

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.

Refer to the sample output for formatting specifications.

Code constraints :

In the given scenario, the test cases fall under the following constraints:

100 ≤ n ≤ 105

Sample test cases :

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) {

// Base case: If n is zero, count 1 for the current zero digit

if (n == 0) {

return 1;

// Recursive case: Check the last digit and count zeros in remaining digits

if (n % 10 == 0) {

return 1 + countZeros(n / 10); // Add 1 for the current zero digit

} else {

return countZeros(n / 10); // Recurse for remaining digits

int main() {

int transactionAmount;

printf("Enter the transaction amount: ");

scanf("%d", &transactionAmount);

int zeroCount = countZeros(transactionAmount);

printf("Count of zeros: %d\n", zeroCount);

return 0;

}
```

Explanation:

1. **`countZeros` function (recursive):**

- 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:**

- `n % 10 == 0` checks if the last digit of `n` is zero.

- 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:**

- Prompts the user to enter the transaction amount using `printf`.

- Reads the input amount using `scanf`.

- Calls the `countZeros` function with the transaction amount to count zeros.

- Prints the count of zeros using `printf`.

Single File Programming Question

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 :

The input consists of an integer n.

Output format :

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

Refer to the sample output for the formatting specifications.

Code constraints :

In the given scenario, the test cases fall under the following constraints:

4 ≤ n ≤ 1000

Sample test cases :

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) {

int sum = 1; // Initialize sum with 1 (includes 1 as a divisor)

// Iterate from 2 to the square root of n (inclusive)

for (int i = 2; i * i <= n; i++) {

if (n % i == 0) { // Check if i is a divisor

sum += i; // Add the divisor i to the sum

// If i has a corresponding pair (n/i), add it as well (excluding duplicates)

if (i != n / i) {

sum += n / i;

}
return sum;

int main() {

int number;

printf("Enter a positive integer: ");

scanf("%d", &number);

int sumDivisors = sumOfDivisors(number);

printf("Sum of divisors: %d\n", sumDivisors);

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.

- **Looping through divisors:**

- 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).

- Inside the loop:

- `if (n % i == 0)` checks if `i` is a divisor of `n`.

- 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:**

- Prompts the user to enter a positive integer using `printf`.

- Reads the input number using `scanf`.

- Calls the `sumOfDivisors` function with the entered number.

- Prints the sum of divisors using `printf`.

Single File Programming Question

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%.

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.

Refer to the sample output for the formatting specifications.

Code constraints :

In this scenario, the test cases fall under the following constraints:

1000.0 ≤ Amount ≤ 10000.0

Sample test cases :

Input 1 :

2000.0

Output 1 :

Discounted amount: 1800.00

Input 2 :

1100.50

Output 2 :

Discounted amount: 990.45

Input 3 :

10000.0

Output 3 :
Discounted amount: 8000.00

Input 4 :

1000.0

Output 4 :

Discounted amount: 1000.00

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>

#include <math.h> // for round function (optional)

// Discount rates declared as global variables

double discount10 = 0.1;

double discount15 = 0.15;

double discount20 = 0.2;

double applyDiscount(double amount) {

if (amount > 5000) {

return amount * (1 - discount20); // Apply 20% discount

} else if (amount > 2000) {

return amount * (1 - discount15); // Apply 15% discount

} else if (amount > 1000) {

return amount * (1 - discount10); // Apply 10% discount

} else {

return amount; // No discount for amount <= 1000


}

int main() {

double purchaseAmount;

printf("Enter purchase amount: ");

scanf("%lf", &purchaseAmount);

double discountedAmount = applyDiscount(purchaseAmount);

// Rounding the discounted amount (optional)

// discountedAmount = round(discountedAmount * 100) / 100.0; // Round to two decimal places

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

return 0;

```

Explanation:

1. **Global discount variables:**

- We declare three global double variables `discount10`, `discount15`, and `discount20` to store
the discount rates (10%, 15%, and 20%).

2. **`applyDiscount` function:**

- Takes a double `amount` as input.

- 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.

- If the amount is less than or equal to 1000, no discount is applied.


- The function returns the final discounted amount.

3. **`main` function:**

- Prompts the user to enter the purchase amount using `printf`.

- Reads the input amount using `scanf`.

- 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.

Single File Programming Question

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.

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:

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 :

The input consists of a positive integer n.

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

Sample test cases :

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) {

static int count = 0; // Static variable to track call count (initialized to 0)

// Base case: Print 1 and reset count

if (num == 1) {

printf("1 ");

count = 0; // Reset count for the next sequence

return;

count++; // Increment count for each call

// Print current number

printf("%d ", num);

// Apply Collatz rules based on even/odd

if (num % 2 == 0) {

collatzSequence(num / 2);

} else {

collatzSequence(3 * num + 1);

int main() {

int steps;

printf("Enter the number of steps: ");

scanf("%d", &steps);

printf("Collatz Sequence: ");

collatzSequence(steps);
printf("\n");

return 0;

```

Explanation:

1. **Static variable `count`:**

- 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.

- **Increment count and print current number:**

- The `count` is incremented to track the number of steps in the sequence.

- The current `num` is printed.

- **Recursive calls based on even/odd:**

- 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:**

- Prompts the user to enter the number of steps using `printf`.

- Reads the input number using `scanf`.

- Prints a message indicating the start of the Collatz Sequence.

- Calls the `collatzSequence` function with the entered number of steps to initiate the sequence
calculation.
- Prints a newline character after the sequence is displayed.

You might also like