commit 098b0287d3cad4997be43a1ddacaebc745804721
parent 6f236059bcadef399ef91afe5e549d326280d62b
Author: Amit Dutta <amitdutta4255@gmail.com>
Date: Sun, 26 Oct 2025 21:08:00 +0530
new lucp
Diffstat:
| M | docs/index.html | | | 247 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
| A | letusc/lucproblem011.c | | | 195 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 404 insertions(+), 38 deletions(-)
diff --git a/docs/index.html b/docs/index.html
@@ -3429,44 +3429,6 @@ int main()
<li>
<div class="flex items-center p-3 pl-8 hover:bg-gray-50 transition-colors duration-200">
<svg class="w-6 h-6 text-green-500 mr-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
- <span class="text-gray-700">lucproblem010-com.c</span>
- <div class="ml-auto flex items-center space-x-4">
- <a href="https://github.com/notamitgamer/bsc/blob/main/letusc/lucproblem010-com.c" target="_blank" class="p-1 rounded-full text-gray-500 hover:bg-gray-200 hover:text-gray-800 transition-colors" title="View file on GitHub">
- <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M12 2C6.477 2 2 6.477 2 12c0 4.418 2.865 8.168 6.839 9.492.5.092.682-.217.682-.482 0-.237-.009-.868-.014-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.031-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.203 2.398.1 2.651.64.7 1.03 1.595 1.03 2.688 0 3.848-2.338 4.695-4.566 4.942.359.308.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.001 10.001 0 0022 12c0-5.523-4.477-10-10-10z" clip-rule="evenodd"></path></svg>
- </a>
- <button onclick="showCode('code-letusc-lucproblem010-com_c', 'lucproblem010-com.c')" class="text-green-600 font-semibold text-sm hover:underline">View Code</button>
- </div>
- </div>
- <div id="code-letusc-lucproblem010-com_c" class="hidden" data-github-url="https://github.com/notamitgamer/bsc/blob/main/letusc/lucproblem010-com.c">
- <pre><code class="language-c">/* Write a program to generate all combination of 1, 2 and 3 using for loop. */
-/* Author - Amit Dutta, Date - 24th OCT, 2025 */
-/* Let Us C, Chap - 6, Page - 103, Problem 6.3 */
-
-#include<stdio.h>
-
-int main() {
- printf("Combination of 1, 2, 3 upto thousands :");
- for (int i = 0; i <= 3; i++)
- {
- for (int j = 0; j <= 3; j++)
- {
- for (int k = 0; k <= 3; k++)
- {
- for (int l = 0; l <= 3; l++)
- {
- int num = (i * 1000) + (j * 100) + (k * 10) + l;
- printf(" %d", num);
- }
- }
- }
- }
-}</code></pre>
- </div>
- </li>
-
- <li>
- <div class="flex items-center p-3 pl-8 hover:bg-gray-50 transition-colors duration-200">
- <svg class="w-6 h-6 text-green-500 mr-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
<span class="text-gray-700">lucproblem010-complex.c</span>
<div class="ml-auto flex items-center space-x-4">
<a href="https://github.com/notamitgamer/bsc/blob/main/letusc/lucproblem010-complex.c" target="_blank" class="p-1 rounded-full text-gray-500 hover:bg-gray-200 hover:text-gray-800 transition-colors" title="View file on GitHub">
@@ -3604,6 +3566,215 @@ int main()
}</code></pre>
</div>
</li>
+
+ <li>
+ <div class="flex items-center p-3 pl-8 hover:bg-gray-50 transition-colors duration-200">
+ <svg class="w-6 h-6 text-green-500 mr-4 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
+ <span class="text-gray-700">lucproblem011.c</span>
+ <div class="ml-auto flex items-center space-x-4">
+ <a href="https://github.com/notamitgamer/bsc/blob/main/letusc/lucproblem011.c" target="_blank" class="p-1 rounded-full text-gray-500 hover:bg-gray-200 hover:text-gray-800 transition-colors" title="View file on GitHub">
+ <svg class="w-6 h-6" fill="currentColor" viewBox="0 0 24 24" aria-hidden="true"><path fill-rule="evenodd" d="M12 2C6.477 2 2 6.477 2 12c0 4.418 2.865 8.168 6.839 9.492.5.092.682-.217.682-.482 0-.237-.009-.868-.014-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.031-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.203 2.398.1 2.651.64.7 1.03 1.595 1.03 2.688 0 3.848-2.338 4.695-4.566 4.942.359.308.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.001 10.001 0 0022 12c0-5.523-4.477-10-10-10z" clip-rule="evenodd"></path></svg>
+ </a>
+ <button onclick="showCode('code-letusc-lucproblem011_c', 'lucproblem011.c')" class="text-green-600 font-semibold text-sm hover:underline">View Code</button>
+ </div>
+ </div>
+ <div id="code-letusc-lucproblem011_c" class="hidden" data-github-url="https://github.com/notamitgamer/bsc/blob/main/letusc/lucproblem011.c">
+ <pre><code class="language-c">/* Write a menu driven program which has following options :
+ 1. Factorial of a number
+ 2. Prime or not
+ 3. Odd or even
+ 4. Exit
+Once a menu item is selected the appropriate action should be taken
+and once this action is finished, the menu should reappear. Unless
+the user selects the 'Exit' option the program should continue work.
+*/
+/* Author - Amit Dutta, Date - 26th OCT, 2025 */
+/* Let Us C, Chap - 7, Page - 118, Problem 7.1 */
+
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+// Function to clear the input buffer after scanf to prevent issues in the next input.
+void clearInputBuffer()
+{
+ int c;
+ while ((c = getchar()) != '\n' && c != EOF)
+ ;
+}
+
+// Calculates the factorial of the input number.
+void factorial(int num)
+{
+ // Factorial is not defined for negative numbers.
+ if (num < 0)
+ {
+ printf("\nFactorial is not defined for negative numbers.");
+ return;
+ }
+ // Checks for input over 20 to prevent long long integer overflow (20! is max safe).
+ if (num > 20)
+ {
+ printf("\nFactorial of %d is too large to calculate (max safe integer factorial is 20!).", num);
+ return;
+ }
+ long long fact = 1;
+ // Calculate factorial iteratively.
+ for (int i = 1; i <= num; i++)
+ {
+ fact *= i;
+ }
+ printf("\nFactorial of %d = %lld", num, fact);
+ return;
+}
+
+// Checks if the input number is a prime number.
+void prime(int num)
+{
+ // Handle special cases: 1 and 2.
+ if (num == 1)
+ {
+ printf("\nInput 1 is NOT a PRIME NUMBER.");
+ return;
+ }
+ else if (num == 2)
+ {
+ printf("\nInput 2 is a PRIME NUMBER. (Fact : 2 is only even prime number)");
+ return;
+ }
+ // Exclude all other even numbers.
+ if (num % 2 == 0)
+ {
+ printf("\nInput %d is NOT a PRIME NUMBER.", num);
+ return;
+ }
+ // Optimization: Only check divisors up to the square root of num.
+ int endCheckDigit = sqrt(num);
+ bool isPrime = true;
+ // Check only odd divisors (i += 2) starting from 3.
+ for (int i = 3; i <= endCheckDigit; i += 2)
+ {
+ if (num % i == 0)
+ {
+ printf("\nInput %d is NOT a PRIME NUMBER.", num);
+ isPrime = false;
+ break;
+ }
+ }
+ if (isPrime)
+ {
+ printf("\nInput %d is a PRIME NUMBER.", num);
+ return;
+ }
+}
+
+// Checks if the input number is odd or even.
+void oddoreven(int num)
+{
+ // A number is even if it is perfectly divisible by 2.
+ if (num % 2 == 0)
+ {
+ printf("\nInput %d is a EVEN NUMBER.", num);
+ return;
+ }
+ else
+ {
+ printf("\nInput %d is a ODD NUMBER.", num);
+ return;
+ }
+}
+
+// Main function: displays the menu and controls program flow.
+int main()
+{
+ int choice, num;
+ // Infinite loop ensures the menu reappears after every operation until 'Exit' is chosen.
+ while (1)
+ {
+ // Display menu options.
+ printf("\n\n===== MENU ====="
+ "\n1. Factorial of a number"
+ "\n2. Prime or not"
+ "\n3. Odd or Even"
+ "\n4. Exit");
+ printf("\nEnter your choice : ");
+
+ // Input validation for menu choice.
+ if (scanf("%d", &choice) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+
+ // Handle menu selection using switch-case.
+ switch (choice)
+ {
+ case 1:
+ printf("\n=== FACTORIAL OF A NUMBER ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be factored.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ factorial(num);
+ break;
+ case 2:
+ printf("\n=== PRIME OR NOT ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be checked.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ // Require a non-negative, non-zero number for prime check.
+ if (num < 0)
+ {
+ printf("\nPlease enter a postive number.");
+ continue;
+ }
+ else if (num == 0)
+ {
+ printf("\nPlease enter a non-zero number.");
+ continue;
+ }
+ prime(num);
+ break;
+ case 3:
+ printf("\n=== ODD OR EVEN ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be checked.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ oddoreven(num);
+ break;
+ case 4:
+ // Exit the program cleanly.
+ printf("\nExiting the program.\n\nSAYONARA...\n\n");
+ exit(0);
+ default:
+ // Handle invalid menu choice input.
+ printf("\nPlease enter a valid choice.");
+ break;
+ }
+ }
+}</code></pre>
+ </div>
+ </li>
</ul>
</div>
</li>
diff --git a/letusc/lucproblem011.c b/letusc/lucproblem011.c
@@ -0,0 +1,194 @@
+/* Write a menu driven program which has following options :
+ 1. Factorial of a number
+ 2. Prime or not
+ 3. Odd or even
+ 4. Exit
+Once a menu item is selected the appropriate action should be taken
+and once this action is finished, the menu should reappear. Unless
+the user selects the 'Exit' option the program should continue work.
+*/
+/* Author - Amit Dutta, Date - 26th OCT, 2025 */
+/* Let Us C, Chap - 7, Page - 118, Problem 7.1 */
+
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+// Function to clear the input buffer after scanf to prevent issues in the next input.
+void clearInputBuffer()
+{
+ int c;
+ while ((c = getchar()) != '\n' && c != EOF)
+ ;
+}
+
+// Calculates the factorial of the input number.
+void factorial(int num)
+{
+ // Factorial is not defined for negative numbers.
+ if (num < 0)
+ {
+ printf("\nFactorial is not defined for negative numbers.");
+ return;
+ }
+ // Checks for input over 20 to prevent long long integer overflow (20! is max safe).
+ if (num > 20)
+ {
+ printf("\nFactorial of %d is too large to calculate (max safe integer factorial is 20!).", num);
+ return;
+ }
+ long long fact = 1;
+ // Calculate factorial iteratively.
+ for (int i = 1; i <= num; i++)
+ {
+ fact *= i;
+ }
+ printf("\nFactorial of %d = %lld", num, fact);
+ return;
+}
+
+// Checks if the input number is a prime number.
+void prime(int num)
+{
+ // Handle special cases: 1 and 2.
+ if (num == 1)
+ {
+ printf("\nInput 1 is NOT a PRIME NUMBER.");
+ return;
+ }
+ else if (num == 2)
+ {
+ printf("\nInput 2 is a PRIME NUMBER. (Fact : 2 is only even prime number)");
+ return;
+ }
+ // Exclude all other even numbers.
+ if (num % 2 == 0)
+ {
+ printf("\nInput %d is NOT a PRIME NUMBER.", num);
+ return;
+ }
+ // Optimization: Only check divisors up to the square root of num.
+ int endCheckDigit = sqrt(num);
+ bool isPrime = true;
+ // Check only odd divisors (i += 2) starting from 3.
+ for (int i = 3; i <= endCheckDigit; i += 2)
+ {
+ if (num % i == 0)
+ {
+ printf("\nInput %d is NOT a PRIME NUMBER.", num);
+ isPrime = false;
+ break;
+ }
+ }
+ if (isPrime)
+ {
+ printf("\nInput %d is a PRIME NUMBER.", num);
+ return;
+ }
+}
+
+// Checks if the input number is odd or even.
+void oddoreven(int num)
+{
+ // A number is even if it is perfectly divisible by 2.
+ if (num % 2 == 0)
+ {
+ printf("\nInput %d is a EVEN NUMBER.", num);
+ return;
+ }
+ else
+ {
+ printf("\nInput %d is a ODD NUMBER.", num);
+ return;
+ }
+}
+
+// Main function: displays the menu and controls program flow.
+int main()
+{
+ int choice, num;
+ // Infinite loop ensures the menu reappears after every operation until 'Exit' is chosen.
+ while (1)
+ {
+ // Display menu options.
+ printf("\n\n===== MENU ====="
+ "\n1. Factorial of a number"
+ "\n2. Prime or not"
+ "\n3. Odd or Even"
+ "\n4. Exit");
+ printf("\nEnter your choice : ");
+
+ // Input validation for menu choice.
+ if (scanf("%d", &choice) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+
+ // Handle menu selection using switch-case.
+ switch (choice)
+ {
+ case 1:
+ printf("\n=== FACTORIAL OF A NUMBER ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be factored.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ factorial(num);
+ break;
+ case 2:
+ printf("\n=== PRIME OR NOT ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be checked.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ // Require a non-negative, non-zero number for prime check.
+ if (num < 0)
+ {
+ printf("\nPlease enter a postive number.");
+ continue;
+ }
+ else if (num == 0)
+ {
+ printf("\nPlease enter a non-zero number.");
+ continue;
+ }
+ prime(num);
+ break;
+ case 3:
+ printf("\n=== ODD OR EVEN ===");
+ printf("\nEnter the number : ");
+ // Input validation for the number to be checked.
+ if (scanf("%d", &num) != 1)
+ {
+ printf("\nPlease enter a number.");
+ clearInputBuffer();
+ continue;
+ }
+ clearInputBuffer();
+ oddoreven(num);
+ break;
+ case 4:
+ // Exit the program cleanly.
+ printf("\nExiting the program.\n\nSAYONARA...\n\n");
+ exit(0);
+ default:
+ // Handle invalid menu choice input.
+ printf("\nPlease enter a valid choice.");
+ break;
+ }
+ }
+}+
\ No newline at end of file