Posts

Weird Algorithm || Introductory Problem || CSES

#include <bits/stdc++.h> using namespace std ; #define MOD 1000000007 ; #define int long long int int32_t main (){     ios_base :: sync_with_stdio ( false );     cin . tie ( NULL );     #ifndef ONLINE_JUDGE     freopen ( "input.txt" , "r" , stdin );     #endif     //----Solution-----//     int n ;     cin >> n ;     while ( n != 1 ){         cout << n << " " ;         if ( n & 1 ){ // if n is not divisible by 3 then             n = n * 3 + 1 ; // multiply it by 3 and add 1         } else {             n /= 2 ; // else divide it by 2         }     }     cout << n ;     return 0 ; }

Calculating Factorials: Harnessing Loops in C Programming

#include <stdio.h> int main () {     int n ;     // Prompt user to enter a number     printf ( "Enter a number: " );     scanf ( " %d " , & n );     int factorial = 1 ; // Initialize factorial to 1     // Loop to calculate the factorial of the entered number     for ( int i = 1 ; n >= i ; i ++ )     {         factorial = factorial * i ; // Calculate the factorial incrementally     }     // Display the calculated factorial     printf ( "Factorial of %d is %d " , n , factorial );     return 0 ; }   // Blog Title: "Calculating Factorials: Harnessing Loops in C Programming" // Keywords: C programming, loops, factorial calculation, iterative computation, mathematical operations

Calculating the Sum of First n Natural Numbers using Loops in C || Unveiling the Logic behind Sum of First n Natural Numbers

#include <stdio.h> int main () {     int n ;     int sum = 0 ;     // Prompt user to enter a number     printf ( "Enter a number: " );     scanf ( " %d " , & n );     // Loop to calculate the sum of first n natural numbers     for ( int i = 0 ; n >= i ; i ++ )     {         ; // Accumulate the current value of 'i' into 'sum'         /*For n=3,         when loop executes first time(i=1),sum = sum + i means 0+1=1         when loop executes second time(i=2),sum = sum + i means 1+2=3         when loop executes third time(i=3),sum = sum + i means 3+3;         */     }     // Display the sum of the first n natural numbers     printf ( "Sum of first %d natural numbers is %d " , n , sum );     return 0 ; } // Blog Title: "Sum of Fi...

Generating a Sequence: Exploring Loops in C Programming

#include <stdio.h> int main () {     int num ;     int a ;     // Prompt user to enter the number of digits to print     printf ( "How many digits you want to print: \n " );     scanf ( " %d " , & num );     // Loop to print the specified number of digits     for ( a = 1 ; num >= a ; a ++ )     {         printf ( " %d \n " , a );     }     return 0 ; } // Blog Title: "Generating a Sequence: Exploring Loops in C Programming" // Keywords: C programming, loops, sequence generation, number printing

Exploring Arrays and Loops: Finding the Greatest Digit in C Programming

Discover the power of arrays and loops in programming with our latest blog post. We present a clear and concise example in C programming that demonstrates how to efficiently find the largest digit among a series of inputs. Learn our step-by-step approach to gather user input, store it in an array, and employ loops for seamless comparison. Uncover the foundational concepts behind this problem-solving technique and gain insights into the essential role of arrays and loops in programming. Whether you're a beginner or honing your coding skills, this post offers practical knowledge and a deeper understanding of these fundamental programming tools. #include <stdio.h> int main () {     int a ;     printf ( "How many digits you want to compare: \n " );     scanf ( " %d " , & a );     int num [ a ]; // Declare an array to store the input digits     // Input loop: Prompt user to enter digits and store them in the array   ...

Identifying Uppercase and Lowercase Characters in C

#include <stdio.h> int main () {     char a ; // Variable to store the input character         printf ( "Enter a character:" ); // Prompt the user to enter a character     scanf ( " %c " , & a ); // Read the character input from the user         if ( a >= 97 && a <= 122 ) { // Check if the character is a lowercase alphabet (ASCII values for 'a' to 'z' are 97 to 122)         printf ( "It is a lowercase alphabet" );     }     else if ( a >= 65 && a <= 90 ) { // Check if the character is an uppercase alphabet (ASCII values for 'A' to 'Z' are 65 to 90)         printf ( "It is an uppercase character" );     }     else { // If the character is neither uppercase nor lowercase, it is not an alphabet         printf ( "It is not an alphabet" );     }     ret...

Finding the Greatest Number Among Three - A C Code Implementation

Description: This blog post presents a C code to find the greatest number among three user-input integers. The code uses scanf to interact with the user and employs an optimized comparison logic to efficiently determine the largest value. By eliminating unnecessary nested if statements, the program becomes more concise and easier to understand. The post includes a commented version of the code, explaining each step of the process for better comprehension. You can also use this way to approach this problem using arrays and loops: Click here . #include <stdio.h> int main () {     int a , b , c ;     // Prompt the user to enter three integers     printf ( "Enter first number: " );     scanf ( " %d " , & a );     printf ( "Enter second number: " );     scanf ( " %d " , & b );     printf ( "Enter third number: " );     scanf ( " %d " , & c );     // Compare the three numbers to fi...