Mastering Loops and Conditional Statements in C Programming

C Programming – For those who are new to programming, one of the essential languages is C. Since they are the foundation of most programs, understanding loops and conditional statements is essential. This blog post will discuss some standard loop and c…


This content originally appeared on DEV Community and was authored by CodePassion

C Programming – For those who are new to programming, one of the essential languages is C. Since they are the foundation of most programs, understanding loops and conditional statements is essential. This blog post will discuss some standard loop and condition techniques in C programming that all newcomers should be familiar with.

Introduction to Conditional Statements and Loops in C Programming
Certain code blocks can be executed based on conditions thanks to conditional statements. If a condition is true, the if statement assesses it and then runs a block of code. You can check multiple criteria with the else if statement, and it also gives a default action in the event that none of the circumstances are met.

1. Positive number program

#include <stdio.h>

int main() {
 int num = 10;

if (num > 0) {
  printf("Number is positive.\n");
 } else if (num < 0) {
  printf("Number is negative.\n");
 } else {
 printf("Number is zero.\n");
 }
 return 0;
}

(Read more about positive number in c)

2. Reversing a Number

#include <stdio.h>

int RevNum(int num) {
    int R = 0;

    // Reversing the number
    while (num != 0) {
        int remainder = num % 10;
        R = R * 10 + remainder;
        num /= 10;
    }    
    return R;
}
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Reversed number: %d\n", RevNum(num));
    return 0;
}

(Read more about Reversing number in c)

3. Armstrong Number

#include <stdio.h>
#include <math.h>

// Function to calculate the number of digits in a number
int countDigits(int num) {
    int count = 0;
    while (num != 0) {
        num /= 10;
        ++count;
    }
    return count;
}

// Function to check if a number is an Armstrong number
int isArmstrong(int num) {
    int No, remainder, result = 0, n = 0, power;

    No = num;

    // Count number of digits
    n = countDigits(num);

    // Calculate result
    while (No != 0) {
        remainder = No % 10;

        // Power of remainder with respect to the number of digits
        power = round(pow(remainder, n));
        result += power;
        No /= 10;
    }

    // Check if num is an Armstrong number
    if (result == num)
        return 1; // Armstrong number
    else
        return 0; // Not an Armstrong number
}


int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (isArmstrong(num))
        printf("%d is an Armstrong number  =  ", num);
    else
        printf("%d is not an Armstrong number  =  ", num);

    return 0;
}

(Read more about Armstrong number in c)

4. Palindrome number

#include <stdio.h>
// Function to check if a number is palindrome or not
int P(int num) {
    int i = 0, no = num;
        // Reversing the number
    while (num != 0) {
        int remainder = num % 10;
        i = i * 10 + remainder;
        num /= 10;
    }    
    // Checking if the reversed number is equal to the original number
    if (no == i)
        return 1; // Palindrome no
    else
        return 0; // Not a palindrome
   end if
}
int main() 
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (P(num))
        printf("%d palindrome no.\n", num);
    else
        printf("%d is not a palindrome no .\n", num);
 end if
    return 0;
}

(Read more about Palindrome number in c)

Conclusion
These programs are crucial for novices to comprehend as they illustrate basic C programming ideas. Effective understanding of these ideas will be aided by practice and experimenting with these examples.


This content originally appeared on DEV Community and was authored by CodePassion


Print Share Comment Cite Upload Translate Updates
APA

CodePassion | Sciencx (2024-07-14T04:30:00+00:00) Mastering Loops and Conditional Statements in C Programming. Retrieved from https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/

MLA
" » Mastering Loops and Conditional Statements in C Programming." CodePassion | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/
HARVARD
CodePassion | Sciencx Sunday July 14, 2024 » Mastering Loops and Conditional Statements in C Programming., viewed ,<https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/>
VANCOUVER
CodePassion | Sciencx - » Mastering Loops and Conditional Statements in C Programming. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/
CHICAGO
" » Mastering Loops and Conditional Statements in C Programming." CodePassion | Sciencx - Accessed . https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/
IEEE
" » Mastering Loops and Conditional Statements in C Programming." CodePassion | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/. [Accessed: ]
rf:citation
» Mastering Loops and Conditional Statements in C Programming | CodePassion | Sciencx | https://www.scien.cx/2024/07/14/mastering-loops-and-conditional-statements-in-c-programming/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.