0% found this document useful (0 votes)
6 views7 pages

C Problem Statement

Uploaded by

sm9395
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)
6 views7 pages

C Problem Statement

Uploaded by

sm9395
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

1.

Problem Statement
Write a program to find all odd multiples of a given number n within a range [low,
high].
• If no odd multiples exist, display a message accordingly.
• Print the multiples in reverse order.
2. Solution Code
#include <stdio.h>
void findOddMultiples(int n, int low, int high) {
int found = 0;
int multiples[100], count = 0;
for (int i = high; i >= low; i--) {
if (i % n == 0 && i % 2 != 0) {
multiples[count++] = i;
found = 1;
}
if (found) {
printf("Odd multiples of %d in reverse order: ", n);
for (int i = 0; i < count; i++) {
printf("%d ", multiples[i]);
}}
printf("\n");
} else {
printf("No odd multiples of %d found in the range [%d, %d].\n", n, low, high);}
}
int main() {
int n, low, high;
printf("Enter the number: ");
scanf("%d", &n);
printf("Enter the range (low high): ");
scanf("%d %d", &low, &high);
if (low > high) {
printf("Invalid range: low should be less than or equal to high.\n");
return 1;
}

findOddMultiples(n, low, high);


return 0;}
3. Screenshot of Execution
Correct Test Case:

Correct Test Case:

• Input:

o Number: 3

o Range: 10 30

• Output: Odd multiples of 3 in reverse order: 27 21 15

Refute Test Case:

Refute Test Case:

• Input:

o Number: 4 (even number)

o Range: 10 20

• Output: No odd multiples of 4 found in the range [10, 20].


[Link] of Failure

The program fails for cases where n is even or if low > high, as there
are logical limitations:
1. Even numbers cannot have odd multiples.
2. Invalid ranges aren’t addressed explicitly in computation logic.
Fix Suggestion
• Add validation for n to ensure it's odd.
• Check and prompt the user for invalid ranges before
processing.
1. Problem Statement
Write a program to determine if a given number is a Perfect Number. A Perfect
Number is a positive integer that is equal to the sum of its proper divisors (excluding
itself). For example, 6 is a perfect number because its divisors are 1, 2, 3, and 1 + 2 +
3 = 6.
2. Solution Code
#include <stdio.h>
// Function to check if a number is a Perfect Number
int isPerfectNumber(int num) {
if (num <= 0) return 0; // Negative numbers and 0 are not perfect numbers

int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum == num; // Returns 1 if true, 0 otherwise
}
int main() {
int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
} else {
if (isPerfectNumber(number)) {
printf("%d is a Perfect Number.\n", number);
} else {
printf("%d is not a Perfect Number.\n", number);
}
}
return 0;
3. Screenshot of Execution
Correct Test Case:

Input:6 | output: 6 is a Perfect Number | Expected Output: 6 is a


Perfect Number
Refute Test Case:

Input:-36 | output: Invalid input. Please enter a positive integer |


Expected Output: 36 is not a Perfect Number
[Link] of Failure

Invalid Input: Negative Numbers or Zero


• Example Input: -6 or 0
• Why it Fails: The program doesn’t explicitly check for
positive integers. If the function isPerfectNumber() is called
with such inputs, the loop to calculate divisors will be
skipped, returning a meaningless result (0 is not a perfect
number).
• Correct Behavior: Add a validation check to ensure the
input is a positive integer and handle invalid inputs
gracefully.

You might also like