bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

commit 098b0287d3cad4997be43a1ddacaebc745804721
parent 6f236059bcadef399ef91afe5e549d326280d62b
Author: Amit Dutta <amitdutta4255@gmail.com>
Date:   Sun, 26 Oct 2025 21:08:00 +0530

new lucp

Diffstat:
Mdocs/index.html | 247+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Aletusc/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&lt;stdio.h&gt; - -int main() { - printf(&quot;Combination of 1, 2, 3 upto thousands :&quot;); - for (int i = 0; i &lt;= 3; i++) - { - for (int j = 0; j &lt;= 3; j++) - { - for (int k = 0; k &lt;= 3; k++) - { - for (int l = 0; l &lt;= 3; l++) - { - int num = (i * 1000) + (j * 100) + (k * 10) + l; - printf(&quot; %d&quot;, 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 &#x27;Exit&#x27; option the program should continue work. +*/ +/* Author - Amit Dutta, Date - 26th OCT, 2025 */ +/* Let Us C, Chap - 7, Page - 118, Problem 7.1 */ + +#include &lt;stdio.h&gt; +#include &lt;math.h&gt; +#include &lt;stdlib.h&gt; +#include &lt;stdbool.h&gt; + +// Function to clear the input buffer after scanf to prevent issues in the next input. +void clearInputBuffer() +{ + int c; + while ((c = getchar()) != &#x27;\n&#x27; &amp;&amp; c != EOF) + ; +} + +// Calculates the factorial of the input number. +void factorial(int num) +{ + // Factorial is not defined for negative numbers. + if (num &lt; 0) + { + printf(&quot;\nFactorial is not defined for negative numbers.&quot;); + return; + } + // Checks for input over 20 to prevent long long integer overflow (20! is max safe). + if (num &gt; 20) + { + printf(&quot;\nFactorial of %d is too large to calculate (max safe integer factorial is 20!).&quot;, num); + return; + } + long long fact = 1; + // Calculate factorial iteratively. + for (int i = 1; i &lt;= num; i++) + { + fact *= i; + } + printf(&quot;\nFactorial of %d = %lld&quot;, 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(&quot;\nInput 1 is NOT a PRIME NUMBER.&quot;); + return; + } + else if (num == 2) + { + printf(&quot;\nInput 2 is a PRIME NUMBER. (Fact : 2 is only even prime number)&quot;); + return; + } + // Exclude all other even numbers. + if (num % 2 == 0) + { + printf(&quot;\nInput %d is NOT a PRIME NUMBER.&quot;, 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 &lt;= endCheckDigit; i += 2) + { + if (num % i == 0) + { + printf(&quot;\nInput %d is NOT a PRIME NUMBER.&quot;, num); + isPrime = false; + break; + } + } + if (isPrime) + { + printf(&quot;\nInput %d is a PRIME NUMBER.&quot;, 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(&quot;\nInput %d is a EVEN NUMBER.&quot;, num); + return; + } + else + { + printf(&quot;\nInput %d is a ODD NUMBER.&quot;, 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 &#x27;Exit&#x27; is chosen. + while (1) + { + // Display menu options. + printf(&quot;\n\n===== MENU =====&quot; + &quot;\n1. Factorial of a number&quot; + &quot;\n2. Prime or not&quot; + &quot;\n3. Odd or Even&quot; + &quot;\n4. Exit&quot;); + printf(&quot;\nEnter your choice : &quot;); + + // Input validation for menu choice. + if (scanf(&quot;%d&quot;, &amp;choice) != 1) + { + printf(&quot;\nPlease enter a number.&quot;); + clearInputBuffer(); + continue; + } + clearInputBuffer(); + + // Handle menu selection using switch-case. + switch (choice) + { + case 1: + printf(&quot;\n=== FACTORIAL OF A NUMBER ===&quot;); + printf(&quot;\nEnter the number : &quot;); + // Input validation for the number to be factored. + if (scanf(&quot;%d&quot;, &amp;num) != 1) + { + printf(&quot;\nPlease enter a number.&quot;); + clearInputBuffer(); + continue; + } + clearInputBuffer(); + factorial(num); + break; + case 2: + printf(&quot;\n=== PRIME OR NOT ===&quot;); + printf(&quot;\nEnter the number : &quot;); + // Input validation for the number to be checked. + if (scanf(&quot;%d&quot;, &amp;num) != 1) + { + printf(&quot;\nPlease enter a number.&quot;); + clearInputBuffer(); + continue; + } + clearInputBuffer(); + // Require a non-negative, non-zero number for prime check. + if (num &lt; 0) + { + printf(&quot;\nPlease enter a postive number.&quot;); + continue; + } + else if (num == 0) + { + printf(&quot;\nPlease enter a non-zero number.&quot;); + continue; + } + prime(num); + break; + case 3: + printf(&quot;\n=== ODD OR EVEN ===&quot;); + printf(&quot;\nEnter the number : &quot;); + // Input validation for the number to be checked. + if (scanf(&quot;%d&quot;, &amp;num) != 1) + { + printf(&quot;\nPlease enter a number.&quot;); + clearInputBuffer(); + continue; + } + clearInputBuffer(); + oddoreven(num); + break; + case 4: + // Exit the program cleanly. + printf(&quot;\nExiting the program.\n\nSAYONARA...\n\n&quot;); + exit(0); + default: + // Handle invalid menu choice input. + printf(&quot;\nPlease enter a valid choice.&quot;); + 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
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror